From ea08106c809c17a660c9180210e12ae02c2d5914 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Bresson?= Date: Tue, 12 Feb 2019 19:27:20 +0100 Subject: [PATCH] [core] Fix NullPointer when schema is not set in header (#2133) --- .../openapitools/codegen/DefaultCodegen.java | 15 +++++++++++---- .../codegen/DefaultCodegenTest.java | 19 +++++++++++++++++++ 2 files changed, 30 insertions(+), 4 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java index 5422042499..6db9d65b22 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/DefaultCodegen.java @@ -3297,12 +3297,19 @@ public class DefaultCodegen implements CodegenConfig { */ private void addHeaders(ApiResponse response, List properties) { if (response.getHeaders() != null) { - for (Map.Entry headers : response.getHeaders().entrySet()) { - String description = headers.getValue().getDescription(); + for (Map.Entry headerEntry : response.getHeaders().entrySet()) { + String description = headerEntry.getValue().getDescription(); // follow the $ref - Header header = ModelUtils.getReferencedHeader(this.openAPI, headers.getValue()); + Header header = ModelUtils.getReferencedHeader(this.openAPI, headerEntry.getValue()); - CodegenProperty cp = fromProperty(headers.getKey(), header.getSchema()); + Schema schema; + if(header.getSchema() == null) { + LOGGER.warn("No schema defined for Header '" + headerEntry.getKey() +"', using a String schema"); + schema = new StringSchema(); + } else { + schema = header.getSchema(); + } + CodegenProperty cp = fromProperty(headerEntry.getKey(), schema); cp.setDescription(escapeText(description)); cp.setUnescapedDescription(description); properties.add(cp); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index 63bf869c79..348c41b1f8 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -22,6 +22,7 @@ import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.Operation; import io.swagger.v3.oas.models.PathItem; import io.swagger.v3.oas.models.media.*; +import io.swagger.v3.oas.models.headers.Header; import io.swagger.v3.oas.models.parameters.QueryParameter; import io.swagger.v3.oas.models.parameters.RequestBody; import io.swagger.v3.oas.models.responses.ApiResponse; @@ -532,6 +533,24 @@ public class DefaultCodegenTest { Assert.assertEquals(co2.path, "/some/path"); } + @Test + public void testResponseWithNoSchemaInHeaders() { + OpenAPI openAPI = TestUtils.createOpenAPI(); + ApiResponse response2XX = new ApiResponse() + .description("OK") + .addHeaderObject("x-custom-header", new Header() + .description("a custom header") + .style(Header.StyleEnum.SIMPLE)); + Operation operation1 = new Operation().operationId("op1").responses(new ApiResponses().addApiResponse("2XX", response2XX)); + openAPI.path("/here", new PathItem().get(operation1)); + final DefaultCodegen codegen = new DefaultCodegen(); + codegen.setOpenAPI(openAPI); + + CodegenResponse cr = codegen.fromResponse("2XX", response2XX); + Assert.assertNotNull(cr); + Assert.assertTrue(cr.hasHeaders); + } + private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) { CodegenDiscriminator test = new CodegenDiscriminator(); test.setPropertyName("DollarUnderscoretype");