Skip to content

Commit 27d3c6f

Browse files
authored
[3.1] Fixed lack of check for booleanSchemaValue (#21742)
* [3.1] Fixed lack of check for ´booleanSchemaValue` * [3.1] Fixed `isMapSchema`
1 parent eae5088 commit 27d3c6f

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,9 @@ public static boolean isMapSchema(Schema schema) {
559559
}
560560

561561
// additionalProperties explicitly set to false
562-
if (schema.getAdditionalProperties() instanceof Boolean && Boolean.FALSE.equals(schema.getAdditionalProperties())) {
562+
if ((schema.getAdditionalProperties() instanceof Boolean && Boolean.FALSE.equals(schema.getAdditionalProperties())) ||
563+
(schema.getAdditionalProperties() instanceof Schema && Boolean.FALSE.equals(((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue()))
564+
) {
563565
return false;
564566
}
565567

@@ -808,7 +810,13 @@ public static boolean isModelWithPropertiesOnly(Schema schema) {
808810
(null != schema.getProperties() && !schema.getProperties().isEmpty()) &&
809811
// no additionalProperties is set
810812
(schema.getAdditionalProperties() == null ||
811-
(schema.getAdditionalProperties() instanceof Boolean && !(Boolean) schema.getAdditionalProperties()));
813+
// additionalProperties is boolean and set to false
814+
(schema.getAdditionalProperties() instanceof Boolean && !(Boolean) schema.getAdditionalProperties()) ||
815+
// additionalProperties is a schema with its boolean value set to false
816+
(schema.getAdditionalProperties() instanceof Schema &&
817+
((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue() != null &&
818+
!((Schema) schema.getAdditionalProperties()).getBooleanSchemaValue())
819+
);
812820
}
813821

814822
public static boolean hasValidation(Schema sc) {
@@ -2422,7 +2430,7 @@ public static void copyMetadata(Schema from, Schema to) {
24222430
* For example, a schema that only has a `description` without any `properties` or `$ref` defined.
24232431
*
24242432
* @param schema the schema
2425-
* @return if the schema is only metadata and not an actual type
2433+
* @return if the schema is only metadata and not an actual type
24262434
*/
24272435
public static boolean isMetadataOnlySchema(Schema schema) {
24282436
return !(schema.get$ref() != null ||

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

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,9 @@ public void test30Schemas() {
353353
Assert.assertNull(anyof1Property.getType());
354354
Assert.assertTrue(ModelUtils.hasAnyOf(anyof1Property));
355355
Assert.assertTrue(ModelUtils.isAnyOf(anyof1Property));
356+
357+
Schema objectSchema = ModelUtils.getSchema(openAPI, "ObjectSchema");
358+
Assert.assertFalse(ModelUtils.isMapSchema(objectSchema));
356359
}
357360

358361
// 3.1 spec tests
@@ -392,7 +395,7 @@ public void test31Schemas() {
392395
Assert.assertTrue(ModelUtils.isAnyOf(anyof2));
393396

394397
Schema objectSchema = ModelUtils.getSchema(openAPI, "ObjectSchema");
395-
Assert.assertTrue(ModelUtils.isMapSchema(objectSchema));
398+
Assert.assertFalse(ModelUtils.isMapSchema(objectSchema));
396399

397400
Schema complexComposedSchema = ModelUtils.getSchema(openAPI, "ComplexComposedSchema");
398401
Assert.assertTrue(ModelUtils.isComplexComposedSchema(complexComposedSchema));
@@ -641,4 +644,35 @@ public void isUnsupportedSchemaTest() {
641644
assertFalse(ModelUtils.isUnsupportedSchema(openAPI, (Schema) property2.getProperties().get("condition")));
642645
assertFalse(ModelUtils.isUnsupportedSchema(openAPI, (Schema) property2.getProperties().get("purpose")));
643646
}
647+
648+
@Test
649+
public void testModelWithPropertiesOnly() {
650+
// Schema with no properties
651+
Schema testSchema = new ObjectSchema().properties(null);
652+
assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema));
653+
654+
// Schema with properties
655+
testSchema.setProperties(Map.of("foo", new Schema()));
656+
assertTrue(ModelUtils.isModelWithPropertiesOnly(testSchema));
657+
658+
// Explicitly no additional properties
659+
testSchema.setAdditionalProperties(false);
660+
assertTrue(ModelUtils.isModelWithPropertiesOnly(testSchema));
661+
662+
// With additional properties
663+
testSchema.setAdditionalProperties(true);
664+
assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema));
665+
666+
// Additional properties is a schema set to false
667+
testSchema.setAdditionalProperties(new Schema().booleanSchemaValue(false));
668+
assertTrue(ModelUtils.isModelWithPropertiesOnly(testSchema));
669+
670+
// Additional properties is a schema set to true
671+
testSchema.setAdditionalProperties(new Schema().booleanSchemaValue(true));
672+
assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema));
673+
674+
// Additional properties is a custom schema
675+
testSchema.setAdditionalProperties(new Schema().type("string"));
676+
assertFalse(ModelUtils.isModelWithPropertiesOnly(testSchema));
677+
}
644678
}

modules/openapi-generator/src/test/resources/3_0/schema.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@ components:
6565
oneOf:
6666
- type: string
6767
- type: integer
68+
ObjectSchema:
69+
type: object
70+
additionalProperties: false
71+
properties:
72+
name:
73+
type: string
74+
address:
75+
type: string
6876
anyof1:
6977
anyOf:
7078
- type: string

0 commit comments

Comments
 (0)