Skip to content

Commit 33a55c9

Browse files
committed
Merge pull request #3244 from Mattias-Sehlstedt/support-local-date-default-value
(cherry picked from commit 963ee22)
1 parent e7106d3 commit 33a55c9

File tree

4 files changed

+50
-19
lines changed

4 files changed

+50
-19
lines changed

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/service/AbstractRequestService.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656

5757
import com.fasterxml.jackson.annotation.JsonView;
5858
import io.swagger.v3.core.converter.AnnotatedType;
59-
import io.swagger.v3.core.util.PrimitiveType;
6059
import io.swagger.v3.oas.annotations.Parameters;
6160
import io.swagger.v3.oas.annotations.enums.ParameterIn;
6261
import io.swagger.v3.oas.models.Components;
@@ -435,8 +434,12 @@ else if (!RequestMethod.GET.equals(requestMethod) || OpenApiVersion.OPENAPI_3_1.
435434
* @param parametersDocMap the parameters doc map
436435
* @return the parameter linked hash map
437436
*/
438-
private LinkedHashMap<ParameterId, Parameter> getParameterLinkedHashMap(Components components, MethodAttributes methodAttributes, List<Parameter> operationParameters, Map<ParameterId, io.swagger.v3.oas.annotations.Parameter> parametersDocMap) {
439-
LinkedHashMap<ParameterId, Parameter> map = operationParameters.stream().collect(Collectors.toMap(ParameterId::new, parameter -> parameter, (u, v) -> {
437+
private LinkedHashMap<ParameterId, Parameter> getParameterLinkedHashMap(Components components,
438+
MethodAttributes methodAttributes,
439+
List<Parameter> operationParameters,
440+
Map<ParameterId, io.swagger.v3.oas.annotations.Parameter> parametersDocMap) {
441+
LinkedHashMap<ParameterId, Parameter> map = operationParameters.stream()
442+
.collect(Collectors.toMap(ParameterId::new, parameter -> parameter, (u, v) -> {
440443
LOGGER.warn(
441444
"Duplicate OpenAPI parameter detected: name='{}', in='{}'. Keeping the first found and ignoring the rest. " +
442445
"Declare the parameter only once.",
@@ -458,7 +461,8 @@ private LinkedHashMap<ParameterId, Parameter> getParameterLinkedHashMap(Componen
458461
long mumParamsWithName = map.keySet().stream().filter(parameterId1 -> parameterId.getpName().equals(parameterId1.getpName())).count();
459462
long mumParamsDocWithName = parametersDocMap.keySet().stream().filter(parameterId1 -> parameterId.getpName().equals(parameterId1.getpName())).count();
460463
if (mumParamsWithName == 1 && mumParamsDocWithName == 1) {
461-
Optional<ParameterId> parameterIdWithSameNameOptional = map.keySet().stream().filter(parameterId1 -> parameterId.getpName().equals(parameterId1.getpName())).findAny();
464+
Optional<ParameterId> parameterIdWithSameNameOptional = map.keySet().stream()
465+
.filter(parameterId1 -> parameterId.getpName().equals(parameterId1.getpName())).findAny();
462466
parameterIdWithSameNameOptional.ifPresent(parameterIdWithSameName -> {
463467
GenericParameterService.mergeParameter(map.get(parameterIdWithSameName), parameter);
464468
map.put(parameterIdWithSameName, parameter);
@@ -595,7 +599,8 @@ private void setParams(Operation operation, List<Parameter> operationParameters,
595599
* @return the boolean
596600
*/
597601
public boolean isValidParameter(Parameter parameter, MethodAttributes methodAttributes) {
598-
return parameter != null && (parameter.getName() != null || parameter.get$ref() != null) && !(List.of(methodAttributes.getMethodConsumes()).contains(APPLICATION_FORM_URLENCODED_VALUE) && ParameterIn.QUERY.toString().equals(parameter.getIn()));
602+
return parameter != null && (parameter.getName() != null || parameter.get$ref() != null) &&
603+
!(List.of(methodAttributes.getMethodConsumes()).contains(APPLICATION_FORM_URLENCODED_VALUE) && ParameterIn.QUERY.toString().equals(parameter.getIn()));
599604
}
600605

601606
/**
@@ -660,14 +665,7 @@ public Parameter buildParam(ParameterInfo parameterInfo, Components components,
660665
Schema<?> schema = parameterBuilder.calculateSchema(components, parameterInfo, null,
661666
jsonView);
662667
if (parameterInfo.getDefaultValue() != null && schema != null) {
663-
Object defaultValue = parameterInfo.getDefaultValue();
664-
// Cast default value
665-
PrimitiveType primitiveType = PrimitiveType.fromTypeAndFormat(schema.getType(), schema.getFormat());
666-
if (primitiveType != null) {
667-
Schema<?> primitiveSchema = primitiveType.createProperty();
668-
primitiveSchema.setDefault(parameterInfo.getDefaultValue());
669-
defaultValue = primitiveSchema.getDefault();
670-
}
668+
Object defaultValue = SpringDocAnnotationsUtils.castDefaultValue(schema, parameterInfo.getDefaultValue());
671669
schema.setDefault(defaultValue);
672670
}
673671
parameter.setSchema(schema);
@@ -784,18 +782,22 @@ private Map<ParameterId, io.swagger.v3.oas.annotations.Parameter> getApiParamete
784782
Class<?> declaringClass = method.getDeclaringClass();
785783

786784
Set<Parameters> apiParametersDoc = AnnotatedElementUtils.findAllMergedAnnotations(method, Parameters.class);
787-
LinkedHashMap<ParameterId, io.swagger.v3.oas.annotations.Parameter> apiParametersMap = apiParametersDoc.stream().flatMap(x -> Stream.of(x.value())).collect(Collectors.toMap(ParameterId::new, x -> x, (e1, e2) -> e2, LinkedHashMap::new));
785+
LinkedHashMap<ParameterId, io.swagger.v3.oas.annotations.Parameter> apiParametersMap = apiParametersDoc.stream()
786+
.flatMap(x -> Stream.of(x.value())).collect(Collectors.toMap(ParameterId::new, x -> x, (e1, e2) -> e2, LinkedHashMap::new));
788787

789788
Set<Parameters> apiParametersDocDeclaringClass = AnnotatedElementUtils.findAllMergedAnnotations(declaringClass, Parameters.class);
790-
LinkedHashMap<ParameterId, io.swagger.v3.oas.annotations.Parameter> apiParametersDocDeclaringClassMap = apiParametersDocDeclaringClass.stream().flatMap(x -> Stream.of(x.value())).collect(Collectors.toMap(ParameterId::new, x -> x, (e1, e2) -> e2, LinkedHashMap::new));
789+
LinkedHashMap<ParameterId, io.swagger.v3.oas.annotations.Parameter> apiParametersDocDeclaringClassMap = apiParametersDocDeclaringClass.stream()
790+
.flatMap(x -> Stream.of(x.value())).collect(Collectors.toMap(ParameterId::new, x -> x, (e1, e2) -> e2, LinkedHashMap::new));
791791
apiParametersMap.putAll(apiParametersDocDeclaringClassMap);
792792

793793
Set<io.swagger.v3.oas.annotations.Parameter> apiParameterDoc = AnnotatedElementUtils.findAllMergedAnnotations(method, io.swagger.v3.oas.annotations.Parameter.class);
794-
LinkedHashMap<ParameterId, io.swagger.v3.oas.annotations.Parameter> apiParameterDocMap = apiParameterDoc.stream().collect(Collectors.toMap(ParameterId::new, x -> x, (e1, e2) -> e2, LinkedHashMap::new));
794+
LinkedHashMap<ParameterId, io.swagger.v3.oas.annotations.Parameter> apiParameterDocMap = apiParameterDoc.stream()
795+
.collect(Collectors.toMap(ParameterId::new, x -> x, (e1, e2) -> e2, LinkedHashMap::new));
795796
apiParametersMap.putAll(apiParameterDocMap);
796797

797798
Set<io.swagger.v3.oas.annotations.Parameter> apiParameterDocDeclaringClass = AnnotatedElementUtils.findAllMergedAnnotations(declaringClass, io.swagger.v3.oas.annotations.Parameter.class);
798-
LinkedHashMap<ParameterId, io.swagger.v3.oas.annotations.Parameter> apiParameterDocDeclaringClassMap = apiParameterDocDeclaringClass.stream().collect(Collectors.toMap(ParameterId::new, x -> x, (e1, e2) -> e2, LinkedHashMap::new));
799+
LinkedHashMap<ParameterId, io.swagger.v3.oas.annotations.Parameter> apiParameterDocDeclaringClassMap = apiParameterDocDeclaringClass.stream()
800+
.collect(Collectors.toMap(ParameterId::new, x -> x, (e1, e2) -> e2, LinkedHashMap::new));
799801
apiParametersMap.putAll(apiParameterDocDeclaringClassMap);
800802

801803
return apiParametersMap;

springdoc-openapi-starter-common/src/main/java/org/springdoc/core/utils/SpringDocAnnotationsUtils.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.io.IOException;
3030
import java.lang.annotation.Annotation;
3131
import java.lang.reflect.Type;
32+
import java.time.LocalDate;
3233
import java.util.ArrayList;
3334
import java.util.Arrays;
3435
import java.util.Collections;
@@ -50,6 +51,7 @@
5051
import io.swagger.v3.core.converter.ModelConverters;
5152
import io.swagger.v3.core.converter.ResolvedSchema;
5253
import io.swagger.v3.core.util.AnnotationsUtils;
54+
import io.swagger.v3.core.util.PrimitiveType;
5355
import io.swagger.v3.oas.annotations.Hidden;
5456
import io.swagger.v3.oas.annotations.media.Encoding;
5557
import io.swagger.v3.oas.annotations.media.ExampleObject;
@@ -477,6 +479,31 @@ private static boolean isArray(io.swagger.v3.oas.annotations.media.Content annot
477479
return isArray;
478480
}
479481

482+
/**
483+
* Attempt to cast the default value so that it matches the {@link Schema} type.
484+
* If the value cannot be cast then the provided default value is returned as-is.
485+
*
486+
* @param schema the schema that will have the default value
487+
* @param defaultValue the default value
488+
* @return the cast default value
489+
*/
490+
public static Object castDefaultValue(Schema schema, Object defaultValue) {
491+
if (schema != null && defaultValue != null) {
492+
PrimitiveType primitiveType = PrimitiveType.fromTypeAndFormat(schema.getType(), schema.getFormat());
493+
if (primitiveType != null) {
494+
Schema<?> primitiveSchema = primitiveType.createProperty();
495+
if (primitiveType.equals(PrimitiveType.DATE) && defaultValue instanceof LocalDate localDate) {
496+
defaultValue = localDate.toString();
497+
}
498+
primitiveSchema.setDefault(defaultValue);
499+
if (primitiveSchema.getDefault() != null) {
500+
defaultValue = primitiveSchema.getDefault();
501+
}
502+
}
503+
}
504+
return defaultValue;
505+
}
506+
480507
/**
481508
* Resolve default value object.
482509
*

springdoc-openapi-starter-webmvc-api/src/test/resources/results/3.0.1/app150.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424
"required": false,
2525
"schema": {
2626
"type": "string",
27-
"format": "date"
27+
"format": "date",
28+
"default" : "2021-03-08"
2829
}
2930
}
3031
],

springdoc-openapi-tests/springdoc-openapi-javadoc-tests/src/test/resources/results/3.0.1/app150.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@
121121
"required": false,
122122
"schema": {
123123
"type": "string",
124-
"format": "date"
124+
"format": "date",
125+
"default": "2021-03-08"
125126
}
126127
}
127128
],

0 commit comments

Comments
 (0)