Skip to content

Commit 3ed59cd

Browse files
authored
[spring] reactive: fix Content-Type (#16228)
* don't set content-type to client's value * revert manual sample change * better fix * generate samples * cover 3rd case * add new test endpoint
1 parent 3d064c6 commit 3ed59cd

File tree

38 files changed

+774
-20
lines changed

38 files changed

+774
-20
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class ExampleGenerator {
3838

3939
private static final String EXAMPLE = "example";
4040
private static final String CONTENT_TYPE = "contentType";
41+
private static final String GENERATED_CONTENT_TYPE = "generatedContentType";
4142
private static final String OUTPUT = "output";
4243
private static final String NONE = "none";
4344
private static final String URL = "url";
@@ -112,12 +113,14 @@ public List<Map<String, String>> generate(Map<String, Object> examples, List<Str
112113
String example = Json.pretty(resolvePropertyToExample("", mediaType, property, processedModels));
113114
if (example != null) {
114115
kv.put(EXAMPLE, example);
116+
kv.put(GENERATED_CONTENT_TYPE, MIME_TYPE_JSON);
115117
output.add(kv);
116118
}
117119
} else if (property != null && mediaType.startsWith(MIME_TYPE_XML)) {
118120
String example = new XmlExampleGenerator(this.examples).toXml(property);
119121
if (example != null) {
120122
kv.put(EXAMPLE, example);
123+
kv.put(GENERATED_CONTENT_TYPE, MIME_TYPE_XML);
121124
output.add(kv);
122125
}
123126
}
@@ -157,6 +160,7 @@ public List<Map<String, String>> generate(Map<String, Object> examples, List<Str
157160

158161
if (example != null) {
159162
kv.put(EXAMPLE, example);
163+
kv.put(GENERATED_CONTENT_TYPE, MIME_TYPE_JSON);
160164
output.add(kv);
161165
}
162166
}
@@ -165,6 +169,7 @@ public List<Map<String, String>> generate(Map<String, Object> examples, List<Str
165169
String example = new XmlExampleGenerator(this.examples).toXml(schema, 0, Collections.emptySet());
166170
if (example != null) {
167171
kv.put(EXAMPLE, example);
172+
kv.put(GENERATED_CONTENT_TYPE, MIME_TYPE_XML);
168173
output.add(kv);
169174
}
170175
} else {
@@ -201,6 +206,7 @@ private List<Map<String, String>> generate(Object example, List<String> mediaTyp
201206
kv.put(CONTENT_TYPE, mediaType);
202207
if ((mediaType.startsWith(MIME_TYPE_JSON) || mediaType.contains("*/*"))) {
203208
kv.put(EXAMPLE, Json.pretty(example));
209+
kv.put(GENERATED_CONTENT_TYPE, MIME_TYPE_JSON);
204210
output.add(kv);
205211
} else if (mediaType.startsWith(MIME_TYPE_XML)) {
206212
// TODO

modules/openapi-generator/src/main/resources/JavaSpring/methodBody.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Mono<Void> result = Mono.empty();
3939
{{/-first}}
4040
if (mediaType.isCompatibleWith(MediaType.valueOf("{{{contentType}}}"))) {
4141
String exampleString = {{>exampleString}};
42-
result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString);
42+
result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("{{{generatedContentType}}}"), exampleString);
4343
break;
4444
}
4545
{{#-last}}

modules/openapi-generator/src/test/resources/3_0/spring/petstore-with-fake-endpoints-models-for-testing.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,20 @@ paths:
10081008
responses:
10091009
"200":
10101010
description: Success
1011+
/fake/response-with-example:
1012+
get:
1013+
tags:
1014+
- fake
1015+
description: This endpoint defines an example value for its response schema.
1016+
operationId: testWithResultExample
1017+
responses:
1018+
"200":
1019+
content:
1020+
application/json:
1021+
schema:
1022+
example: 42
1023+
type: integer
1024+
description: Success
10111025
/fake/test-query-parameters:
10121026
put:
10131027
tags:

samples/client/petstore/spring-http-interface-reactive/src/main/java/org/openapitools/api/FakeApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,20 @@ Mono<ResponseEntity<Void>> testQueryParameterCollectionFormat(
343343
@RequestParam(value = "context", required = true) List<String> context
344344
);
345345

346+
347+
/**
348+
* GET /fake/response-with-example
349+
* This endpoint defines an example value for its response schema.
350+
*
351+
* @return Success (status code 200)
352+
*/
353+
@HttpExchange(
354+
method = "GET",
355+
value = "/fake/response-with-example",
356+
accept = "application/json"
357+
)
358+
Mono<ResponseEntity<Integer>> testWithResultExample(
359+
360+
);
361+
346362
}

samples/client/petstore/spring-http-interface/src/main/java/org/openapitools/api/FakeApi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,4 +339,20 @@ ResponseEntity<Void> testQueryParameterCollectionFormat(
339339
@RequestParam(value = "context", required = true) List<String> context
340340
);
341341

342+
343+
/**
344+
* GET /fake/response-with-example
345+
* This endpoint defines an example value for its response schema.
346+
*
347+
* @return Success (status code 200)
348+
*/
349+
@HttpExchange(
350+
method = "GET",
351+
value = "/fake/response-with-example",
352+
accept = "application/json"
353+
)
354+
ResponseEntity<Integer> testWithResultExample(
355+
356+
);
357+
342358
}

samples/openapi3/client/petstore/spring-cloud-oas3-fakeapi/src/main/java/org/openapitools/api/FakeApi.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,4 +480,30 @@ ResponseEntity<Void> testQueryParameterCollectionFormat(
480480
@NotNull @Parameter(name = "context", description = "", required = true, in = ParameterIn.QUERY) @Valid @RequestParam(value = "context", required = true) List<String> context
481481
);
482482

483+
484+
/**
485+
* GET /fake/response-with-example
486+
* This endpoint defines an example value for its response schema.
487+
*
488+
* @return Success (status code 200)
489+
*/
490+
@Operation(
491+
operationId = "testWithResultExample",
492+
description = "This endpoint defines an example value for its response schema.",
493+
tags = { "fake" },
494+
responses = {
495+
@ApiResponse(responseCode = "200", description = "Success", content = {
496+
@Content(mediaType = "application/json", schema = @Schema(implementation = Integer.class))
497+
})
498+
}
499+
)
500+
@RequestMapping(
501+
method = RequestMethod.GET,
502+
value = "/fake/response-with-example",
503+
produces = "application/json"
504+
)
505+
ResponseEntity<Integer> testWithResultExample(
506+
507+
);
508+
483509
}

samples/openapi3/server/petstore/springboot-delegate/src/main/java/org/openapitools/api/FakeApi.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,34 @@ default ResponseEntity<Void> testQueryParameterCollectionFormat(
538538
}
539539

540540

541+
/**
542+
* GET /fake/response-with-example
543+
* This endpoint defines an example value for its response schema.
544+
*
545+
* @return Success (status code 200)
546+
*/
547+
@Operation(
548+
operationId = "testWithResultExample",
549+
description = "This endpoint defines an example value for its response schema.",
550+
tags = { "fake" },
551+
responses = {
552+
@ApiResponse(responseCode = "200", description = "Success", content = {
553+
@Content(mediaType = "application/json", schema = @Schema(implementation = Integer.class))
554+
})
555+
}
556+
)
557+
@RequestMapping(
558+
method = RequestMethod.GET,
559+
value = "/fake/response-with-example",
560+
produces = { "application/json" }
561+
)
562+
default ResponseEntity<Integer> testWithResultExample(
563+
564+
) {
565+
return getDelegate().testWithResultExample();
566+
}
567+
568+
541569
/**
542570
* POST /fake/{petId}/uploadImageWithRequiredFile : uploads an image (required)
543571
*

samples/openapi3/server/petstore/springboot-delegate/src/main/java/org/openapitools/api/FakeApiDelegate.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,27 @@ default ResponseEntity<Void> testQueryParameterCollectionFormat(List<String> pip
318318

319319
}
320320

321+
/**
322+
* GET /fake/response-with-example
323+
* This endpoint defines an example value for its response schema.
324+
*
325+
* @return Success (status code 200)
326+
* @see FakeApi#testWithResultExample
327+
*/
328+
default ResponseEntity<Integer> testWithResultExample() {
329+
getRequest().ifPresent(request -> {
330+
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
331+
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
332+
String exampleString = "42";
333+
ApiUtil.setExampleResponse(request, "application/json", exampleString);
334+
break;
335+
}
336+
}
337+
});
338+
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
339+
340+
}
341+
321342
/**
322343
* POST /fake/{petId}/uploadImageWithRequiredFile : uploads an image (required)
323344
*

samples/openapi3/server/petstore/springboot-delegate/src/main/resources/openapi.yaml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1088,6 +1088,23 @@ paths:
10881088
x-accepts: application/json
10891089
x-tags:
10901090
- tag: fake
1091+
/fake/response-with-example:
1092+
get:
1093+
description: This endpoint defines an example value for its response schema.
1094+
operationId: testWithResultExample
1095+
responses:
1096+
"200":
1097+
content:
1098+
application/json:
1099+
schema:
1100+
example: 42
1101+
type: integer
1102+
description: Success
1103+
tags:
1104+
- fake
1105+
x-accepts: application/json
1106+
x-tags:
1107+
- tag: fake
10911108
/fake/test-query-parameters:
10921109
put:
10931110
description: To test the collection format in query parameters

samples/openapi3/server/petstore/springboot-implicitHeaders/src/main/java/org/openapitools/api/FakeApi.java

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,44 @@ default ResponseEntity<Void> testQueryParameterCollectionFormat(
584584
}
585585

586586

587+
/**
588+
* GET /fake/response-with-example
589+
* This endpoint defines an example value for its response schema.
590+
*
591+
* @return Success (status code 200)
592+
*/
593+
@Operation(
594+
operationId = "testWithResultExample",
595+
description = "This endpoint defines an example value for its response schema.",
596+
tags = { "fake" },
597+
responses = {
598+
@ApiResponse(responseCode = "200", description = "Success", content = {
599+
@Content(mediaType = "application/json", schema = @Schema(implementation = Integer.class))
600+
})
601+
}
602+
)
603+
@RequestMapping(
604+
method = RequestMethod.GET,
605+
value = "/fake/response-with-example",
606+
produces = { "application/json" }
607+
)
608+
default ResponseEntity<Integer> testWithResultExample(
609+
610+
) {
611+
getRequest().ifPresent(request -> {
612+
for (MediaType mediaType: MediaType.parseMediaTypes(request.getHeader("Accept"))) {
613+
if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) {
614+
String exampleString = "42";
615+
ApiUtil.setExampleResponse(request, "application/json", exampleString);
616+
break;
617+
}
618+
}
619+
});
620+
return new ResponseEntity<>(HttpStatus.NOT_IMPLEMENTED);
621+
622+
}
623+
624+
587625
/**
588626
* POST /fake/{petId}/uploadImageWithRequiredFile : uploads an image (required)
589627
*

0 commit comments

Comments
 (0)