Skip to content

Commit

Permalink
Fix NPE in ModelUtils.isFreeFormObject() (OpenAPITools#1625)
Browse files Browse the repository at this point in the history
Fixes OpenAPITools#1696

An object schema containing no properties that also has additionalProperties
set to an object schema with no properties will cause
ModelUtils.isFreeFormObject to throw an NPE.
This PR adds additional checking to avoid the NPE.
  • Loading branch information
padamstx authored and wing328 committed Dec 7, 2018
1 parent 2f4bea6 commit 2d39f14
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -547,20 +547,17 @@ public static boolean isFreeFormObject(Schema schema) {
if ("object".equals(schema.getType())) {
// no properties
if ((schema.getProperties() == null || schema.getProperties().isEmpty())) {
if (schema.getAdditionalProperties() == null) {
Schema addlProps = getAdditionalProperties(schema);
// additionalProperties not defined
if (addlProps == null) {
return true;
} else {
// additionalProperties set to true
if (schema.getAdditionalProperties() instanceof Boolean
&& (Boolean) schema.getAdditionalProperties()) {
return true;
}

// additionalProperties is set to {}
if (schema.getAdditionalProperties() instanceof Schema && schema.getAdditionalProperties() != null
&& schema.getAdditionalProperties() instanceof ObjectSchema
&& ((Schema) schema.getAdditionalProperties()).getProperties().isEmpty()) {
return true;
if (addlProps instanceof ObjectSchema) {
ObjectSchema objSchema = (ObjectSchema) addlProps;
// additionalProperties defined as {}
if (objSchema.getProperties() == null || objSchema.getProperties().isEmpty()) {
return true;
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,4 +199,32 @@ public void testComposedSchemasAreNotUnaliased() {

Assert.assertEquals(refToComposedSchema, ModelUtils.unaliasSchema(allSchemas, refToComposedSchema));
}

/**
* Issue https://github.com/OpenAPITools/openapi-generator/issues/1624.
* ModelUtils.isFreeFormObject() should not throw an NPE when passed an empty
* object schema that has additionalProperties defined as an empty object schema.
*/
@Test
public void testIsFreeFormObject() {
// Create initial "empty" object schema.
ObjectSchema objSchema = new ObjectSchema();
Assert.assertTrue(ModelUtils.isFreeFormObject(objSchema));

// Set additionalProperties to an empty ObjectSchema.
objSchema.setAdditionalProperties(new ObjectSchema());
Assert.assertTrue(ModelUtils.isFreeFormObject(objSchema));

// Add a single property to the schema (no longer a free-form object).
Map<String, Schema> props = new HashMap<>();
props.put("prop1", new StringSchema());
objSchema.setProperties(props);
Assert.assertFalse(ModelUtils.isFreeFormObject(objSchema));

// Test a non-object schema
Assert.assertFalse(ModelUtils.isFreeFormObject(new StringSchema()));

// Test a null schema
Assert.assertFalse(ModelUtils.isFreeFormObject(null));
}
}

0 comments on commit 2d39f14

Please sign in to comment.