Skip to content

Commit 450763c

Browse files
committed
Use TypeDescriptor information for default value conversions.
1 parent 58c777f commit 450763c

File tree

4 files changed

+43
-7
lines changed

4 files changed

+43
-7
lines changed

springdoc-openapi-common/src/main/java/org/springdoc/core/ParameterInfo.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.apache.commons.lang3.StringUtils;
2727

2828
import org.springframework.core.MethodParameter;
29+
import org.springframework.core.convert.TypeDescriptor;
2930
import org.springframework.web.bind.annotation.CookieValue;
3031
import org.springframework.web.bind.annotation.PathVariable;
3132
import org.springframework.web.bind.annotation.RequestHeader;
@@ -101,7 +102,7 @@ else if (cookieValue != null)
101102
if (this.defaultValue !=null && !ValueConstants.DEFAULT_NONE.equals(this.defaultValue.toString())){
102103
this.defaultValue = propertyResolverUtils.resolve(this.defaultValue.toString());
103104
parameterBuilder.getOptionalWebConversionServiceProvider()
104-
.ifPresent(conversionService ->this.defaultValue= conversionService.convert(this.defaultValue, methodParameter.getParameterType()));
105+
.ifPresent(conversionService ->this.defaultValue= conversionService.convert(this.defaultValue, new TypeDescriptor(methodParameter)));
105106
}
106107

107108
this.required = this.required && !methodParameter.isOptional();

springdoc-openapi-common/src/main/java/org/springdoc/core/WebConversionServiceProvider.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.springframework.boot.autoconfigure.web.format.DateTimeFormatters;
66
import org.springframework.boot.autoconfigure.web.format.WebConversionService;
77
import org.springframework.boot.autoconfigure.web.reactive.WebFluxProperties.Format;
8+
import org.springframework.core.convert.TypeDescriptor;
89
import org.springframework.lang.Nullable;
910

1011
/**
@@ -34,15 +35,14 @@ public WebConversionServiceProvider(Optional<WebConversionService> webConversion
3435
}
3536

3637
/**
37-
* Convert t.
38+
* Attempts to convert {@code source} into the target type as described by {@code targetTypeDescriptor}.
3839
*
39-
* @param <T> the type parameter
4040
* @param source the source
41-
* @param targetType the target type
42-
* @return the t
41+
* @param targetTypeDescriptor the target type descriptor
42+
* @return the converted source
4343
*/
4444
@Nullable
45-
public <T> T convert(@Nullable Object source, Class<T> targetType) {
46-
return webConversionService.convert(source, targetType);
45+
public Object convert(@Nullable Object source, TypeDescriptor targetTypeDescriptor) {
46+
return webConversionService.convert(source, targetTypeDescriptor);
4747
}
4848
}

springdoc-openapi-webmvc-core/src/test/java/test/org/springdoc/api/app150/HelloController.java

+11
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,16 @@
1818

1919
package test.org.springdoc.api.app150;
2020

21+
import static org.springframework.format.annotation.DateTimeFormat.ISO.DATE;
22+
23+
import java.time.LocalDate;
2124
import java.util.List;
2225

2326
import io.swagger.v3.oas.annotations.Parameter;
2427
import io.swagger.v3.oas.annotations.media.Schema;
2528
import io.swagger.v3.oas.annotations.responses.ApiResponse;
2629

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

5559
}
5660

61+
@GetMapping("/test4")
62+
@ApiResponse(responseCode = "204", description = "No content")
63+
@ResponseStatus(value = HttpStatus.NO_CONTENT)
64+
public void test4(@DateTimeFormat(iso = DATE) @RequestParam(defaultValue = "2021-03-08") LocalDate localDate) {
65+
66+
}
67+
5768
}

springdoc-openapi-webmvc-core/src/test/resources/results/app150.json

+24
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,30 @@
1111
}
1212
],
1313
"paths": {
14+
"/test4": {
15+
"get": {
16+
"tags": [
17+
"hello-controller"
18+
],
19+
"operationId": "test4",
20+
"parameters": [
21+
{
22+
"name": "localDate",
23+
"in": "query",
24+
"required": false,
25+
"schema": {
26+
"type": "string",
27+
"format": "date"
28+
}
29+
}
30+
],
31+
"responses": {
32+
"204": {
33+
"description": "No content"
34+
}
35+
}
36+
}
37+
},
1438
"/test3": {
1539
"get": {
1640
"tags": [

0 commit comments

Comments
 (0)