Skip to content

Commit 8be775e

Browse files
author
bnasslahsen
committed
Unable to generate correct definition for request parameter containing JSON. Fixes #791
1 parent 767ef5c commit 8be775e

File tree

5 files changed

+49
-38
lines changed

5 files changed

+49
-38
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ private Parameter buildParam(String in, Components components, ParameterInfo par
493493
if (containsDeprecatedAnnotation(parameterInfo.getMethodParameter().getParameterAnnotations()))
494494
parameter.setDeprecated(true);
495495

496-
if (parameter.getSchema() == null) {
496+
if (parameter.getSchema() == null && parameter.getContent() == null) {
497497
Schema<?> schema = parameterBuilder.calculateSchema(components, parameterInfo, null,
498498
jsonView);
499499
if (defaultValue != null)

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import io.swagger.v3.oas.models.Components;
4141
import io.swagger.v3.oas.models.examples.Example;
4242
import io.swagger.v3.oas.models.media.ArraySchema;
43+
import io.swagger.v3.oas.models.media.Content;
4344
import io.swagger.v3.oas.models.media.FileSchema;
4445
import io.swagger.v3.oas.models.media.ObjectSchema;
4546
import io.swagger.v3.oas.models.media.Schema;
@@ -190,7 +191,7 @@ private static void mergeParameter(Parameter paramCalcul, Parameter paramDoc) {
190191
if (StringUtils.isBlank(paramDoc.get$ref()))
191192
paramDoc.set$ref(paramDoc.get$ref());
192193

193-
if (paramDoc.getSchema() == null)
194+
if (paramDoc.getSchema() == null && paramDoc.getContent() == null)
194195
paramDoc.setSchema(paramCalcul.getSchema());
195196

196197
if (paramDoc.getExamples() == null)
@@ -240,7 +241,13 @@ public Parameter buildParameterFromDoc(io.swagger.v3.oas.annotations.Parameter p
240241
if (parameterDoc.allowReserved())
241242
parameter.setAllowReserved(parameterDoc.allowReserved());
242243

243-
setSchema(parameterDoc, components, jsonView, parameter);
244+
if (parameterDoc.content().length > 0) {
245+
Optional<Content> optionalContent = AnnotationsUtils.getContent(parameterDoc.content(), null, null, null, components, jsonView);
246+
optionalContent.ifPresent(parameter::setContent);
247+
}
248+
else
249+
setSchema(parameterDoc, components, jsonView, parameter);
250+
244251
setExamples(parameterDoc, parameter);
245252
setExtensions(parameterDoc, parameter);
246253
setParameterStyle(parameter, parameterDoc);
@@ -269,10 +276,7 @@ private void setSchema(io.swagger.v3.oas.annotations.Parameter parameterDoc, Com
269276
LOGGER.warn(Constants.GRACEFUL_EXCEPTION_OCCURRED, e);
270277
}
271278
if (schema == null) {
272-
if (parameterDoc.content().length > 0)
273-
schema = AnnotationsUtils.getSchema(parameterDoc.content()[0], components, jsonView).orElse(null);
274-
else
275-
schema = AnnotationsUtils.getArraySchema(parameterDoc.array(), components, jsonView).orElse(null);
279+
schema = AnnotationsUtils.getArraySchema(parameterDoc.array(), components, jsonView).orElse(null);
276280
}
277281
parameter.setSchema(schema);
278282
}

springdoc-openapi-common/src/main/java/org/springdoc/core/converters/models/PageableAsQueryParam.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import io.swagger.v3.oas.annotations.Parameter;
2929
import io.swagger.v3.oas.annotations.enums.ParameterIn;
3030
import io.swagger.v3.oas.annotations.media.ArraySchema;
31-
import io.swagger.v3.oas.annotations.media.Content;
3231
import io.swagger.v3.oas.annotations.media.Schema;
3332

3433
/**
@@ -40,16 +39,16 @@
4039
@Parameter(in = ParameterIn.QUERY
4140
, description = "Zero-based page index (0..N)"
4241
, name = "page"
43-
, content = @Content(schema = @Schema(type = "integer", defaultValue = "0")))
42+
, schema = @Schema(type = "integer", defaultValue = "0"))
4443
@Parameter(in = ParameterIn.QUERY
4544
, description = "The size of the page to be returned"
4645
, name = "size"
47-
, content = @Content(schema = @Schema(type = "integer", defaultValue = "20")))
46+
, schema = @Schema(type = "integer", defaultValue = "20"))
4847
@Parameter(in = ParameterIn.QUERY
4948
, description = "Sorting criteria in the format: property(,asc|desc). "
5049
+ "Default sort order is ascending. " + "Multiple sort criteria are supported."
5150
, name = "sort"
52-
, content = @Content(array = @ArraySchema(schema = @Schema(type = "string"))))
51+
, array = @ArraySchema(schema = @Schema(type = "string")))
5352
public @interface
5453
PageableAsQueryParam {
5554

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

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,27 @@
2424
}
2525
}
2626
},
27+
"/examplePost": {
28+
"post": {
29+
"tags": [
30+
"hello-controller"
31+
],
32+
"summary": "schema example",
33+
"operationId": "example",
34+
"responses": {
35+
"200": {
36+
"description": "OK",
37+
"content": {
38+
"*/*": {
39+
"schema": {
40+
"type": "object"
41+
}
42+
}
43+
}
44+
}
45+
}
46+
}
47+
},
2748
"/foo": {
2849
"get": {
2950
"tags": [
@@ -60,8 +81,12 @@
6081
"in": "query",
6182
"description": "User",
6283
"required": true,
63-
"schema": {
64-
"$ref": "#/components/schemas/PersonDTO"
84+
"content": {
85+
"application/json": {
86+
"schema": {
87+
"$ref": "#/components/schemas/PersonDTO"
88+
}
89+
}
6590
}
6691
}
6792
],
@@ -71,27 +96,6 @@
7196
}
7297
}
7398
}
74-
},
75-
"/examplePost": {
76-
"post": {
77-
"tags": [
78-
"hello-controller"
79-
],
80-
"summary": "schema example",
81-
"operationId": "example",
82-
"responses": {
83-
"200": {
84-
"description": "OK",
85-
"content": {
86-
"*/*": {
87-
"schema": {
88-
"type": "object"
89-
}
90-
}
91-
}
92-
}
93-
}
94-
}
9599
}
96100
},
97101
"components": {

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,14 @@
4242
"in": "query",
4343
"description": "createdFrom",
4444
"required": true,
45-
"schema": {
46-
"type": "array",
47-
"items": {
48-
"type": "string"
45+
"content": {
46+
"*/*": {
47+
"schema": {
48+
"type": "array",
49+
"items": {
50+
"type": "string"
51+
}
52+
}
4953
}
5054
}
5155
},

0 commit comments

Comments
 (0)