From 6fcefd58292d3290ace9c65ceb986fa8afd18a1d Mon Sep 17 00:00:00 2001 From: Rodrigo Maciel de Almeida Date: Fri, 4 Aug 2023 11:02:05 -0300 Subject: [PATCH 1/3] fix #12219 #15822 --- docs/generators/java-camel.md | 1 + docs/generators/spring.md | 1 + .../openapitools/codegen/VendorExtension.java | 1 + .../codegen/languages/SpringCodegen.java | 1 + .../main/resources/JavaSpring/api.mustache | 3 + .../java/spring/SpringCodegenTest.java | 68 +++++++++++++++++++ .../src/test/resources/2_0/issue12219.yaml | 53 +++++++++++++++ .../src/test/resources/2_0/issue15822.yaml | 53 +++++++++++++++ 8 files changed, 181 insertions(+) create mode 100644 modules/openapi-generator/src/test/resources/2_0/issue12219.yaml create mode 100644 modules/openapi-generator/src/test/resources/2_0/issue15822.yaml diff --git a/docs/generators/java-camel.md b/docs/generators/java-camel.md index 850f5893ddbe..2f268bccc1bb 100644 --- a/docs/generators/java-camel.md +++ b/docs/generators/java-camel.md @@ -119,6 +119,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |x-content-type|Specify custom value for 'Content-Type' header for operation|OPERATION|null |x-class-extra-annotation|List of custom annotations to be added to model|MODEL|null |x-field-extra-annotation|List of custom annotations to be added to property|FIELD|null +|x-operation-extra-annotation|List of custom annotations to be added to operation|OPERATION|null |x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false |x-version-param|Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false|OPERATION_PARAMETER|null |x-pattern-message|Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable|FIELD|null diff --git a/docs/generators/spring.md b/docs/generators/spring.md index 56213442d50c..fd35b96fb2dc 100644 --- a/docs/generators/spring.md +++ b/docs/generators/spring.md @@ -112,6 +112,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl |x-content-type|Specify custom value for 'Content-Type' header for operation|OPERATION|null |x-class-extra-annotation|List of custom annotations to be added to model|MODEL|null |x-field-extra-annotation|List of custom annotations to be added to property|FIELD|null +|x-operation-extra-annotation|List of custom annotations to be added to operation|OPERATION|null |x-spring-paginated|Add org.springframework.data.domain.Pageable to controller method. Can be used to handle page & size query parameters|OPERATION|false |x-version-param|Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false|OPERATION_PARAMETER|null |x-pattern-message|Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable|FIELD|null diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/VendorExtension.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/VendorExtension.java index 77b1b834166f..8d2361d0a067 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/VendorExtension.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/VendorExtension.java @@ -15,6 +15,7 @@ public enum VendorExtension { X_CONTENT_TYPE("x-content-type", ExtensionLevel.OPERATION, "Specify custom value for 'Content-Type' header for operation", null), X_CLASS_EXTRA_ANNOTATION("x-class-extra-annotation", ExtensionLevel.MODEL, "List of custom annotations to be added to model", null), X_FIELD_EXTRA_ANNOTATION("x-field-extra-annotation", ExtensionLevel.FIELD, "List of custom annotations to be added to property", null), + X_OPERATION_EXTRA_ANNOTATION("x-operation-extra-annotation", ExtensionLevel.OPERATION, "List of custom annotations to be added to operation", null), X_VERSION_PARAM("x-version-param", ExtensionLevel.OPERATION_PARAMETER, "Marker property that tells that this parameter would be used for endpoint versioning. Applicable for headers & query params. true/false", null), X_PATTERN_MESSAGE("x-pattern-message", ExtensionLevel.FIELD, "Add this property whenever you need to customize the invalidation error message for the regex pattern of a variable", null), ; diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java index 6db6ad2903ed..b2ae0ecd991d 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/SpringCodegen.java @@ -1304,6 +1304,7 @@ public void setUseSwaggerUI(boolean useSwaggerUI) { @Override public List getSupportedVendorExtensions() { List extensions = super.getSupportedVendorExtensions(); + extensions.add(VendorExtension.X_OPERATION_EXTRA_ANNOTATION); extensions.add(VendorExtension.X_SPRING_PAGINATED); extensions.add(VendorExtension.X_VERSION_PARAM); extensions.add(VendorExtension.X_PATTERN_MESSAGE); diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache index e02b4ba6b4bb..12c689c6fd39 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache @@ -240,6 +240,9 @@ public interface {{classname}} { {{^useResponseEntity}} @ResponseStatus({{#springHttpStatus}}{{#responses.0}}{{{code}}}{{/responses.0}}{{/springHttpStatus}}) {{/useResponseEntity}} + {{#vendorExtensions.x-operation-extra-annotation}} + {{{vendorExtensions.x-operation-extra-annotation}}} + {{/vendorExtensions.x-operation-extra-annotation}} {{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}{{#useResponseEntity}}ResponseEntity<{{/useResponseEntity}}{{>returnTypes}}{{#useResponseEntity}}>{{/useResponseEntity}}{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}( {{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}}, {{/-last}}{{/allParams}}{{#reactive}}{{#hasParams}}, diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java index 1c3313381ea8..4554bb83ed43 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java @@ -2899,6 +2899,74 @@ public void testEnumCaseSensitive_issue8084() throws IOException { } + @Test + public void testHasOperationExtraAnnotation_issue15822() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/issue15822.yaml"); + final SpringCodegen codegen = new SpringCodegen(); + codegen.setOpenAPI(openAPI); + codegen.setOutputDir(output.getAbsolutePath()); + + codegen.additionalProperties().put(SpringCodegen.DATE_LIBRARY, "java8-localdatetime"); + codegen.additionalProperties().put(INTERFACE_ONLY, "true"); + codegen.additionalProperties().put(USE_RESPONSE_ENTITY, "false"); + codegen.additionalProperties().put(DELEGATE_PATTERN, "true"); + codegen.additionalProperties().put(REQUEST_MAPPING_OPTION, "api_interface"); + codegen.additionalProperties().put(SPRING_CONTROLLER, "true"); + + ClientOptInput input = new ClientOptInput(); + input.openAPI(openAPI); + input.config(codegen); + + DefaultGenerator generator = new DefaultGenerator(); + + Map files = generator.opts(input).generate().stream() + .collect(Collectors.toMap(File::getName, Function.identity())); + + JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("TestApi.java")); + javaFileAssert + .assertMethod("_postToTest") + .assertMethodAnnotations() + .containsWithName("javax.annotation.security.RolesAllowed"); + } + + @Test + public void testHasOperationExtraAnnotation_issue12219() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/issue12219.yaml"); + final SpringCodegen codegen = new SpringCodegen(); + codegen.setOpenAPI(openAPI); + codegen.setOutputDir(output.getAbsolutePath()); + + codegen.additionalProperties().put(SpringCodegen.DATE_LIBRARY, "java8-localdatetime"); + codegen.additionalProperties().put(INTERFACE_ONLY, "true"); + codegen.additionalProperties().put(USE_RESPONSE_ENTITY, "false"); + codegen.additionalProperties().put(DELEGATE_PATTERN, "true"); + codegen.additionalProperties().put(REQUEST_MAPPING_OPTION, "api_interface"); + codegen.additionalProperties().put(SPRING_CONTROLLER, "true"); + + ClientOptInput input = new ClientOptInput(); + input.openAPI(openAPI); + input.config(codegen); + + DefaultGenerator generator = new DefaultGenerator(); + + Map files = generator.opts(input).generate().stream() + .collect(Collectors.toMap(File::getName, Function.identity())); + + JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("TestApi.java")); + javaFileAssert + .assertMethod("_postToTest") + .assertMethodAnnotations() + .containsWithName("javax.annotation.security.RolesAllowed") + .containsWithName("org.springframework.security.access.annotation.Secured") + .containsWithName("org.springframework.security.access.prepost.PreAuthorize"); + } + @Test public void multiLineOperationDescription() throws IOException { diff --git a/modules/openapi-generator/src/test/resources/2_0/issue12219.yaml b/modules/openapi-generator/src/test/resources/2_0/issue12219.yaml new file mode 100644 index 000000000000..5d261286ca85 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/2_0/issue12219.yaml @@ -0,0 +1,53 @@ +swagger: '2.0' +info: + description: 'blah' + version: 1.0.0 + title: sample spec +host: fake.site.com +tags: + - name: Test +schemes: + - https +paths: + /test: + post: + summary: Post to test + description: '' + operationId: postToTest + consumes: + - application/json + produces: + - application/json + parameters: + - in: body + name: Obj to test + schema: + $ref: '#/definitions/ObjTest' + responses: + '201': + description: successful operation + schema: + $ref: '#/definitions/ObjTest' + x-operation-extra-annotation: '@javax.annotation.security.RolesAllowed({"ROLE_TEST"}) @org.springframework.security.access.annotation.Secured({"ROLE_TEST"}) @org.springframework.security.access.prepost.PreAuthorize("hasRole(''ROLE_TEST'') and hasRole(''ROLE_TEST_TWO'')")' +definitions: + ObjTest: + description: A model to return + type: object + properties: + field1: + type: integer + format: int64 + field2: + type: string + pattern: "\\w" + x-pattern-message: "Only letters, numbers and underscore" + field3: + type: string + pattern: "\\w" + EnumTest: + title: An enum to test + type: string + enum: + - ONE + - Two + - three \ No newline at end of file diff --git a/modules/openapi-generator/src/test/resources/2_0/issue15822.yaml b/modules/openapi-generator/src/test/resources/2_0/issue15822.yaml new file mode 100644 index 000000000000..408ee5665c92 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/2_0/issue15822.yaml @@ -0,0 +1,53 @@ +swagger: '2.0' +info: + description: 'blah' + version: 1.0.0 + title: sample spec +host: fake.site.com +tags: + - name: Test +schemes: + - https +paths: + /test: + post: + summary: Post to test + description: '' + operationId: postToTest + consumes: + - application/json + produces: + - application/json + parameters: + - in: body + name: Obj to test + schema: + $ref: '#/definitions/ObjTest' + responses: + '201': + description: successful operation + schema: + $ref: '#/definitions/ObjTest' + x-operation-extra-annotation: '@javax.annotation.security.RolesAllowed({"ROLE_TEST"})' +definitions: + ObjTest: + description: A model to return + type: object + properties: + field1: + type: integer + format: int64 + field2: + type: string + pattern: "\\w" + x-pattern-message: "Only letters, numbers and underscore" + field3: + type: string + pattern: "\\w" + EnumTest: + title: An enum to test + type: string + enum: + - ONE + - Two + - three \ No newline at end of file From f19760ed802d75f95880d6408f103b7ca64e2ee4 Mon Sep 17 00:00:00 2001 From: Rodrigo Maciel de Almeida Date: Tue, 8 Aug 2023 19:19:50 -0300 Subject: [PATCH 2/3] add test with array --- .../main/resources/JavaSpring/api.mustache | 2 +- .../java/spring/SpringCodegenTest.java | 35 ++++++++++++ .../test/resources/2_0/issue12219_array.yaml | 56 +++++++++++++++++++ 3 files changed, 92 insertions(+), 1 deletion(-) create mode 100644 modules/openapi-generator/src/test/resources/2_0/issue12219_array.yaml diff --git a/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache b/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache index 12c689c6fd39..6dea8c3ccd4a 100644 --- a/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache +++ b/modules/openapi-generator/src/main/resources/JavaSpring/api.mustache @@ -241,7 +241,7 @@ public interface {{classname}} { @ResponseStatus({{#springHttpStatus}}{{#responses.0}}{{{code}}}{{/responses.0}}{{/springHttpStatus}}) {{/useResponseEntity}} {{#vendorExtensions.x-operation-extra-annotation}} - {{{vendorExtensions.x-operation-extra-annotation}}} + {{{.}}} {{/vendorExtensions.x-operation-extra-annotation}} {{#jdk8-default-interface}}default {{/jdk8-default-interface}}{{#responseWrapper}}{{.}}<{{/responseWrapper}}{{#useResponseEntity}}ResponseEntity<{{/useResponseEntity}}{{>returnTypes}}{{#useResponseEntity}}>{{/useResponseEntity}}{{#responseWrapper}}>{{/responseWrapper}} {{#delegate-method}}_{{/delegate-method}}{{operationId}}( {{#allParams}}{{>queryParams}}{{>pathParams}}{{>headerParams}}{{>bodyParams}}{{>formParams}}{{>cookieParams}}{{^-last}}, diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java index 4554bb83ed43..5e819f5fd92a 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java @@ -2967,6 +2967,41 @@ public void testHasOperationExtraAnnotation_issue12219() throws IOException { .containsWithName("org.springframework.security.access.prepost.PreAuthorize"); } + @Test + public void testHasOperationExtraAnnotation_issue12219_array() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + + final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/issue12219_array.yaml"); + final SpringCodegen codegen = new SpringCodegen(); + codegen.setOpenAPI(openAPI); + codegen.setOutputDir(output.getAbsolutePath()); + + codegen.additionalProperties().put(SpringCodegen.DATE_LIBRARY, "java8-localdatetime"); + codegen.additionalProperties().put(INTERFACE_ONLY, "true"); + codegen.additionalProperties().put(USE_RESPONSE_ENTITY, "false"); + codegen.additionalProperties().put(DELEGATE_PATTERN, "true"); + codegen.additionalProperties().put(REQUEST_MAPPING_OPTION, "api_interface"); + codegen.additionalProperties().put(SPRING_CONTROLLER, "true"); + + ClientOptInput input = new ClientOptInput(); + input.openAPI(openAPI); + input.config(codegen); + + DefaultGenerator generator = new DefaultGenerator(); + + Map files = generator.opts(input).generate().stream() + .collect(Collectors.toMap(File::getName, Function.identity())); + + JavaFileAssert javaFileAssert = JavaFileAssert.assertThat(files.get("TestApi.java")); + javaFileAssert + .assertMethod("_postToTest") + .assertMethodAnnotations() + .containsWithName("javax.annotation.security.RolesAllowed") + .containsWithName("org.springframework.security.access.annotation.Secured") + .containsWithName("org.springframework.security.access.prepost.PreAuthorize"); + } + @Test public void multiLineOperationDescription() throws IOException { diff --git a/modules/openapi-generator/src/test/resources/2_0/issue12219_array.yaml b/modules/openapi-generator/src/test/resources/2_0/issue12219_array.yaml new file mode 100644 index 000000000000..393f6a258127 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/2_0/issue12219_array.yaml @@ -0,0 +1,56 @@ +swagger: '2.0' +info: + description: 'blah' + version: 1.0.0 + title: sample spec +host: fake.site.com +tags: + - name: Test +schemes: + - https +paths: + /test: + post: + summary: Post to test + description: '' + operationId: postToTest + consumes: + - application/json + produces: + - application/json + parameters: + - in: body + name: Obj to test + schema: + $ref: '#/definitions/ObjTest' + responses: + '201': + description: successful operation + schema: + $ref: '#/definitions/ObjTest' + x-operation-extra-annotation: + - '@javax.annotation.security.RolesAllowed({"ROLE_TEST"})' + - '@org.springframework.security.access.annotation.Secured({"ROLE_TEST"})' + - '@org.springframework.security.access.prepost.PreAuthorize("hasRole(''ROLE_TEST'') and hasRole(''ROLE_TEST_TWO'')")' +definitions: + ObjTest: + description: A model to return + type: object + properties: + field1: + type: integer + format: int64 + field2: + type: string + pattern: "\\w" + x-pattern-message: "Only letters, numbers and underscore" + field3: + type: string + pattern: "\\w" + EnumTest: + title: An enum to test + type: string + enum: + - ONE + - Two + - three \ No newline at end of file From dfa6e575c335117873f8a18ceec676babd7b3d62 Mon Sep 17 00:00:00 2001 From: Rodrigo Maciel de Almeida Date: Wed, 9 Aug 2023 10:13:49 -0300 Subject: [PATCH 3/3] update samples --- .../api/AnotherFakeApiDelegate.java | 2 +- .../java/org/openapitools/api/FakeApi.java | 29 +++++++++++++++++++ .../org/openapitools/api/FakeApiDelegate.java | 29 ++++++++++++++++--- .../api/FakeClassnameTestApiDelegate.java | 2 +- .../org/openapitools/api/PetApiDelegate.java | 14 ++++----- .../openapitools/api/StoreApiDelegate.java | 8 ++--- .../org/openapitools/api/UserApiDelegate.java | 4 +-- .../src/main/resources/openapi.yaml | 17 +++++++++++ 8 files changed, 86 insertions(+), 19 deletions(-) diff --git a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/AnotherFakeApiDelegate.java b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/AnotherFakeApiDelegate.java index a3074ad55632..f65a40c0cf62 100644 --- a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/AnotherFakeApiDelegate.java +++ b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/AnotherFakeApiDelegate.java @@ -44,7 +44,7 @@ default Mono call123testSpecialTags(Mono client, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"client\" : \"client\" }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } } diff --git a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeApi.java b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeApi.java index 5b5193a1e228..c3b5edab35a0 100644 --- a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeApi.java +++ b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeApi.java @@ -567,6 +567,35 @@ default Mono testQueryParameterCollectionFormat( } + /** + * GET /fake/response-with-example + * This endpoint defines an example value for its response schema. + * + * @return Success (status code 200) + */ + @ApiOperation( + tags = { "fake" }, + value = "", + nickname = "testWithResultExample", + notes = "This endpoint defines an example value for its response schema.", + response = Integer.class + ) + @ApiResponses({ + @ApiResponse(code = 200, message = "Success", response = Integer.class) + }) + @RequestMapping( + method = RequestMethod.GET, + value = "/fake/response-with-example", + produces = { "application/json" } + ) + @ResponseStatus(HttpStatus.OK) + default Mono testWithResultExample( + @ApiIgnore final ServerWebExchange exchange + ) { + return getDelegate().testWithResultExample(exchange); + } + + /** * POST /fake/{petId}/uploadImageWithRequiredFile : uploads an image (required) * diff --git a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeApiDelegate.java b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeApiDelegate.java index 122639ab0da5..5392a1a9baf9 100644 --- a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeApiDelegate.java +++ b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeApiDelegate.java @@ -87,7 +87,7 @@ default Mono fakeOuterCompositeSerialize(Mono ou for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("*/*"))) { String exampleString = "{ \"my_string\" : \"my_string\", \"my_number\" : 0.8008281904610115, \"my_boolean\" : true }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } } @@ -141,7 +141,7 @@ default Mono responseObjectDifferentNames for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"UPPER_CASE_PROPERTY_SNAKE\" : \"UPPER_CASE_PROPERTY_SNAKE\", \"lower-case-property-dashes\" : \"lower-case-property-dashes\", \"property name with spaces\" : \"property name with spaces\", \"normalPropertyName\" : \"normalPropertyName\" }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } } @@ -197,7 +197,7 @@ default Mono testClientModel(Mono client, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"client\" : \"client\" }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } } @@ -361,6 +361,27 @@ default Mono testQueryParameterCollectionFormat(List pipe, } + /** + * GET /fake/response-with-example + * This endpoint defines an example value for its response schema. + * + * @return Success (status code 200) + * @see FakeApi#testWithResultExample + */ + default Mono testWithResultExample(ServerWebExchange exchange) { + Mono result = Mono.empty(); + exchange.getResponse().setStatusCode(HttpStatus.NOT_IMPLEMENTED); + for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { + if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { + String exampleString = "42"; + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); + break; + } + } + return result.then(Mono.empty()); + + } + /** * POST /fake/{petId}/uploadImageWithRequiredFile : uploads an image (required) * @@ -380,7 +401,7 @@ default Mono uploadFileWithRequiredFile(Long petId, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"code\" : 0, \"type\" : \"type\", \"message\" : \"message\" }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } } diff --git a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeClassnameTestApiDelegate.java b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeClassnameTestApiDelegate.java index 04ac8af146b0..57760b13889c 100644 --- a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeClassnameTestApiDelegate.java +++ b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/FakeClassnameTestApiDelegate.java @@ -44,7 +44,7 @@ default Mono testClassname(Mono client, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"client\" : \"client\" }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } } diff --git a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/PetApiDelegate.java b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/PetApiDelegate.java index 2a04517067b7..ade70ed2956b 100644 --- a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/PetApiDelegate.java +++ b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/PetApiDelegate.java @@ -83,12 +83,12 @@ default Mono> findPetsByStatus(List status, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "[ { \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" }, { \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" } ]"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) { String exampleString = " 123456789 123456789 aeiou doggie aeiou 123456789 aeiou aeiou "; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/xml"), exampleString); break; } } @@ -114,12 +114,12 @@ default Mono> findPetsByTags(Set tags, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "[ { \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" }, { \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" } ]"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) { String exampleString = " 123456789 123456789 aeiou doggie aeiou 123456789 aeiou aeiou "; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/xml"), exampleString); break; } } @@ -144,12 +144,12 @@ default Mono getPetById(Long petId, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"photoUrls\" : [ \"photoUrls\", \"photoUrls\" ], \"name\" : \"doggie\", \"id\" : 0, \"category\" : { \"name\" : \"default-name\", \"id\" : 6 }, \"tags\" : [ { \"name\" : \"name\", \"id\" : 1 }, { \"name\" : \"name\", \"id\" : 1 } ], \"status\" : \"available\" }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) { String exampleString = " 123456789 123456789 aeiou doggie aeiou 123456789 aeiou aeiou "; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/xml"), exampleString); break; } } @@ -215,7 +215,7 @@ default Mono uploadFile(Long petId, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"code\" : 0, \"type\" : \"type\", \"message\" : \"message\" }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } } diff --git a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/StoreApiDelegate.java b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/StoreApiDelegate.java index 75fe1083006a..e2a6e5ede83f 100644 --- a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/StoreApiDelegate.java +++ b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/StoreApiDelegate.java @@ -78,12 +78,12 @@ default Mono getOrderById(Long orderId, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"petId\" : 6, \"quantity\" : 1, \"id\" : 0, \"shipDate\" : \"2000-01-23T04:56:07.000+00:00\", \"complete\" : false, \"status\" : \"placed\" }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) { String exampleString = " 123456789 123456789 123 2000-01-23T04:56:07.000Z aeiou true "; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/xml"), exampleString); break; } } @@ -107,12 +107,12 @@ default Mono placeOrder(Mono order, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"petId\" : 6, \"quantity\" : 1, \"id\" : 0, \"shipDate\" : \"2000-01-23T04:56:07.000+00:00\", \"complete\" : false, \"status\" : \"placed\" }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) { String exampleString = " 123456789 123456789 123 2000-01-23T04:56:07.000Z aeiou true "; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/xml"), exampleString); break; } } diff --git a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/UserApiDelegate.java b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/UserApiDelegate.java index f27edc8f907c..a7dd7ea9a6db 100644 --- a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/UserApiDelegate.java +++ b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/java/org/openapitools/api/UserApiDelegate.java @@ -112,12 +112,12 @@ default Mono getUserByName(String username, for (MediaType mediaType : exchange.getRequest().getHeaders().getAccept()) { if (mediaType.isCompatibleWith(MediaType.valueOf("application/json"))) { String exampleString = "{ \"firstName\" : \"firstName\", \"lastName\" : \"lastName\", \"password\" : \"password\", \"userStatus\" : 6, \"phone\" : \"phone\", \"id\" : 0, \"email\" : \"email\", \"username\" : \"username\" }"; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/json"), exampleString); break; } if (mediaType.isCompatibleWith(MediaType.valueOf("application/xml"))) { String exampleString = " 123456789 aeiou aeiou aeiou aeiou aeiou aeiou 123 "; - result = ApiUtil.getExampleResponse(exchange, mediaType, exampleString); + result = ApiUtil.getExampleResponse(exchange, MediaType.valueOf("application/xml"), exampleString); break; } } diff --git a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/resources/openapi.yaml b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/resources/openapi.yaml index 98f0e7e6f9d0..ae88e043d50b 100644 --- a/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/resources/openapi.yaml +++ b/samples/server/petstore/springboot-reactive-noResponseEntity/src/main/resources/openapi.yaml @@ -1088,6 +1088,23 @@ paths: x-accepts: application/json x-tags: - tag: fake + /fake/response-with-example: + get: + description: This endpoint defines an example value for its response schema. + operationId: testWithResultExample + responses: + "200": + content: + application/json: + schema: + example: 42 + type: integer + description: Success + tags: + - fake + x-accepts: application/json + x-tags: + - tag: fake /fake/test-query-parameters: put: description: To test the collection format in query parameters