Skip to content

Commit

Permalink
fix patternproperties handling in SpecFilter.
Browse files Browse the repository at this point in the history
  • Loading branch information
HWeissbrodt committed Jan 7, 2025
1 parent 1eb2774 commit c6e6403
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,19 @@ protected Map<String, Schema> filterComponentsSchema(OpenAPISpecFilter filter, M
}
}

Map<String, Schema> clonedPatternProperties = new LinkedHashMap<>();
if (filteredDefinition.get().getPatternProperties() != null) {
for (Object propName : filteredDefinition.get().getPatternProperties().keySet()) {
Schema property = (Schema) filteredDefinition.get().getPatternProperties().get(propName);
if (property != null) {
Optional<Schema> filteredProperty = filter.filterSchemaProperty(property, definition, (String) propName, params, cookies, headers);
if (filteredProperty.isPresent()) {
clonedPatternProperties.put((String) propName, filteredProperty.get());
}
}
}
}

try {
// TODO solve this, and generally handle clone and passing references
Schema clonedModel;
Expand All @@ -269,6 +282,12 @@ protected Map<String, Schema> filterComponentsSchema(OpenAPISpecFilter filter, M
if (!clonedProperties.isEmpty()) {
clonedModel.setProperties(clonedProperties);
}
if(clonedModel.getPatternProperties() != null) {
clonedModel.getPatternProperties().clear();
}
if(!clonedPatternProperties.isEmpty()) {
clonedModel.setPatternProperties(clonedPatternProperties);
}
clonedComponentsSchema.put(key, clonedModel);

} catch (IOException e) {
Expand Down Expand Up @@ -304,6 +323,13 @@ private void addSchemaRef(Schema schema, Set<String> referencedDefinitions) {
addSchemaRef((Schema)schema.getAdditionalProperties(), referencedDefinitions);
}

if (schema.getPatternProperties() != null) {
for (Object propName : schema.getPatternProperties().keySet()) {
Schema property = (Schema) schema.getPatternProperties().get(propName);
addSchemaRef(property, referencedDefinitions);
}
}

if (schema instanceof ArraySchema &&
((ArraySchema) schema).getItems() != null) {
addSchemaRef(((ArraySchema) schema).getItems(), referencedDefinitions);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,10 +297,11 @@ public void shouldRemoveBrokenNestedRefsKeepComposedSchemas() throws IOException
final RemoveUnreferencedDefinitionsFilter remover = new RemoveUnreferencedDefinitionsFilter();
final OpenAPI filtered = new SpecFilter().filter(openAPI, remover, null, null, null);

assertEquals(filtered.getComponents().getSchemas().size(), 4, "Expected to have parent and abstract child with both implementations schemas");
assertEquals(filtered.getComponents().getSchemas().size(), 5, "Expected to have parent and abstract child with both implementations schemas");
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChild1ImplObject"), "Schemas should contains child 1 implementation");
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChild2ImplObject"), "Schemas should contains child 2 implementation");
assertTrue(filtered.getComponents().getSchemas().containsKey("SomeChildObject"), "Schemas should contains child abstract parent");
assertTrue(filtered.getComponents().getSchemas().containsKey("PatternPropertiesReferencedObject"), "Schemas should contains pattern properties referenced schema");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"SomeChildObject": {
"description": "Some child object"
},
"PatternPropertiesReferencedObject": {
"description": "An object schema with is ONLY referenced via pattern properties"
},
"SomeParentObject": {
"description": "Some parent object",
"properties": {
Expand All @@ -37,6 +40,11 @@
}
]
}
},
"patternProperties": {
"somePattern": {
"$ref": "#/components/schemas/PatternPropertiesReferencedObject"
}
}
}
}
Expand Down

0 comments on commit c6e6403

Please sign in to comment.