Skip to content

Commit 99abf29

Browse files
committed
Changes report: Schema properties are ignored in method-level @RequestBody #1664
1 parent 8e6753b commit 99abf29

File tree

3 files changed

+93
-0
lines changed

3 files changed

+93
-0
lines changed

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

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

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@
2525
package test.org.springdoc.api.v30.app83;
2626

2727
import io.swagger.v3.oas.annotations.Parameter;
28+
import io.swagger.v3.oas.annotations.media.Content;
2829
import io.swagger.v3.oas.annotations.media.Schema;
30+
import io.swagger.v3.oas.annotations.media.SchemaProperty;
31+
import io.swagger.v3.oas.annotations.parameters.RequestBody;
2932

3033
import org.springframework.http.MediaType;
3134
import org.springframework.http.ResponseEntity;
@@ -52,4 +55,15 @@ public ResponseEntity<?> put(
5255
return null;
5356
}
5457

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

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

Lines changed: 38 additions & 0 deletions
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)