Skip to content

Fix certain WebConversionServiceProvider#convert calls by supplying contextual information #1096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import org.apache.commons.lang3.StringUtils;

import org.springframework.core.MethodParameter;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestHeader;
Expand Down Expand Up @@ -101,7 +102,7 @@ else if (cookieValue != null)
if (this.defaultValue !=null && !ValueConstants.DEFAULT_NONE.equals(this.defaultValue.toString())){
this.defaultValue = propertyResolverUtils.resolve(this.defaultValue.toString());
parameterBuilder.getOptionalWebConversionServiceProvider()
.ifPresent(conversionService ->this.defaultValue= conversionService.convert(this.defaultValue, methodParameter.getParameterType()));
.ifPresent(conversionService ->this.defaultValue= conversionService.convert(this.defaultValue, new TypeDescriptor(methodParameter)));
}

this.required = this.required && !methodParameter.isOptional();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.boot.autoconfigure.web.format.DateTimeFormatters;
import org.springframework.boot.autoconfigure.web.format.WebConversionService;
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties.Format;
import org.springframework.core.convert.TypeDescriptor;
import org.springframework.lang.Nullable;

/**
Expand Down Expand Up @@ -34,15 +35,14 @@ public WebConversionServiceProvider(Optional<WebConversionService> webConversion
}

/**
* Convert t.
* Attempts to convert {@code source} into the target type as described by {@code targetTypeDescriptor}.
*
* @param <T> the type parameter
* @param source the source
* @param targetType the target type
* @return the t
* @param targetTypeDescriptor the target type descriptor
* @return the converted source
*/
@Nullable
public <T> T convert(@Nullable Object source, Class<T> targetType) {
return webConversionService.convert(source, targetType);
public Object convert(@Nullable Object source, TypeDescriptor targetTypeDescriptor) {
return webConversionService.convert(source, targetTypeDescriptor);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,16 @@

package test.org.springdoc.api.app150;

import static org.springframework.format.annotation.DateTimeFormat.ISO.DATE;

import java.time.LocalDate;
import java.util.List;

import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;

import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
Expand Down Expand Up @@ -54,4 +58,11 @@ public void test3(@RequestParam(defaultValue = "users,123") List<String> toto) {

}

@GetMapping("/test4")
@ApiResponse(responseCode = "204", description = "No content")
@ResponseStatus(value = HttpStatus.NO_CONTENT)
public void test4(@DateTimeFormat(iso = DATE) @RequestParam(defaultValue = "2021-03-08") LocalDate localDate) {

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@
}
],
"paths": {
"/test4": {
"get": {
"tags": [
"hello-controller"
],
"operationId": "test4",
"parameters": [
{
"name": "localDate",
"in": "query",
"required": false,
"schema": {
"type": "string",
"format": "date"
}
}
],
"responses": {
"204": {
"description": "No content"
}
}
}
},
"/test3": {
"get": {
"tags": [
Expand Down