Skip to content

Commit 06b297a

Browse files
committed
@Schema(types = "xxx") does not work for multipart param with enabled springdoc.default-support-form-data config option. Fixes #2852
1 parent 4bff24b commit 06b297a

File tree

3 files changed

+33
-21
lines changed

3 files changed

+33
-21
lines changed

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

+25-17
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
* * * *
2222
* * *
2323
* *
24-
*
24+
*
2525
*/
2626

2727
package org.springdoc.core.service;
@@ -333,7 +333,7 @@ public Operation build(HandlerMethod handlerMethod, RequestMethod requestMethod,
333333
if (!isParamToIgnore(methodParameter)) {
334334
parameter = buildParams(parameterInfo, components, requestMethod, methodAttributes, openAPI.getOpenapi());
335335
List<Annotation> parameterAnnotations = List.of(getParameterAnnotations(methodParameter));
336-
if (isValidParameter(parameter,methodAttributes)) {
336+
if (isValidParameter(parameter, methodAttributes)) {
337337
// Merge with the operation parameters
338338
parameter = GenericParameterService.mergeParameter(operationParameters, parameter);
339339
// Add param javadoc
@@ -368,12 +368,20 @@ else if (!RequestMethod.GET.equals(requestMethod) || OpenApiVersion.OPENAPI_3_1.
368368
Parameter parameter = entry.getValue();
369369
if (!ParameterIn.PATH.toString().equals(parameter.getIn()) && !ParameterIn.HEADER.toString().equals(parameter.getIn())
370370
&& !ParameterIn.COOKIE.toString().equals(parameter.getIn())) {
371-
Schema<?> itemSchema = new Schema<>();
372-
itemSchema.setName(entry.getKey().getpName());
373-
itemSchema.setDescription(parameter.getDescription());
374-
itemSchema.setDeprecated(parameter.getDeprecated());
371+
Schema<?> itemSchema;
372+
if (parameter.getSchema() != null) {
373+
itemSchema = parameter.getSchema();
374+
}
375+
else {
376+
itemSchema = new Schema<>();
377+
itemSchema.setName(entry.getKey().getpName());
378+
}
379+
if (StringUtils.isNotEmpty(parameter.getDescription()))
380+
itemSchema.setDescription(parameter.getDescription());
375381
if (parameter.getExample() != null)
376382
itemSchema.setExample(parameter.getExample());
383+
if (parameter.getDeprecated() != null)
384+
itemSchema.setDeprecated(parameter.getDeprecated());
377385
requestBodyInfo.addProperties(entry.getKey().getpName(), itemSchema);
378386
it.remove();
379387
}
@@ -525,7 +533,7 @@ private void setParams(Operation operation, List<Parameter> operationParameters,
525533
* @param methodAttributes the method attributes
526534
* @return the boolean
527535
*/
528-
public boolean isValidParameter(Parameter parameter, MethodAttributes methodAttributes ) {
536+
public boolean isValidParameter(Parameter parameter, MethodAttributes methodAttributes) {
529537
return parameter != null && (parameter.getName() != null || parameter.get$ref() != null) && !(Arrays.asList(methodAttributes.getMethodConsumes()).contains(APPLICATION_FORM_URLENCODED_VALUE) && ParameterIn.QUERY.toString().equals(parameter.getIn()));
530538
}
531539

@@ -842,14 +850,14 @@ else if (requestBody.content().length > 0)
842850
return false;
843851
}
844852

845-
/**
846-
* Check if the parameter has any of the annotations that make it non-optional
847-
*
848-
* @param annotationSimpleNames the annotation simple class named, e.g. NotNull
849-
* @return whether any of the known NotNull annotations are present
850-
*/
851-
public static boolean hasNotNullAnnotation(Collection<String> annotationSimpleNames) {
852-
return Arrays.stream(ANNOTATIONS_FOR_REQUIRED).anyMatch(annotationSimpleNames::contains);
853-
}
854-
853+
/**
854+
* Check if the parameter has any of the annotations that make it non-optional
855+
*
856+
* @param annotationSimpleNames the annotation simple class named, e.g. NotNull
857+
* @return whether any of the known NotNull annotations are present
858+
*/
859+
public static boolean hasNotNullAnnotation(Collection<String> annotationSimpleNames) {
860+
return Arrays.stream(ANNOTATIONS_FOR_REQUIRED).anyMatch(annotationSimpleNames::contains);
861+
}
862+
855863
}

Diff for: springdoc-openapi-starter-webmvc-api/src/test/resources/results/3.0.1/app189.json

+2
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,11 @@
3131
"format": "binary"
3232
},
3333
"title": {
34+
"type": "string",
3435
"description": "title"
3536
},
3637
"content": {
38+
"type": "string",
3739
"description": "content"
3840
}
3941
}

Diff for: springdoc-openapi-starter-webmvc-api/src/test/resources/results/3.1.0/app189.json

+6-4
Original file line numberDiff line numberDiff line change
@@ -21,22 +21,24 @@
2121
"content": {
2222
"multipart/form-data": {
2323
"schema": {
24-
"required": [
25-
"file"
26-
],
2724
"type": "object",
2825
"properties": {
2926
"file": {
3027
"type": "string",
3128
"format": "binary"
3229
},
3330
"title": {
31+
"type": "string",
3432
"description": "title"
3533
},
3634
"content": {
35+
"type": "string",
3736
"description": "content"
3837
}
39-
}
38+
},
39+
"required": [
40+
"file"
41+
]
4042
}
4143
}
4244
}

0 commit comments

Comments
 (0)