From 2e4f426adccff8b544a52726658d55d15d2b740e Mon Sep 17 00:00:00 2001 From: Timon Back Date: Fri, 2 Feb 2024 14:46:08 +0100 Subject: [PATCH] feat(core): remove SwaggerSchemaPostProcessor (replaced by SwaggerSchemaUtil) --- .../SpringwolfAutoConfiguration.java | 8 ---- .../SwaggerSchemaPostProcessor.java | 39 ------------------- .../schemas/DefaultSchemasServiceTest.java | 6 +-- .../schemas/complex-definitions.json | 5 ++- .../schemas/documented-definitions.json | 13 ++++++- 5 files changed, 16 insertions(+), 55 deletions(-) delete mode 100644 springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/SwaggerSchemaPostProcessor.java diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/SpringwolfAutoConfiguration.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/SpringwolfAutoConfiguration.java index 90479eaa1..a8969c49f 100644 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/SpringwolfAutoConfiguration.java +++ b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/SpringwolfAutoConfiguration.java @@ -24,7 +24,6 @@ import io.github.stavshamir.springwolf.schemas.postprocessor.AvroSchemaPostProcessor; import io.github.stavshamir.springwolf.schemas.postprocessor.ExampleGeneratorPostProcessor; import io.github.stavshamir.springwolf.schemas.postprocessor.SchemasPostProcessor; -import io.github.stavshamir.springwolf.schemas.postprocessor.SwaggerSchemaPostProcessor; import io.swagger.v3.core.converter.ModelConverter; import org.springframework.boot.autoconfigure.AutoConfiguration; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; @@ -118,13 +117,6 @@ public ExampleGeneratorPostProcessor exampleGeneratorPostProcessor(ExampleGenera return new ExampleGeneratorPostProcessor(exampleGenerator); } - @Bean - @ConditionalOnMissingBean - @Order(100) - public SwaggerSchemaPostProcessor swaggerSchemaPostProcessor() { - return new SwaggerSchemaPostProcessor(); - } - @Bean @ConditionalOnMissingBean public ExampleGenerator exampleGenerator() { diff --git a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/SwaggerSchemaPostProcessor.java b/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/SwaggerSchemaPostProcessor.java deleted file mode 100644 index 858f966e5..000000000 --- a/springwolf-core/src/main/java/io/github/stavshamir/springwolf/schemas/postprocessor/SwaggerSchemaPostProcessor.java +++ /dev/null @@ -1,39 +0,0 @@ -// SPDX-License-Identifier: Apache-2.0 -package io.github.stavshamir.springwolf.schemas.postprocessor; - -import io.swagger.v3.oas.models.media.Schema; - -import java.util.Map; -import java.util.Set; - -public class SwaggerSchemaPostProcessor implements SchemasPostProcessor { - @Override - public void process(Schema schema, Map definitions) { - fixOpenApiSchemaDiscrepancies(schema); - removeAdditionalProperties(schema); - - Map properties = schema.getProperties(); - if (properties != null) { - properties.values().forEach((property) -> process(property, definitions)); - } - } - - private void fixOpenApiSchemaDiscrepancies(Schema schema) { - if (schema.getType() != null) { - schema.setTypes(Set.of(schema.getType())); - } - - if (schema.getExclusiveMinimum() != null) { - schema.setExclusiveMinimumValue(schema.getMinimum()); - schema.setExclusiveMinimum(null); - } - if (schema.getExclusiveMaximum() != null) { - schema.setExclusiveMaximumValue(schema.getMaximum()); - schema.setExclusiveMaximum(null); - } - } - - private void removeAdditionalProperties(Schema schema) { - schema.setAdditionalProperties(null); - } -} diff --git a/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/DefaultSchemasServiceTest.java b/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/DefaultSchemasServiceTest.java index a8a035cfc..6975f2f53 100644 --- a/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/DefaultSchemasServiceTest.java +++ b/springwolf-core/src/test/java/io/github/stavshamir/springwolf/schemas/DefaultSchemasServiceTest.java @@ -12,7 +12,6 @@ import io.github.stavshamir.springwolf.schemas.example.ExampleJsonGenerator; import io.github.stavshamir.springwolf.schemas.postprocessor.ExampleGeneratorPostProcessor; import io.github.stavshamir.springwolf.schemas.postprocessor.SchemasPostProcessor; -import io.github.stavshamir.springwolf.schemas.postprocessor.SwaggerSchemaPostProcessor; import io.swagger.v3.core.util.Json; import io.swagger.v3.oas.annotations.media.Schema; import jakarta.annotation.Nullable; @@ -42,10 +41,7 @@ class DefaultSchemasServiceTest { private final SchemasPostProcessor schemasPostProcessor = Mockito.mock(SchemasPostProcessor.class); private final ComponentsService componentsService = new DefaultComponentsService( List.of(), - List.of( - new ExampleGeneratorPostProcessor(new ExampleJsonGenerator()), - schemasPostProcessor, - new SwaggerSchemaPostProcessor()), + List.of(new ExampleGeneratorPostProcessor(new ExampleJsonGenerator()), schemasPostProcessor), new SwaggerSchemaUtil(), new SpringwolfConfigProperties()); diff --git a/springwolf-core/src/test/resources/schemas/complex-definitions.json b/springwolf-core/src/test/resources/schemas/complex-definitions.json index a8faac1ae..bec4392a1 100644 --- a/springwolf-core/src/test/resources/schemas/complex-definitions.json +++ b/springwolf-core/src/test/resources/schemas/complex-definitions.json @@ -93,7 +93,10 @@ } }, "nmfm" : { - "type" : "object" + "type" : "object", + "additionalProperties" : { + "$ref" : "#/components/schemas/MyClass" + } }, "ns" : { "type" : "string" diff --git a/springwolf-core/src/test/resources/schemas/documented-definitions.json b/springwolf-core/src/test/resources/schemas/documented-definitions.json index a8be8e651..7fdb2ecd6 100644 --- a/springwolf-core/src/test/resources/schemas/documented-definitions.json +++ b/springwolf-core/src/test/resources/schemas/documented-definitions.json @@ -26,11 +26,20 @@ "description" : "Map with example", "examples" : [ { "key1" : "value1" - } ] + } ], + "additionalProperties" : { + "type" : "string", + "description" : "Map with example", + "examples" : [ "{\"key1\":\"value1\"}" ] + } }, "mss_plain" : { "type" : "object", - "description" : "Map without example" + "description" : "Map without example", + "additionalProperties" : { + "type" : "string", + "description" : "Map without example" + } }, "s" : { "type" : "string",