Fix NullPointerException in getSchemaType(Schema) (#14)

Fix #11
This commit is contained in:
Jérémie Bresson 2018-05-13 09:23:48 +02:00 committed by William Cheng
parent 9fa9b115e3
commit 05a2f4b240
3 changed files with 80 additions and 5 deletions

View File

@ -1166,6 +1166,7 @@ public class DefaultCodegen implements CodegenConfig {
// TODO better logic to handle compose schema
if (schema instanceof ComposedSchema) { // composed schema
ComposedSchema cs = (ComposedSchema) schema;
if(cs.getAllOf() != null) {
for (Schema s : cs.getAllOf()) {
if (s != null) {
// using the first schema defined in allOf
@ -1174,6 +1175,7 @@ public class DefaultCodegen implements CodegenConfig {
}
}
}
}
if (StringUtils.isNotBlank(schema.get$ref())) { // reference to another definition/schema
// get the schema/model name from $ref

View File

@ -202,4 +202,16 @@ public class DefaultCodegenTest {
Assert.assertEquals(co.produces.size(), 1);
Assert.assertEquals(co.produces.get(0).get("mediaType"), "application/json");
}
@Test
public void testGetSchemaTypeWithComposedSchemaWithOneOf() {
final OpenAPI openAPI = new OpenAPIParser().readLocation("src/test/resources/3_0/composed-oneof.yaml", null, new ParseOptions()).getOpenAPI();
final DefaultCodegen codegen = new DefaultCodegen();
Operation operation = openAPI.getPaths().get("/state").getPost();
Schema schema = ModelUtils.getSchemaFromRequestBody(operation.getRequestBody());
String type = codegen.getSchemaType(schema);
Assert.assertNotNull(type);
}
}

View File

@ -0,0 +1,61 @@
openapi: 3.0.1
info:
title: oneOf test
version: '1.0'
servers:
- url: 'http://localhost:8000/'
paths:
/state:
get:
operationId: getState
responses:
'200':
description: OK
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/ObjA'
- $ref: '#/components/schemas/ObjB'
discriminator:
propertyName: realtype
mapping:
a-type: '#/components/schemas/ObjA'
b-type: '#/components/schemas/ObjB'
post:
operationId: createState
requestBody:
content:
application/json:
schema:
oneOf:
- $ref: '#/components/schemas/ObjA'
- $ref: '#/components/schemas/ObjB'
discriminator:
propertyName: realtype
mapping:
a-type: '#/components/schemas/ObjA'
b-type: '#/components/schemas/ObjB'
required: true
responses:
'201':
description: OK
components:
schemas:
ObjA:
type: object
properties:
realtype:
type: string
message:
type: string
ObjB:
type: object
properties:
realtype:
type: string
description:
type: string
code:
type: integer
format: int32