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 75123e86a30..a979dff275f 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 @@ -559,7 +559,9 @@ public class ModelUtils { } // additionalProperties explicitly set to false - if (schema.getAdditionalProperties() instanceof Boolean && Boolean.FALSE.equals(schema.getAdditionalProperties())) { + if ((schema.getAdditionalProperties() instanceof Boolean && Boolean.FALSE.equals(schema.getAdditionalProperties())) || + (schema.getAdditionalProperties() instanceof Schema && Boolean.FALSE.equals(((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue())) + ) { return false; } @@ -808,7 +810,13 @@ public class ModelUtils { (null != schema.getProperties() && !schema.getProperties().isEmpty()) && // no additionalProperties is set (schema.getAdditionalProperties() == null || - (schema.getAdditionalProperties() instanceof Boolean && !(Boolean) schema.getAdditionalProperties())); + // additionalProperties is boolean and set to false + (schema.getAdditionalProperties() instanceof Boolean && !(Boolean) schema.getAdditionalProperties()) || + // additionalProperties is a schema with its boolean value set to false + (schema.getAdditionalProperties() instanceof Schema && + ((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue() != null && + !((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue()) + ); } public static boolean hasValidation(Schema sc) { @@ -2422,7 +2430,7 @@ public class ModelUtils { * For example, a schema that only has a `description` without any `properties` or `$ref` defined. * * @param schema the schema - * @return if the schema is only metadata and not an actual type + * @return if the schema is only metadata and not an actual type */ public static boolean isMetadataOnlySchema(Schema schema) { return !(schema.get$ref() != null || 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 621e80de710..44e0e008368 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 @@ -353,6 +353,9 @@ public class ModelUtilsTest { Assert.assertNull(anyof1Property.getType()); Assert.assertTrue(ModelUtils.hasAnyOf(anyof1Property)); Assert.assertTrue(ModelUtils.isAnyOf(anyof1Property)); + + Schema objectSchema = ModelUtils.getSchema(openAPI, "ObjectSchema"); + Assert.assertFalse(ModelUtils.isMapSchema(objectSchema)); } // 3.1 spec tests @@ -392,7 +395,7 @@ public class ModelUtilsTest { Assert.assertTrue(ModelUtils.isAnyOf(anyof2)); Schema objectSchema = ModelUtils.getSchema(openAPI, "ObjectSchema"); - Assert.assertTrue(ModelUtils.isMapSchema(objectSchema)); + Assert.assertFalse(ModelUtils.isMapSchema(objectSchema)); Schema complexComposedSchema = ModelUtils.getSchema(openAPI, "ComplexComposedSchema"); Assert.assertTrue(ModelUtils.isComplexComposedSchema(complexComposedSchema)); @@ -641,4 +644,35 @@ public class ModelUtilsTest { assertFalse(ModelUtils.isUnsupportedSchema(openAPI, (Schema) property2.getProperties().get("condition"))); assertFalse(ModelUtils.isUnsupportedSchema(openAPI, (Schema) property2.getProperties().get("purpose"))); } + + @Test + public void testModelWithPropertiesOnly() { + // Schema with no properties + Schema testSchema = new ObjectSchema().properties(null); + assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema)); + + // Schema with properties + testSchema.setProperties(Map.of("foo", new Schema())); + assertTrue(ModelUtils.isModelWithPropertiesOnly(testSchema)); + + // Explicitly no additional properties + testSchema.setAdditionalProperties(false); + assertTrue(ModelUtils.isModelWithPropertiesOnly(testSchema)); + + // With additional properties + testSchema.setAdditionalProperties(true); + assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema)); + + // Additional properties is a schema set to false + testSchema.setAdditionalProperties(new Schema().booleanSchemaValue(false)); + assertTrue(ModelUtils.isModelWithPropertiesOnly(testSchema)); + + // Additional properties is a schema set to true + testSchema.setAdditionalProperties(new Schema().booleanSchemaValue(true)); + assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema)); + + // Additional properties is a custom schema + testSchema.setAdditionalProperties(new Schema().type("string")); + assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema)); + } } diff --git a/modules/openapi-generator/src/test/resources/3_0/schema.yaml b/modules/openapi-generator/src/test/resources/3_0/schema.yaml index 2a6acce5c57..147e12d9c95 100644 --- a/modules/openapi-generator/src/test/resources/3_0/schema.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/schema.yaml @@ -65,6 +65,14 @@ components: oneOf: - type: string - type: integer + ObjectSchema: + type: object + additionalProperties: false + properties: + name: + type: string + address: + type: string anyof1: anyOf: - type: string