Skip to content

Commit

Permalink
Fix missing nullable (#2189)
Browse files Browse the repository at this point in the history
* Add test case which reproduces the issue

* Fix missing `nullable`

* Tweak property name

* Add test case which covers `Paths`

* Add test case for CodegenProperty.isNullable

* Fix missing CodegenProperty.isNullable

* Rename r -> referencedSchema
  • Loading branch information
ackintosh authored and wing328 committed Feb 19, 2019
1 parent c79d277 commit 1d08296
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2041,10 +2041,12 @@ public CodegenProperty fromProperty(String name, Schema p) {
property.allowableValues = allowableValues;
}
}

Schema referencedSchema = ModelUtils.getReferencedSchema(this.openAPI, p);

//Referenced enum case:
Schema r = ModelUtils.getReferencedSchema(this.openAPI, p);
if (r.getEnum() != null && !r.getEnum().isEmpty()) {
List<Object> _enum = r.getEnum();
if (referencedSchema.getEnum() != null && !referencedSchema.getEnum().isEmpty()) {
List<Object> _enum = referencedSchema.getEnum();

Map<String, Object> allowableValues = new HashMap<String, Object>();
allowableValues.put("values", _enum);
Expand All @@ -2053,6 +2055,10 @@ public CodegenProperty fromProperty(String name, Schema p) {
}
}

if (referencedSchema.getNullable() != null) {
property.isNullable = referencedSchema.getNullable();
}

property.dataType = getTypeDeclaration(p);
property.dataFormat = p.getFormat();
property.baseType = getSchemaType(p);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@ private Schema modelFromProperty(ObjectSchema object, String path) {
model.setName(object.getName());
model.setXml(xml);
model.setRequired(object.getRequired());
model.setNullable(object.getNullable());
if (properties != null) {
flattenProperties(properties, path);
model.setProperties(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,18 @@ public void testResponseWithNoSchemaInHeaders() {
Assert.assertTrue(cr.hasHeaders);
}

@Test
public void testNullableProperty() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/examples.yaml");
new InlineModelResolver().flatten(openAPI);
final DefaultCodegen codegen = new DefaultCodegen();
codegen.setOpenAPI(openAPI);

CodegenProperty property = codegen.fromProperty("address", (Schema) openAPI.getComponents().getSchemas().get("User").getProperties().get("address"));

Assert.assertTrue(property.isNullable);
}

private void verifyPersonDiscriminator(CodegenDiscriminator discriminator) {
CodegenDiscriminator test = new CodegenDiscriminator();
test.setPropertyName("DollarUnderscoretype");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,4 +695,28 @@ public void emptyExampleOnStringTypeModels() {
assertTrue(ModelUtils.getReferencedSchema(openAPI, schema.getItems()) instanceof StringSchema);
assertNull(ModelUtils.getReferencedSchema(openAPI, schema.getItems()).getExample());
}

@Test
public void nullable() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/inline_model_resolver.yaml");
new InlineModelResolver().flatten(openAPI);

Schema nullablePropertyReference = (Schema) openAPI.getComponents().getSchemas().get("InlinePropertyIsNullable").getProperties().get("nullable_property");
Schema nullablePropertySchema = ModelUtils.getReferencedSchema(openAPI, nullablePropertyReference);
assertTrue(nullablePropertySchema.getNullable());


Schema nullableRequestBodyReference = (Schema) openAPI
.getPaths()
.get("/nullable_properties")
.getPost()
.getRequestBody()
.getContent()
.get("application/json")
.getSchema()
.getProperties()
.get("nullable_request_body_property");
Schema nullableRequestBodySchema = ModelUtils.getReferencedSchema(openAPI, nullableRequestBodyReference);
assertTrue(nullableRequestBodySchema.getNullable());
}
}
12 changes: 11 additions & 1 deletion modules/openapi-generator/src/test/resources/3_0/examples.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -107,4 +107,14 @@ paths:
responses:
'200':
description: successful operation

components:
schemas:
User:
type: object
properties:
address:
type: object
nullable: true
properties:
city:
type: string
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,24 @@ paths:
type: array
items:
$ref: '#/components/schemas/EmptyExampleOnStringTypeModels'
/nullable_properties:
post:
requestBody:
content:
application/json:
schema:
type: object
properties:
nullable_request_body_property:
type: object
nullable: true
properties:
nullable_request_body_property_name:
type: string
operationId: arbitraryObjectRequestBodyProperty
responses:
'200':
description: OK
components:
schemas:
Users:
Expand Down Expand Up @@ -301,3 +319,12 @@ components:
EmptyExampleOnStringTypeModels:
type: string
example: ''
InlinePropertyIsNullable:
type: object
properties:
nullable_property:
type: object
nullable: true
properties:
nullable_property_name:
type: string

0 comments on commit 1d08296

Please sign in to comment.