From 2d39f1488202442c2ec2fd378a0c4849f6e99395 Mon Sep 17 00:00:00 2001 From: Phil Adams Date: Fri, 7 Dec 2018 09:16:58 -0600 Subject: [PATCH] Fix NPE in ModelUtils.isFreeFormObject() (#1625) Fixes #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. --- .../codegen/utils/ModelUtils.java | 21 ++++++-------- .../codegen/utils/ModelUtilsTest.java | 28 +++++++++++++++++++ 2 files changed, 37 insertions(+), 12 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 6beb9a7616f..8ac76f74734 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -547,20 +547,17 @@ public class ModelUtils { 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; + } } } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java index e024897e231..16951a91b5d 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java @@ -199,4 +199,32 @@ public class ModelUtilsTest { 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 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)); + } }