Skip to content

Commit d7e4a1b

Browse files
committed
Schema properties are ignored in method-level @RequestBody. Fixes springdoc#1664.
1 parent cd634fd commit d7e4a1b

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

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

+41
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import io.swagger.v3.core.util.AnnotationsUtils;
4343
import io.swagger.v3.oas.annotations.Hidden;
4444
import io.swagger.v3.oas.annotations.media.ExampleObject;
45+
import io.swagger.v3.oas.annotations.media.SchemaProperty;
4546
import io.swagger.v3.oas.models.Components;
4647
import io.swagger.v3.oas.models.media.ArraySchema;
4748
import io.swagger.v3.oas.models.media.ComposedSchema;
@@ -347,6 +348,46 @@ private static MediaType getMediaType(Schema schema, Components components, Json
347348
if (components != null) {
348349
try {
349350
getSchema(annotationContent, components, jsonViewAnnotation).ifPresent(mediaType::setSchema);
351+
if (annotationContent.schemaProperties().length > 0) {
352+
if (mediaType.getSchema() == null) {
353+
mediaType.schema(new Schema<Object>().type("object"));
354+
}
355+
Schema oSchema = mediaType.getSchema();
356+
for (SchemaProperty sp: annotationContent.schemaProperties()) {
357+
Class<?> schemaImplementation = sp.schema().implementation();
358+
boolean isArray = false;
359+
if (schemaImplementation == Void.class) {
360+
schemaImplementation = sp.array().schema().implementation();
361+
if (schemaImplementation != Void.class) {
362+
isArray = true;
363+
}
364+
}
365+
getSchema(sp.schema(), sp.array(), isArray, schemaImplementation, components, jsonViewAnnotation)
366+
.ifPresent(s -> {
367+
if ("array".equals(oSchema.getType())) {
368+
oSchema.getItems().addProperty(sp.name(), s);
369+
} else {
370+
oSchema.addProperty(sp.name(), s);
371+
}
372+
});
373+
374+
}
375+
}
376+
if (
377+
hasSchemaAnnotation(annotationContent.additionalPropertiesSchema()) &&
378+
mediaType.getSchema() != null &&
379+
!Boolean.TRUE.equals(mediaType.getSchema().getAdditionalProperties()) &&
380+
!Boolean.FALSE.equals(mediaType.getSchema().getAdditionalProperties())) {
381+
getSchemaFromAnnotation(annotationContent.additionalPropertiesSchema(), components, jsonViewAnnotation)
382+
.ifPresent(s -> {
383+
if ("array".equals(mediaType.getSchema().getType())) {
384+
mediaType.getSchema().getItems().additionalProperties(s);
385+
} else {
386+
mediaType.getSchema().additionalProperties(s);
387+
}
388+
}
389+
);
390+
}
350391
}
351392
catch (Exception e) {
352393
if (isArray(annotationContent))

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

+14
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323
package test.org.springdoc.api.v30.app83;
2424

2525
import io.swagger.v3.oas.annotations.Parameter;
26+
import io.swagger.v3.oas.annotations.media.Content;
2627
import io.swagger.v3.oas.annotations.media.Schema;
28+
import io.swagger.v3.oas.annotations.media.SchemaProperty;
29+
import io.swagger.v3.oas.annotations.parameters.RequestBody;
2730

2831
import org.springframework.http.MediaType;
2932
import org.springframework.http.ResponseEntity;
@@ -50,4 +53,15 @@ public ResponseEntity<?> put(
5053
return null;
5154
}
5255

56+
@RequestMapping(value = "/test",
57+
method = RequestMethod.PUT,
58+
consumes = { MediaType.MULTIPART_FORM_DATA_VALUE },
59+
produces = { MediaType.APPLICATION_JSON_VALUE }
60+
)
61+
@RequestBody(content =@Content(schema = @Schema( requiredProperties = "file", type = "object")
62+
, schemaProperties = @SchemaProperty(name = "file", schema = @Schema(type = "string", format = "binary"))
63+
))
64+
public ResponseEntity<?> put2() {
65+
return null;
66+
}
5367
}

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

+38
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,44 @@
6363
}
6464
}
6565
}
66+
},
67+
"/test": {
68+
"put": {
69+
"tags": [
70+
"hello-controller"
71+
],
72+
"operationId": "put2",
73+
"requestBody": {
74+
"content": {
75+
"multipart/form-data": {
76+
"schema": {
77+
"required": [
78+
"file"
79+
],
80+
"type": "object",
81+
"properties": {
82+
"file": {
83+
"type": "string",
84+
"format": "binary"
85+
}
86+
}
87+
}
88+
}
89+
}
90+
},
91+
"responses": {
92+
"200": {
93+
"description": "OK",
94+
"content": {
95+
"application/json": {
96+
"schema": {
97+
"type": "object"
98+
}
99+
}
100+
}
101+
}
102+
}
103+
}
66104
}
67105
},
68106
"components": {}

0 commit comments

Comments
 (0)