Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,9 @@ public static boolean isMapSchema(Schema schema) {
}

// 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;
}

Expand Down Expand Up @@ -808,7 +810,13 @@ public static boolean isModelWithPropertiesOnly(Schema schema) {
(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) {
Expand Down Expand Up @@ -2415,7 +2423,7 @@ public static void copyMetadata(Schema from, Schema to) {
* 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 ||
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ public void test30Schemas() {
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
Expand Down Expand Up @@ -392,7 +395,7 @@ public void test31Schemas() {
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));
Expand Down Expand Up @@ -585,4 +588,35 @@ public void isUnsupportedSchemaTest() {
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));
}
}
8 changes: 8 additions & 0 deletions modules/openapi-generator/src/test/resources/3_0/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading