From cfebd14c2bb1da219aa75d841658108fb5e14edb Mon Sep 17 00:00:00 2001 From: Oleh Kurpiak Date: Wed, 6 Jul 2022 20:29:55 +0300 Subject: [PATCH] Handle custom content type example (#12460) --- .../codegen/examples/ExampleGenerator.java | 3 + .../java/spring/SpringCodegenTest.java | 24 ++++++++ .../src/test/resources/bugs/issue_11731.yaml | 59 +++++++++++++++++++ 3 files changed, 86 insertions(+) create mode 100644 modules/openapi-generator/src/test/resources/bugs/issue_11731.yaml diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java index 8b7f64b72565..2a62a8cae1a2 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/examples/ExampleGenerator.java @@ -170,6 +170,9 @@ public class ExampleGenerator { kv.put(EXAMPLE, example); output.add(kv); } + } else { + kv.put(EXAMPLE, null); + output.add(kv); } } } else { 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 59e2054df38b..601b8c0769db 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 @@ -1404,6 +1404,30 @@ public class SpringCodegenTest { .bodyContainsLines("super.putSomeMapItem(key, someMapItem);"); } + @Test + public void shouldHandleCustomResponseType_issue11731() throws IOException { + File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); + output.deleteOnExit(); + + OpenAPI openAPI = new OpenAPIParser() + .readLocation("src/test/resources/bugs/issue_11731.yaml", null, new ParseOptions()).getOpenAPI(); + SpringCodegen codegen = new SpringCodegen(); + codegen.setLibrary(SPRING_BOOT); + codegen.setOutputDir(output.getAbsolutePath()); + + ClientOptInput input = new ClientOptInput() + .openAPI(openAPI) + .config(codegen); + + DefaultGenerator generator = new DefaultGenerator(); + Map files = generator.opts(input).generate().stream() + .collect(Collectors.toMap(File::getName, Function.identity())); + + JavaFileAssert.assertThat(files.get("CustomersApi.java")) + .assertMethod("getAllUsingGET1") + .bodyContainsLines("if (mediaType.isCompatibleWith(MediaType.valueOf(\"application/hal+json\"))) {"); + } + @Test public void shouldHandleContentTypeWithSecondWildcardSubtype_issue12457() throws IOException { File output = Files.createTempDirectory("test").toFile().getCanonicalFile(); diff --git a/modules/openapi-generator/src/test/resources/bugs/issue_11731.yaml b/modules/openapi-generator/src/test/resources/bugs/issue_11731.yaml new file mode 100644 index 000000000000..02c62808a1fc --- /dev/null +++ b/modules/openapi-generator/src/test/resources/bugs/issue_11731.yaml @@ -0,0 +1,59 @@ +openapi: 3.0.2 +info: + title: Petstore - OpenAPI 3.0 + description: Specification to reproduce issues with mediatypes other than common application/json + termsOfService: http://inss.ch + contact: + email: openapi@inss.ch + license: + name: Apache 2.0 + url: http://www.apache.org/licenses/LICENSE-2.0.html + version: 1.0.0 +servers: +- url: http://localhost:8080/api/v3 + +paths: + /customers: + get: + tags: + - customer-controller + summary: getAll + operationId: getAllUsingGET_1 + responses: + 200: + description: OK + content: + application/hal+json: + schema: + $ref: '#/components/schemas/ResponseObject' + 401: + description: Unauthorized + content: {} + 403: + description: Forbidden + content: {} + 404: + description: Not Found + content: {} + +components: + schemas: + ResponseObject: + required: + - name + - photoUrls + type: object + properties: + id: + type: integer + format: int64 + example: 10 + description: 'Id' + status: + type: string + description: pet status in the store + enum: + - available + - pending + - sold + example: available \ No newline at end of file