Skip to content

Commit b3cf336

Browse files
committed
[3.1] Fixed lack of check for ´booleanSchemaValue`
1 parent aacbdf8 commit b3cf336

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -808,7 +808,13 @@ public static boolean isModelWithPropertiesOnly(Schema schema) {
808808
(null != schema.getProperties() && !schema.getProperties().isEmpty()) &&
809809
// no additionalProperties is set
810810
(schema.getAdditionalProperties() == null ||
811-
(schema.getAdditionalProperties() instanceof Boolean && !(Boolean) schema.getAdditionalProperties()));
811+
// additionalProperties is boolean and set to false
812+
(schema.getAdditionalProperties() instanceof Boolean && !(Boolean) schema.getAdditionalProperties()) ||
813+
// additionalProperties is a schema with its boolean value set to false
814+
(schema.getAdditionalProperties() instanceof Schema &&
815+
((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue() != null &&
816+
!((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue())
817+
);
812818
}
813819

814820
public static boolean hasValidation(Schema sc) {
@@ -2415,7 +2421,7 @@ public static void copyMetadata(Schema from, Schema to) {
24152421
* For example, a schema that only has a `description` without any `properties` or `$ref` defined.
24162422
*
24172423
* @param schema the schema
2418-
* @return if the schema is only metadata and not an actual type
2424+
* @return if the schema is only metadata and not an actual type
24192425
*/
24202426
public static boolean isMetadataOnlySchema(Schema schema) {
24212427
return !(schema.get$ref() != null ||

modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,4 +585,35 @@ public void isUnsupportedSchemaTest() {
585585
assertFalse(ModelUtils.isUnsupportedSchema(openAPI, (Schema) property2.getProperties().get("condition")));
586586
assertFalse(ModelUtils.isUnsupportedSchema(openAPI, (Schema) property2.getProperties().get("purpose")));
587587
}
588+
589+
@Test
590+
public void testModelWithPropertiesOnly() {
591+
// Schema with no properties
592+
Schema testSchema = new ObjectSchema().properties(null);
593+
assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema));
594+
595+
// Schema with properties
596+
testSchema.setProperties(Map.of("foo", new Schema()));
597+
assertTrue(ModelUtils.isModelWithPropertiesOnly(testSchema));
598+
599+
// Explicitly no additional properties
600+
testSchema.setAdditionalProperties(false);
601+
assertTrue(ModelUtils.isModelWithPropertiesOnly(testSchema));
602+
603+
// With additional properties
604+
testSchema.setAdditionalProperties(true);
605+
assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema));
606+
607+
// Additional properties is a schema set to false
608+
testSchema.setAdditionalProperties(new Schema().booleanSchemaValue(false));
609+
assertTrue(ModelUtils.isModelWithPropertiesOnly(testSchema));
610+
611+
// Additional properties is a schema set to true
612+
testSchema.setAdditionalProperties(new Schema().booleanSchemaValue(true));
613+
assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema));
614+
615+
// Additional properties is a custom schema
616+
testSchema.setAdditionalProperties(new Schema().type("string"));
617+
assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema));
618+
}
588619
}

0 commit comments

Comments
 (0)