From c6e6403897f7b804f730c38631e1d21a11c0f3ec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hendrik=20Wei=C3=9Fbrodt?= Date: Tue, 7 Jan 2025 15:33:20 +0100 Subject: [PATCH] fix patternproperties handling in SpecFilter. --- .../io/swagger/v3/core/filter/SpecFilter.java | 26 +++++++++++++++++++ .../v3/core/filter/SpecFilterTest.java | 3 ++- .../specFiles/3.1.0/composed-schema-3.1.json | 8 ++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/modules/swagger-core/src/main/java/io/swagger/v3/core/filter/SpecFilter.java b/modules/swagger-core/src/main/java/io/swagger/v3/core/filter/SpecFilter.java index e0f15c8679..d4bf2a9c47 100755 --- a/modules/swagger-core/src/main/java/io/swagger/v3/core/filter/SpecFilter.java +++ b/modules/swagger-core/src/main/java/io/swagger/v3/core/filter/SpecFilter.java @@ -255,6 +255,19 @@ protected Map filterComponentsSchema(OpenAPISpecFilter filter, M } } + Map 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 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; @@ -269,6 +282,12 @@ protected Map 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) { @@ -304,6 +323,13 @@ private void addSchemaRef(Schema schema, Set 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); diff --git a/modules/swagger-core/src/test/java/io/swagger/v3/core/filter/SpecFilterTest.java b/modules/swagger-core/src/test/java/io/swagger/v3/core/filter/SpecFilterTest.java index 355d4722e2..9ec9e786d1 100644 --- a/modules/swagger-core/src/test/java/io/swagger/v3/core/filter/SpecFilterTest.java +++ b/modules/swagger-core/src/test/java/io/swagger/v3/core/filter/SpecFilterTest.java @@ -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 diff --git a/modules/swagger-core/src/test/resources/specFiles/3.1.0/composed-schema-3.1.json b/modules/swagger-core/src/test/resources/specFiles/3.1.0/composed-schema-3.1.json index 3fbe926a7e..32057f5573 100644 --- a/modules/swagger-core/src/test/resources/specFiles/3.1.0/composed-schema-3.1.json +++ b/modules/swagger-core/src/test/resources/specFiles/3.1.0/composed-schema-3.1.json @@ -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": { @@ -37,6 +40,11 @@ } ] } + }, + "patternProperties": { + "somePattern": { + "$ref": "#/components/schemas/PatternPropertiesReferencedObject" + } } } }