[core] Fix NullPointer when schema is not set in header (#2133)

This commit is contained in:
Jérémie Bresson 2019-02-12 19:27:20 +01:00 committed by William Cheng
parent de33360883
commit ea08106c80
2 changed files with 30 additions and 4 deletions

View File

@ -3297,12 +3297,19 @@ public class DefaultCodegen implements CodegenConfig {
*/
private void addHeaders(ApiResponse response, List<CodegenProperty> properties) {
if (response.getHeaders() != null) {
for (Map.Entry<String, Header> headers : response.getHeaders().entrySet()) {
String description = headers.getValue().getDescription();
for (Map.Entry<String, Header> 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);

View File

@ -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");