Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix missing nullable #2189

Merged
merged 7 commits into from
Feb 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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