Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix patternproperties handling in SpecFilter #4818

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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 @@ -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