[3.1] Fixed lack of check for booleanSchemaValue (#21742)

* [3.1] Fixed lack of check for ´booleanSchemaValue`

* [3.1] Fixed `isMapSchema`
This commit is contained in:
Eduardo Menges Mattje 2025-09-01 03:06:27 -03:00 committed by GitHub
parent eae5088f7c
commit 27d3c6f326
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 4 deletions

View File

@ -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 ||

View File

@ -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));
}
}

View File

@ -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