diff --git a/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java b/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java index 77c1df721..4a5728a72 100644 --- a/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java +++ b/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java @@ -350,16 +350,15 @@ private void debugModel(String source, OpenAPI model) { return; } ApiLogging.logger.addingModel(source); - debugMap("callbacks", source, Optional.ofNullable(model.getComponents()).map(Components::getCallbacks)); - debugMap("examples", source, Optional.ofNullable(model.getComponents()).map(Components::getExamples)); - debugMap("headers", source, Optional.ofNullable(model.getComponents()).map(Components::getHeaders)); - debugMap("links", source, Optional.ofNullable(model.getComponents()).map(Components::getLinks)); - debugMap("parameters", source, Optional.ofNullable(model.getComponents()).map(Components::getParameters)); - debugMap("request bodies", source, Optional.ofNullable(model.getComponents()).map(Components::getRequestBodies)); - debugMap("responses", source, Optional.ofNullable(model.getComponents()).map(Components::getResponses)); - debugMap("schemas", source, Optional.ofNullable(model.getComponents()).map(Components::getSchemas)); - debugMap("security schemes", source, - Optional.ofNullable(model.getComponents()).map(Components::getSecuritySchemes)); + debugMap("callbacks", source, getMap(model, Components::getCallbacks)); + debugMap("examples", source, getMap(model, Components::getExamples)); + debugMap("headers", source, getMap(model, Components::getHeaders)); + debugMap("links", source, getMap(model, Components::getLinks)); + debugMap("parameters", source, getMap(model, Components::getParameters)); + debugMap("request bodies", source, getMap(model, Components::getRequestBodies)); + debugMap("responses", source, getMap(model, Components::getResponses)); + debugMap("schemas", source, getMap(model, Components::getSchemas)); + debugMap("security schemes", source, getMap(model, Components::getSecuritySchemes)); debugList("servers", source, Optional.ofNullable(model.getServers())); debugMap("path items", source, Optional.ofNullable(model.getPaths()).map(Paths::getPathItems)); debugList("security", source, Optional.ofNullable(model.getSecurity())); @@ -367,7 +366,11 @@ private void debugModel(String source, OpenAPI model) { debugMap("extensions", source, Optional.ofNullable(model.getExtensions())); } - private void debugMap(String name, String source, Optional> collection) { + private Optional> getMap(OpenAPI model, Function> extract) { + return Optional.ofNullable(model.getComponents()).map(extract); + } + + private void debugMap(String name, String source, Optional> collection) { debugModel(name, source, collection.map(Map::size)); } diff --git a/core/src/test/java/io/smallrye/openapi/runtime/OpenApiProcessorTest.java b/core/src/test/java/io/smallrye/openapi/runtime/OpenApiProcessorTest.java index 7201817d7..4942dbcde 100644 --- a/core/src/test/java/io/smallrye/openapi/runtime/OpenApiProcessorTest.java +++ b/core/src/test/java/io/smallrye/openapi/runtime/OpenApiProcessorTest.java @@ -13,6 +13,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +@Deprecated class OpenApiProcessorTest { ClassLoader loader; diff --git a/core/src/test/java/io/smallrye/openapi/runtime/scanner/IndexScannerTestBase.java b/core/src/test/java/io/smallrye/openapi/runtime/scanner/IndexScannerTestBase.java index 5ab7f7df4..bb56a1908 100644 --- a/core/src/test/java/io/smallrye/openapi/runtime/scanner/IndexScannerTestBase.java +++ b/core/src/test/java/io/smallrye/openapi/runtime/scanner/IndexScannerTestBase.java @@ -19,6 +19,7 @@ import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.config.spi.ConfigSource; +import org.eclipse.microprofile.openapi.OASFactory; import org.eclipse.microprofile.openapi.models.OpenAPI; import org.eclipse.microprofile.openapi.models.Operation; import org.eclipse.microprofile.openapi.models.PathItem; @@ -35,12 +36,9 @@ import io.smallrye.config.PropertiesConfigSource; import io.smallrye.config.SmallRyeConfigBuilder; import io.smallrye.openapi.api.OpenApiConfig; -import io.smallrye.openapi.api.models.ComponentsImpl; -import io.smallrye.openapi.api.models.OpenAPIImpl; +import io.smallrye.openapi.api.SmallRyeOpenAPI; import io.smallrye.openapi.api.models.OperationImpl; import io.smallrye.openapi.api.models.parameters.ParameterImpl; -import io.smallrye.openapi.runtime.io.Format; -import io.smallrye.openapi.runtime.io.OpenApiSerializer; public class IndexScannerTestBase { @@ -113,14 +111,27 @@ protected static DotName componentize(String className) { return name; } - public static void printToConsole(String entityName, Schema schema) throws IOException { + public static void printToConsole(String entityName, Schema schema) { // Remember to set debug level logging. LOG.debug(schemaToString(entityName, schema)); } - public static void printToConsole(OpenAPI oai) throws IOException { + public static void printToConsole(OpenAPI oai) { // Remember to set debug level logging. - LOG.debug(OpenApiSerializer.serialize(oai, Format.JSON)); + LOG.debug(toJSON(oai)); + } + + public static String toJSON(OpenAPI oai) { + return SmallRyeOpenAPI.builder() + .withConfig(config(Collections.emptyMap())) + .withInitialModel(oai) + .defaultRequiredProperties(false) + .enableModelReader(false) + .enableStandardStaticFiles(false) + .enableAnnotationScan(false) + .enableStandardFilter(false) + .build() + .toJSON(); } public static void verifyMethodAndParamRefsPresent(OpenAPI oai) { @@ -156,14 +167,10 @@ private static boolean isPathMatrixObject(Parameter parameter) { && parameter.getSchema() != null && parameter.getSchema().getType() == Schema.SchemaType.OBJECT; } - public static String schemaToString(String entityName, Schema schema) throws IOException { - Map map = new HashMap<>(); - map.put(entityName, schema); - OpenAPIImpl oai = new OpenAPIImpl(); - ComponentsImpl comp = new ComponentsImpl(); - comp.setSchemas(map); - oai.setComponents(comp); - return OpenApiSerializer.serialize(oai, Format.JSON); + public static String schemaToString(String entityName, Schema schema) { + return toJSON(OASFactory.createOpenAPI() + .components(OASFactory.createComponents() + .addSchema(entityName, schema))); } public static void assertJsonEquals(String entityName, String expectedResource, Schema actual) @@ -178,17 +185,42 @@ public static void assertJsonEquals(String expectedResource, OpenAPI actual) thr } public static void assertJsonEquals(URL expectedResourceUrl, OpenAPI actual) throws JSONException, IOException { - JSONAssert.assertEquals(loadResource(expectedResourceUrl), OpenApiSerializer.serialize(actual, Format.JSON), - true); + JSONAssert.assertEquals(loadResource(expectedResourceUrl), toJSON(actual), true); + } + + public static OpenAPI scan(Class... classes) { + return scan(config(Collections.emptyMap()), null, classes); + } + + public static OpenAPI scan(InputStream customStaticFile, Class... classes) { + return scan(config(Collections.emptyMap()), customStaticFile, classes); + } + + public static OpenAPI scan(Config config, InputStream customStaticFile, Class... classes) { + return scan(config, false, customStaticFile, classes); + } + + public static OpenAPI scan(Config config, Class... classes) { + return scan(config, false, null, classes); } - public static void assertJsonEquals(String expectedResource, Class... classes) - throws IOException, JSONException { + public static OpenAPI scan(Config config, boolean defaultRequiredProperties, InputStream customStaticFile, Class... classes) { Index index = indexOf(classes); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(dynamicConfig(new HashMap()), index); - OpenAPI result = scanner.scan(); + + OpenAPI result = SmallRyeOpenAPI.builder() + .defaultRequiredProperties(defaultRequiredProperties) + .withCustomStaticFile(customStaticFile) + .withIndex(index) + .withConfig(config) + .build() + .model(); + printToConsole(result); - assertJsonEquals(expectedResource, result); + return result; + } + + public static void assertJsonEquals(String expectedResource, Class... classes) throws IOException, JSONException { + assertJsonEquals(expectedResource, scan(config(Collections.emptyMap()), null, classes)); } public static String loadResource(URL testResource) throws IOException { @@ -223,10 +255,16 @@ public static OpenApiConfig dynamicConfig(Map properties) { return OpenApiConfig.fromConfig(config(properties)); } + public static Config config(String key, Object value) { + Map config = new HashMap<>(1); + config.put(key, value.toString()); + return config(config); + } + public static Config config(Map properties) { return new SmallRyeConfigBuilder() + .addDefaultSources() .withSources(new PropertiesConfigSource(properties, "unit-test", ConfigSource.DEFAULT_ORDINAL)) .build(); } - } diff --git a/core/src/test/java/io/smallrye/openapi/runtime/scanner/PropertyNamingStrategyTest.java b/core/src/test/java/io/smallrye/openapi/runtime/scanner/PropertyNamingStrategyTest.java index 57693acd6..dbaacf75e 100644 --- a/core/src/test/java/io/smallrye/openapi/runtime/scanner/PropertyNamingStrategyTest.java +++ b/core/src/test/java/io/smallrye/openapi/runtime/scanner/PropertyNamingStrategyTest.java @@ -9,16 +9,15 @@ import java.util.Set; import java.util.TreeSet; +import org.eclipse.microprofile.config.Config; import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.models.OpenAPI; -import org.jboss.jandex.Index; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.CsvSource; import com.fasterxml.jackson.databind.annotation.JsonNaming; -import io.smallrye.openapi.api.OpenApiConfig; import io.smallrye.openapi.api.constants.JsonbConstants; import io.smallrye.openapi.api.constants.OpenApiConstants; import io.smallrye.openapi.runtime.OpenApiRuntimeException; @@ -27,69 +26,47 @@ class PropertyNamingStrategyTest extends IndexScannerTestBase { @Test void testSnakeCase() throws Exception { - Index index = indexOf(NameStrategyBean1.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner( - dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, + OpenAPI result = scan(config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, "com.fasterxml.jackson.databind.PropertyNamingStrategies$SnakeCaseStrategy"), - index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); + NameStrategyBean1.class); assertJsonEquals("components.schemas.name-strategy-snake.json", result); } @Test void testJacksonNamingIgnoresConfig() throws Exception { - Index index = indexOf(NameStrategyBean2.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner( - dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, + OpenAPI result = scan(config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, "com.fasterxml.jackson.databind.PropertyNamingStrategies$SnakeCaseStrategy"), - index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); + NameStrategyBean2.class); assertJsonEquals("components.schemas.name-strategy-ignored.json", result); } @Test void testJacksonNamingOverridesConfig() throws Exception { - Index index = indexOf(NameStrategyKebab.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner( - dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, + OpenAPI result = scan(config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, "com.fasterxml.jackson.databind.PropertyNamingStrategies$SnakeCaseStrategy"), - index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); + NameStrategyKebab.class); assertJsonEquals("components.schemas.name-strategy-kebab.json", result); } @Test void testInvalidNamingStrategyClass() throws Exception { - Index index = indexOf(NameStrategyKebab.class); - OpenApiConfig config = dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, + Config config = config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, "com.fasterxml.jackson.databind.PropertyNamingStrategies$InvalidStrategy"); - assertThrows(OpenApiRuntimeException.class, () -> new OpenApiAnnotationScanner(config, index)); + assertThrows(OpenApiRuntimeException.class, () -> scan(config, NameStrategyKebab.class)); } @Test void testNoValidTranslationMethods() throws Exception { - Index index = indexOf(NameStrategyKebab.class); - OpenApiConfig config = dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, - NoValidTranslationMethods.class.getName()); - assertThrows(OpenApiRuntimeException.class, () -> new OpenApiAnnotationScanner(config, index)); + Config config = config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, + NoValidTranslationMethods.class.getName()); + assertThrows(OpenApiRuntimeException.class, () -> scan(config, NameStrategyKebab.class)); } @Test void testInvalidPropertyNameTranslationAttempt() throws Exception { - Index index = indexOf(NameStrategyBean3.class); - OpenApiConfig config = dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, + Config config = config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, TranslationThrowsException.class.getName()); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(config, index); - assertThrows(OpenApiRuntimeException.class, () -> scanner.scan()); + assertThrows(OpenApiRuntimeException.class, () -> scan(config, NameStrategyBean3.class)); } @ParameterizedTest(name = "testJsonbConstantStrategy-{0}") @@ -102,14 +79,7 @@ void testInvalidPropertyNameTranslationAttempt() throws Exception { JsonbConstants.CASE_INSENSITIVE + ", simpleStringOne|anotherField|Y|z" }) void testJsonbConstantStrategy(String strategy, String expectedNames) throws Exception { - Index index = indexOf(NameStrategyBean3.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner( - dynamicConfig(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, strategy), - index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); + OpenAPI result = scan(config(OpenApiConstants.SMALLRYE_PROPERTY_NAMING_STRATEGY, strategy), NameStrategyBean3.class); Set expectedNameSet = new TreeSet<>(Arrays.asList(expectedNames.split("\\|"))); Map schemas = result.getComponents().getSchemas(); org.eclipse.microprofile.openapi.models.media.Schema schema = schemas.get(NameStrategyBean3.class.getSimpleName()); diff --git a/core/src/test/java/io/smallrye/openapi/runtime/scanner/SchemaPropertyNegativeTest.java b/core/src/test/java/io/smallrye/openapi/runtime/scanner/SchemaPropertyNegativeTest.java index 78842f8f3..35f48d0bb 100644 --- a/core/src/test/java/io/smallrye/openapi/runtime/scanner/SchemaPropertyNegativeTest.java +++ b/core/src/test/java/io/smallrye/openapi/runtime/scanner/SchemaPropertyNegativeTest.java @@ -8,8 +8,6 @@ import org.eclipse.microprofile.openapi.annotations.enums.SchemaType; import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.annotations.media.SchemaProperty; -import org.eclipse.microprofile.openapi.models.OpenAPI; -import org.jboss.jandex.Index; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestInfo; @@ -27,13 +25,7 @@ public void beforeEach(TestInfo testInfo) { @Test void testClassSchemaPropertyBlankName() throws Exception { - Index index = indexOf(BlankNameTest.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-blankname.json", result); + assertJsonEquals("components.schemas.schemaproperty-blankname.json", BlankNameTest.class); } @@ -44,13 +36,7 @@ static class BlankNameTest { @Test void testClassSchemaPropertyDuplicateName() throws Exception { - Index index = indexOf(DuplicateNameTest.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-duplicatename.json", result); + assertJsonEquals("components.schemas.schemaproperty-duplicatename.json", DuplicateNameTest.class); } // Last instance replaces others, instances are not merged @@ -61,13 +47,7 @@ static class DuplicateNameTest { @Test void testClassSchemaPropertyNegativeMultipleOf() throws Exception { - Index index = indexOf(NegativeMultipleOf.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-negativemultipleof.json", result); + assertJsonEquals("components.schemas.schemaproperty-negativemultipleof.json", NegativeMultipleOf.class); } @Schema(properties = { @SchemaProperty(name = "test", type = SchemaType.INTEGER, multipleOf = -2) }) @@ -76,13 +56,7 @@ static class NegativeMultipleOf { @Test void testClassSchemaPropertyMaximumNotNumber() throws Exception { - Index index = indexOf(MaximumNotNumber.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-maximumnotnumber.json", result); + assertJsonEquals("components.schemas.schemaproperty-maximumnotnumber.json", MaximumNotNumber.class); } // maximum should be ignored @@ -92,13 +66,7 @@ static class MaximumNotNumber { @Test void testClassSchemaPropertyMinimumNotNumber() throws Exception { - Index index = indexOf(MinimumNotNumber.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-minimumnotnumber.json", result); + assertJsonEquals("components.schemas.schemaproperty-minimumnotnumber.json", MinimumNotNumber.class); } // minimum should be ignored @@ -108,13 +76,7 @@ static class MinimumNotNumber { @Test void testClassSchemaPropertyMinLengthNegative() throws Exception { - Index index = indexOf(MinLengthNegative.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-minlengthnegative.json", result); + assertJsonEquals("components.schemas.schemaproperty-minlengthnegative.json", MinLengthNegative.class); } // Negative value used in document @@ -124,13 +86,7 @@ static class MinLengthNegative { @Test void testClassSchemaPropertyMaxLengthNegative() throws Exception { - Index index = indexOf(MaxLengthNegative.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-maxlengthnegative.json", result); + assertJsonEquals("components.schemas.schemaproperty-maxlengthnegative.json", MaxLengthNegative.class); } // Negative value used in document @@ -140,13 +96,7 @@ static class MaxLengthNegative { @Test void testClassSchemaPropertyPatternInvalid() throws Exception { - Index index = indexOf(PatternInvalid.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-patterninvalid.json", result); + assertJsonEquals("components.schemas.schemaproperty-patterninvalid.json", PatternInvalid.class); } // Invalid pattern used in document @@ -156,13 +106,7 @@ static class PatternInvalid { @Test void testClassSchemaPropertyMaxPropertiesNegative() throws Exception { - Index index = indexOf(MaxPropertiesNegative.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-maxpropertiesnegative.json", result); + assertJsonEquals("components.schemas.schemaproperty-maxpropertiesnegative.json", MaxPropertiesNegative.class); } // Negative value used in document @@ -172,13 +116,7 @@ static class MaxPropertiesNegative { @Test void testClassSchemaPropertyMinPropertiesNegative() throws Exception { - Index index = indexOf(MinPropertiesNegative.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-minpropertiesnegative.json", result); + assertJsonEquals("components.schemas.schemaproperty-minpropertiesnegative.json", MinPropertiesNegative.class); } // Negative value used in document @@ -188,13 +126,7 @@ static class MinPropertiesNegative { @Test void testClassSchemaPropertyRefWithOtherProps() throws Exception { - Index index = indexOf(RefWithOtherProps.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-refwithotherprops.json", result); + assertJsonEquals("components.schemas.schemaproperty-refwithotherprops.json", RefWithOtherProps.class); } // Name and ref used in document, other attributes ignored @@ -204,13 +136,7 @@ static class RefWithOtherProps { @Test void testClassSchemaPropertyDefaultValueWrongType() throws Exception { - Index index = indexOf(DefaultValueWrongType.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-defaultvaluewrongtype.json", result); + assertJsonEquals("components.schemas.schemaproperty-defaultvaluewrongtype.json", DefaultValueWrongType.class); } // Invalid default value used in document @@ -220,13 +146,7 @@ static class DefaultValueWrongType { @Test void testClassSchemaPropertyMaxItemsNegative() throws Exception { - Index index = indexOf(MaxItemsNegative.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-maxitemsnegative.json", result); + assertJsonEquals("components.schemas.schemaproperty-maxitemsnegative.json", MaxItemsNegative.class); } // Negative value used in document @@ -236,13 +156,7 @@ static class MaxItemsNegative { @Test void testClassSchemaPropertyMinItemsNegative() throws Exception { - Index index = indexOf(MinItemsNegative.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-minitemsnegative.json", result); + assertJsonEquals("components.schemas.schemaproperty-minitemsnegative.json", MinItemsNegative.class); } // Negative value used in document @@ -256,13 +170,7 @@ static class MissingClass { @Test void testClassSchemaPropertyImplementationMissing() throws Exception { - Index index = indexOf(ImplementationMissing.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-implementationmissing.json", result); + assertJsonEquals("components.schemas.schemaproperty-implementationmissing.json", ImplementationMissing.class); } // Implementation attribute not included in document @@ -272,13 +180,7 @@ static class ImplementationMissing { @Test void testClassSchemaPropertyNotMissing() throws Exception { - Index index = indexOf(NotMissing.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-notmissing.json", result); + assertJsonEquals("components.schemas.schemaproperty-notmissing.json", NotMissing.class); String expectedMessage = String.format("Could not find schema class in index: %s", MissingClass.class.getName()); LogRecord record = logs.assertLogContaining(expectedMessage); @@ -292,13 +194,7 @@ static class NotMissing { @Test void testClassSchemaPropertyOneOfMissing() throws Exception { - Index index = indexOf(OneOfMissing.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-oneofmissing.json", result); + assertJsonEquals("components.schemas.schemaproperty-oneofmissing.json", OneOfMissing.class); String expectedMessage = String.format("Could not find schema class in index: %s", MissingClass.class.getName()); LogRecord record = logs.assertLogContaining(expectedMessage); @@ -312,13 +208,7 @@ static class OneOfMissing { @Test void testClassSchemaPropertyAnyOfMissing() throws Exception { - Index index = indexOf(AnyOfMissing.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-anyofmissing.json", result); + assertJsonEquals("components.schemas.schemaproperty-anyofmissing.json", AnyOfMissing.class); String expectedMessage = String.format("Could not find schema class in index: %s", MissingClass.class.getName()); LogRecord record = logs.assertLogContaining(expectedMessage); @@ -332,13 +222,7 @@ static class AnyOfMissing { @Test void testClassSchemaPropertyAllOfMissing() throws Exception { - Index index = indexOf(AllOfMissing.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.schemaproperty-allofmissing.json", result); + assertJsonEquals("components.schemas.schemaproperty-allofmissing.json", AllOfMissing.class); String expectedMessage = String.format("Could not find schema class in index: %s", MissingClass.class.getName()); LogRecord record = logs.assertLogContaining(expectedMessage); diff --git a/core/src/test/java/io/smallrye/openapi/runtime/scanner/SchemaPropertyTest.java b/core/src/test/java/io/smallrye/openapi/runtime/scanner/SchemaPropertyTest.java index b1957ba44..bb18a8056 100644 --- a/core/src/test/java/io/smallrye/openapi/runtime/scanner/SchemaPropertyTest.java +++ b/core/src/test/java/io/smallrye/openapi/runtime/scanner/SchemaPropertyTest.java @@ -19,7 +19,7 @@ void testClassSchemaPropertyMergesWithFieldSchemas() throws Exception { SmallRyeOpenAPI result1 = SmallRyeOpenAPI.builder() .withConfig(config(Collections.emptyMap())) .withIndex(index) - .setDefaultRequiredProperties(false) + .defaultRequiredProperties(false) .build(); printToConsole(result1.model()); assertJsonEquals("components.schemas.schemaproperty-merge.json", result1.model()); diff --git a/core/src/test/java/io/smallrye/openapi/runtime/scanner/StandaloneSchemaScanTest.java b/core/src/test/java/io/smallrye/openapi/runtime/scanner/StandaloneSchemaScanTest.java index 4a6db1225..546c297d8 100644 --- a/core/src/test/java/io/smallrye/openapi/runtime/scanner/StandaloneSchemaScanTest.java +++ b/core/src/test/java/io/smallrye/openapi/runtime/scanner/StandaloneSchemaScanTest.java @@ -29,7 +29,6 @@ import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.annotations.media.SchemaProperty; import org.eclipse.microprofile.openapi.models.OpenAPI; -import org.jboss.jandex.Index; import org.json.JSONException; import org.junit.jupiter.api.Test; @@ -45,53 +44,28 @@ class StandaloneSchemaScanTest extends IndexScannerTestBase { @Test void testUnreferencedSchemasInComponents() throws Exception { - Index index = indexOf(Cat.class, Dog.class, Class.forName(getClass().getPackage().getName() + ".package-info")); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); + OpenAPI result = scan(Cat.class, Dog.class, Class.forName(getClass().getPackage().getName() + ".package-info")); assertJsonEquals("components.schemas.unreferenced.json", result); } @Test void testInheritanceAnyOf() throws Exception { - Index index = indexOf(Reptile.class, Lizard.class, Snake.class, Turtle.class, Alligator.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); + OpenAPI result = scan(Reptile.class, Lizard.class, Snake.class, Turtle.class, Alligator.class); assertJsonEquals("components.schemas.inheritance.json", result); - } @Test void testInheritanceAutomaticAnyOf() throws Exception { - Index index = indexOf(ReptileNoAllOf.class, LizardNoAllOf.class, SnakeNoAllOf.class, TurtleNoAllOf.class, - AlligatorNoAllOf.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner( - dynamicConfig(OpenApiConstants.AUTO_INHERITANCE, "BOTH"), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); + OpenAPI result = scan(config(OpenApiConstants.AUTO_INHERITANCE, "BOTH"), + ReptileNoAllOf.class, LizardNoAllOf.class, SnakeNoAllOf.class, TurtleNoAllOf.class, AlligatorNoAllOf.class); assertJsonEquals("components.schemas.inheritance.json", result); - } @Test void testInheritanceAutomaticAnyOfParentOnly() throws Exception { - Index index = indexOf(ReptileNoAllOf.class, LizardNoAllOf.class, SnakeNoAllOf.class, TurtleNoAllOf.class, - AlligatorNoAllOf.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner( - dynamicConfig(OpenApiConstants.AUTO_INHERITANCE, "PARENT_ONLY"), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); + OpenAPI result = scan(config(OpenApiConstants.AUTO_INHERITANCE, "PARENT_ONLY"), + ReptileNoAllOf.class, LizardNoAllOf.class, SnakeNoAllOf.class, TurtleNoAllOf.class, AlligatorNoAllOf.class); assertJsonEquals("components.schemas.inheritance-parent-only.json", result); - } /****************************************************************/ @@ -184,17 +158,12 @@ static class AlligatorNoAllOf extends ReptileNoAllOf { */ @Test void testRegisteredSchemaTypePreserved() throws IOException, JSONException { - Index index = indexOf(RegisteredSchemaTypePreservedModel.Animal.class, - RegisteredSchemaTypePreservedModel.AnimalListEnvelope.class, - RegisteredSchemaTypePreservedModel.MessageBase.class, - RegisteredSchemaTypePreservedModel.MessageData.class, - RegisteredSchemaTypePreservedModel.MessageDataItems.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.registered-schema-type-preserved.json", result); + assertJsonEquals("components.schemas.registered-schema-type-preserved.json", + RegisteredSchemaTypePreservedModel.Animal.class, + RegisteredSchemaTypePreservedModel.AnimalListEnvelope.class, + RegisteredSchemaTypePreservedModel.MessageBase.class, + RegisteredSchemaTypePreservedModel.MessageData.class, + RegisteredSchemaTypePreservedModel.MessageDataItems.class); } static class RegisteredSchemaTypePreservedModel { @@ -343,20 +312,14 @@ public int getCurrentItemCount() { */ @Test void testJavaxJaxbElementUnwrapped() throws IOException, JSONException { - Index index = indexOf(test.io.smallrye.openapi.runtime.scanner.javax.JAXBElementDto.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.jaxbelement-generic-type-unwrapped.json", result); + assertJsonEquals("components.schemas.jaxbelement-generic-type-unwrapped.json", + test.io.smallrye.openapi.runtime.scanner.javax.JAXBElementDto.class); } @Test void testJakartaJaxbElementUnwrapped() throws IOException, JSONException { - Index index = indexOf(test.io.smallrye.openapi.runtime.scanner.jakarta.JAXBElementDto.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.jaxbelement-generic-type-unwrapped.json", result); + assertJsonEquals("components.schemas.jaxbelement-generic-type-unwrapped.json", + test.io.smallrye.openapi.runtime.scanner.jakarta.JAXBElementDto.class); } /****************************************************************/ @@ -366,12 +329,9 @@ void testJakartaJaxbElementUnwrapped() throws IOException, JSONException { */ @Test void testJacksonJsonUnwrapped() throws IOException, JSONException { - Index index = indexOf(JacksonJsonPerson.class, JacksonJsonPersonWithPrefixedAddress.class, - JacksonJsonPersonWithSuffixedAddress.class, JacksonJsonAddress.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas-jackson-jsonunwrapped.json", result); + assertJsonEquals("components.schemas-jackson-jsonunwrapped.json", + JacksonJsonPerson.class, JacksonJsonPersonWithPrefixedAddress.class, + JacksonJsonPersonWithSuffixedAddress.class, JacksonJsonAddress.class); } @Schema @@ -417,22 +377,18 @@ static class JacksonJsonAddress { @Test void testNestedCollectionSchemas() throws IOException, JSONException { // Place the JDK classes in the index to simulate Quarkus - Index index = indexOf(CollectionBean.class, - EntryBean.class, - MultivaluedCollection.class, - MultivaluedMap.class, - // CustomMap.class excluded intentionally - Collection.class, - ArrayList.class, - HashMap.class, - List.class, - Map.class, - Set.class, - UUID.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.nested-parameterized-collection-types.json", result); + assertJsonEquals("components.schemas.nested-parameterized-collection-types.json", CollectionBean.class, + EntryBean.class, + MultivaluedCollection.class, + MultivaluedMap.class, + // CustomMap.class excluded intentionally + Collection.class, + ArrayList.class, + HashMap.class, + List.class, + Map.class, + Set.class, + UUID.class); } @Schema @@ -491,11 +447,7 @@ static class MultivaluedMap extends HashMap> { */ @Test void testNestedCustomGenericSchemas() throws IOException, JSONException { - Index index = indexOf(Foo.class, Generic0.class, Generic1.class, Generic2.class, CustomMap.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.nested-custom-generics.json", result); + assertJsonEquals("components.schemas.nested-custom-generics.json", Foo.class, Generic0.class, Generic1.class, Generic2.class, CustomMap.class); } /* @@ -544,11 +496,8 @@ class A { public Optional[] arrayOfOptionalB; public List> listOfOptionalB; } - Index index = indexOf(B.class, A.class, UUID.class, List.class, Optional.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.optional-arraytype.json", result); + + assertJsonEquals("components.schemas.optional-arraytype.json", B.class, A.class, UUID.class, List.class, Optional.class); } @Target(ElementType.TYPE_USE) @@ -573,11 +522,7 @@ class Sample { public char[] arrayFromType; } - Index index = indexOf(Sample.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.array-type-override.json", result); + assertJsonEquals("components.schemas.array-type-override.json", Sample.class); } /* @@ -585,11 +530,7 @@ class Sample { */ @Test void testSingleAnnotatedConstructorArgumentIgnored() throws IOException, JSONException { - Index index = indexOf(SingleAnnotatedConstructorArgument.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.annotated-constructor-arg-ignored.json", result); + assertJsonEquals("components.schemas.annotated-constructor-arg-ignored.json", SingleAnnotatedConstructorArgument.class); } /* @@ -613,11 +554,7 @@ class Bean { Pair pair; } - Index index = indexOf(Bean.class, Pair.class, Tuple.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.nonparameterized-ancestry-chain-link.json", result); + assertJsonEquals("components.schemas.nonparameterized-ancestry-chain-link.json", Bean.class, Pair.class, Tuple.class); } /* @@ -648,9 +585,7 @@ class Bean3 { Bean2 prop3; } - Index index = indexOf(Bean1.class, Bean2.class, Bean3.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); + OpenAPI result = scan(Bean1.class, Bean2.class, Bean3.class); assertTrue(result.getComponents().getSchemas().get("Bean1").getDeprecated()); assertNull(result.getComponents().getSchemas().get("Bean1").getProperties().get("prop1").getDeprecated()); @@ -694,11 +629,7 @@ class Bean { OtherBean third; } - Index index = indexOf(OtherBean.class, /* BeanTwo.class, BeanThree.class, */ Bean.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.field-overrides-type.json", result); + assertJsonEquals("components.schemas.field-overrides-type.json", OtherBean.class, /* BeanTwo.class, BeanThree.class, */ Bean.class); } /* @@ -725,11 +656,7 @@ class Bean { Collection anyArrayFromRawCollection; } - Index index = indexOf(Bean.class, StringArray.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.iterator-stream-map-types.json", result); + assertJsonEquals("components.schemas.iterator-stream-map-types.json", Bean.class, StringArray.class); } /** @@ -746,13 +673,7 @@ class ZonedDateTimeArrayWrapper { ZonedDateTime[] now; } - Index index = indexOf(ZonedDateTimeArrayWrapper.class, ZonedDateTime.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.terminal-array-item-registration.json", result); + assertJsonEquals("components.schemas.terminal-array-item-registration.json", ZonedDateTimeArrayWrapper.class, ZonedDateTime.class); } /* @@ -777,11 +698,7 @@ class Class1 { Class2 value; } - Index index = indexOf(Class1.class, Class2.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("components.schemas.no-self-ref-for-property-schema.json", result); + assertJsonEquals("components.schemas.no-self-ref-for-property-schema.json", Class1.class, Class2.class); } @Test @@ -801,16 +718,11 @@ class Bean { Nullable nullableString; } - Index index = indexOf(Nullable.class, Bean.class); String nullableStringArySig = Nullable.class.getName() + ""; - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(dynamicConfig( - OASConfig.SCHEMA_PREFIX + nullableStringArySig, - "{ \"name\": \"NullableStringArray\", \"type\": \"array\", \"items\": { \"type\": \"string\" }, \"nullable\": true }"), - index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); + OpenAPI result = scan(config( + OASConfig.SCHEMA_PREFIX + nullableStringArySig, + "{ \"name\": \"NullableStringArray\", \"type\": \"array\", \"items\": { \"type\": \"string\" }, \"nullable\": true }"), + Nullable.class, Bean.class); assertJsonEquals("components.schemas.parameterized-type-schema-config.json", result); } @@ -845,12 +757,6 @@ public String getWo() { } } - Index index = indexOf(Bean.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - - OpenAPI result = scanner.scan(); - - printToConsole(result); - assertJsonEquals("components.schemas.jackson-property-access.json", result); + assertJsonEquals("components.schemas.jackson-property-access.json", Bean.class); } } diff --git a/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/ApiResponseTests.java b/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/ApiResponseTests.java index 3021aae79..22c31d8af 100644 --- a/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/ApiResponseTests.java +++ b/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/ApiResponseTests.java @@ -11,8 +11,6 @@ import org.eclipse.microprofile.openapi.annotations.media.Schema; import org.eclipse.microprofile.openapi.annotations.responses.APIResponse; import org.eclipse.microprofile.openapi.annotations.responses.APIResponseSchema; -import org.eclipse.microprofile.openapi.models.OpenAPI; -import org.jboss.jandex.Index; import org.json.JSONException; import org.junit.jupiter.api.Test; @@ -21,131 +19,123 @@ */ class ApiResponseTests extends IndexScannerTestBase { - private static void test(String expectedResource, Class... classes) throws IOException, JSONException { - Index index = indexOf(classes); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals(expectedResource, result); - } - @Test void testJavaxResponseGenerationSuppressedByApiResourcesAnnotation() throws IOException, JSONException { - test("responses.generation-suppressed-by-api-responses-annotation.json", + assertJsonEquals("responses.generation-suppressed-by-api-responses-annotation.json", test.io.smallrye.openapi.runtime.scanner.javax.ResponseGenerationSuppressedByApiResourcesAnnotationTestResource.class, test.io.smallrye.openapi.runtime.scanner.javax.Pet.class, javax.json.JsonString.class); } @Test void testJakartaResponseGenerationSuppressedByApiResourcesAnnotation() throws IOException, JSONException { - test("responses.generation-suppressed-by-api-responses-annotation.json", + assertJsonEquals("responses.generation-suppressed-by-api-responses-annotation.json", test.io.smallrye.openapi.runtime.scanner.jakarta.ResponseGenerationSuppressedByApiResourcesAnnotationTestResource.class, test.io.smallrye.openapi.runtime.scanner.jakarta.Pet.class, jakarta.json.JsonString.class); } @Test void testJavaxResponseGenerationSuppressedBySuppliedDefaultApiResource() throws IOException, JSONException { - test("responses.generation-suppressed-by-supplied-default-api-response.json", + assertJsonEquals("responses.generation-suppressed-by-supplied-default-api-response.json", test.io.smallrye.openapi.runtime.scanner.javax.ResponseGenerationSuppressedBySuppliedDefaultApiResourceTestResource.class, test.io.smallrye.openapi.runtime.scanner.javax.Pet.class, javax.json.JsonString.class); } @Test void testJakartaResponseGenerationSuppressedBySuppliedDefaultApiResource() throws IOException, JSONException { - test("responses.generation-suppressed-by-supplied-default-api-response.json", + assertJsonEquals("responses.generation-suppressed-by-supplied-default-api-response.json", test.io.smallrye.openapi.runtime.scanner.jakarta.ResponseGenerationSuppressedBySuppliedDefaultApiResourceTestResource.class, test.io.smallrye.openapi.runtime.scanner.jakarta.Pet.class, jakarta.json.JsonString.class); } @Test void testJavaxResponseGenerationSuppressedByStatusOmission() throws IOException, JSONException { - test("responses.generation-suppressed-by-status-omission.json", + assertJsonEquals("responses.generation-suppressed-by-status-omission.json", test.io.smallrye.openapi.runtime.scanner.javax.ResponseGenerationSuppressedByStatusOmissionTestResource.class, test.io.smallrye.openapi.runtime.scanner.javax.Pet.class, javax.json.JsonString.class); } @Test void testJakartaResponseGenerationSuppressedByStatusOmission() throws IOException, JSONException { - test("responses.generation-suppressed-by-status-omission.json", + assertJsonEquals("responses.generation-suppressed-by-status-omission.json", test.io.smallrye.openapi.runtime.scanner.jakarta.ResponseGenerationSuppressedByStatusOmissionTestResource.class, test.io.smallrye.openapi.runtime.scanner.jakarta.Pet.class, jakarta.json.JsonString.class); } @Test void testJavaxResponseGenerationEnabledByIncompleteApiResponse() throws IOException, JSONException { - test("responses.generation-enabled-by-incomplete-api-response.json", + assertJsonEquals("responses.generation-enabled-by-incomplete-api-response.json", test.io.smallrye.openapi.runtime.scanner.javax.ResponseGenerationEnabledByIncompleteApiResponseTestResource.class, test.io.smallrye.openapi.runtime.scanner.javax.Pet.class, javax.json.JsonString.class); } @Test void testJakartaResponseGenerationEnabledByIncompleteApiResponse() throws IOException, JSONException { - test("responses.generation-enabled-by-incomplete-api-response.json", + assertJsonEquals("responses.generation-enabled-by-incomplete-api-response.json", test.io.smallrye.openapi.runtime.scanner.jakarta.ResponseGenerationEnabledByIncompleteApiResponseTestResource.class, test.io.smallrye.openapi.runtime.scanner.jakarta.Pet.class, jakarta.json.JsonString.class); } @Test void testJakartaResponseGenerationJsonExampleApiResourceTestResource() throws IOException, JSONException { - test("responses.generation-json-example-api-response.json", + assertJsonEquals("responses.generation-json-example-api-response.json", test.io.smallrye.openapi.runtime.scanner.jakarta.ResponseGenerationJsonExampleApiResourceTestResource.class); } @Test void testJavaxResponseMultipartGeneration() throws IOException, JSONException { - test("responses.multipart-generation.json", + assertJsonEquals("responses.multipart-generation.json", test.io.smallrye.openapi.runtime.scanner.javax.ResponseMultipartGenerationTestResource.class); } @Test void testJakartaResponseMultipartGeneration() throws IOException, JSONException { - test("responses.multipart-generation.json", + assertJsonEquals("responses.multipart-generation.json", test.io.smallrye.openapi.runtime.scanner.jakarta.ResponseMultipartGenerationTestResource.class); } @Test void testJavaxVoidPostResponseGeneration() throws IOException, JSONException { - test("responses.void-post-response-generation.json", + assertJsonEquals("responses.void-post-response-generation.json", test.io.smallrye.openapi.runtime.scanner.javax.VoidPostResponseGenerationTestResource.class, test.io.smallrye.openapi.runtime.scanner.javax.Pet.class, javax.json.JsonString.class); } @Test void testJakartaVoidPostResponseGeneration() throws IOException, JSONException { - test("responses.void-post-response-generation.json", + assertJsonEquals("responses.void-post-response-generation.json", test.io.smallrye.openapi.runtime.scanner.jakarta.VoidPostResponseGenerationTestResource.class, test.io.smallrye.openapi.runtime.scanner.jakarta.Pet.class, jakarta.json.JsonString.class); } @Test void testJavaxVoidNonPostResponseGeneration() throws IOException, JSONException { - test("responses.void-nonpost-response-generation.json", + assertJsonEquals("responses.void-nonpost-response-generation.json", test.io.smallrye.openapi.runtime.scanner.javax.VoidNonPostResponseGenerationTestResource.class); } @Test void testJakartaVoidNonPostResponseGeneration() throws IOException, JSONException { - test("responses.void-nonpost-response-generation.json", + assertJsonEquals("responses.void-nonpost-response-generation.json", test.io.smallrye.openapi.runtime.scanner.jakarta.VoidNonPostResponseGenerationTestResource.class); } @Test void testJavaxVoidAsyncResponseGeneration() throws IOException, JSONException { - test("responses.void-async-response-generation.json", + assertJsonEquals("responses.void-async-response-generation.json", test.io.smallrye.openapi.runtime.scanner.javax.VoidAsyncResponseGenerationTestResource.class, test.io.smallrye.openapi.runtime.scanner.ServerError.class); } @Test void testJakartaVoidAsyncResponseGeneration() throws IOException, JSONException { - test("responses.void-async-response-generation.json", + assertJsonEquals("responses.void-async-response-generation.json", test.io.smallrye.openapi.runtime.scanner.jakarta.VoidAsyncResponseGenerationTestResource.class, test.io.smallrye.openapi.runtime.scanner.ServerError.class); } @Test void testJavaxReferenceResponse() throws IOException, JSONException { - test("responses.component-status-reuse.json", + assertJsonEquals("responses.component-status-reuse.json", test.io.smallrye.openapi.runtime.scanner.javax.ReferenceResponseTestApp.class, test.io.smallrye.openapi.runtime.scanner.javax.ReferenceResponseTestResource.class, javax.json.JsonObject.class); @@ -153,7 +143,7 @@ void testJavaxReferenceResponse() throws IOException, JSONException { @Test void testJakartaReferenceResponse() throws IOException, JSONException { - test("responses.component-status-reuse.json", + assertJsonEquals("responses.component-status-reuse.json", test.io.smallrye.openapi.runtime.scanner.jakarta.ReferenceResponseTestApp.class, test.io.smallrye.openapi.runtime.scanner.jakarta.ReferenceResponseTestResource.class, jakarta.json.JsonObject.class); @@ -161,7 +151,7 @@ void testJakartaReferenceResponse() throws IOException, JSONException { @Test void testJavaxGenericTypeVariableResponses() throws IOException, JSONException { - test("responses.generic-type-variables.json", + assertJsonEquals("responses.generic-type-variables.json", test.io.smallrye.openapi.runtime.scanner.Apple.class, test.io.smallrye.openapi.runtime.scanner.javax.BaseResource2.class, test.io.smallrye.openapi.runtime.scanner.javax.TestResource3.class); @@ -169,7 +159,7 @@ void testJavaxGenericTypeVariableResponses() throws IOException, JSONException { @Test void testJakartaGenericTypeVariableResponses() throws IOException, JSONException { - test("responses.generic-type-variables.json", + assertJsonEquals("responses.generic-type-variables.json", test.io.smallrye.openapi.runtime.scanner.Apple.class, test.io.smallrye.openapi.runtime.scanner.jakarta.BaseResource2.class, test.io.smallrye.openapi.runtime.scanner.jakarta.TestResource3.class); @@ -197,7 +187,7 @@ public javax.ws.rs.core.MultivaluedMap> getM } } - test("responses.nested-parameterized-collection-types.json", + assertJsonEquals("responses.nested-parameterized-collection-types.json", Resource.class, Resource.CustomRequest.class, Resource.CustomResponse.class, @@ -231,7 +221,7 @@ public jakarta.ws.rs.core.MultivaluedMap> ge } } - test("responses.nested-parameterized-collection-types.json", + assertJsonEquals("responses.nested-parameterized-collection-types.json", Resource.class, Resource.CustomRequest.class, Resource.CustomResponse.class, @@ -275,7 +265,7 @@ public String hello2() { } } - test("responses.kotlin-continuation.json", + assertJsonEquals("responses.kotlin-continuation.json", Resource.class, kotlin.coroutines.Continuation.class, kotlin.coroutines.CoroutineContext.class); @@ -299,11 +289,8 @@ public Either getEither() { return null; } } - Index index = indexOf(Test.class, Either.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("responses.standin-generic-type-resolved.json", result); + + assertJsonEquals("responses.standin-generic-type-resolved.json", Test.class, Either.class); } @Test @@ -328,11 +315,7 @@ public jakarta.ws.rs.core.Response alsoWrong(kotlin.Pair aPair) } } - Index index = Index.of(Resource.class, kotlin.Pair.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("responses.kotlin-continuation-opaque.json", result); + assertJsonEquals("responses.kotlin-continuation-opaque.json", Resource.class, kotlin.Pair.class); } @Test @@ -351,11 +334,7 @@ public io.smallrye.mutiny.Uni deleteItem() { } } - Index index = indexOf(TestResource.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("responses.mutiny-uni.json", result); + assertJsonEquals("responses.mutiny-uni.json", TestResource.class); } @Test @@ -390,11 +369,7 @@ public java.util.concurrent.CompletionStage deleteI } } - Index index = indexOf(TestResource.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("responses.api-response-schema-variations.json", result); + assertJsonEquals("responses.api-response-schema-variations.json", TestResource.class); } @Test @@ -435,14 +410,9 @@ public Reference getDefaultBranch() { } } - Index index = indexOf( - TreeApi.class, - HttpTreeApi.class, - Reference.class, - ReferencesResponse.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); - printToConsole(result); - assertJsonEquals("responses.nonjaxrs-methods-skipped.json", result); + assertJsonEquals("responses.nonjaxrs-methods-skipped.json", TreeApi.class, + HttpTreeApi.class, + Reference.class, + ReferencesResponse.class); } } diff --git a/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/DeprecatedAnnotationTest.java b/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/DeprecatedAnnotationTest.java index 5b167f47b..5b054aa5c 100644 --- a/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/DeprecatedAnnotationTest.java +++ b/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/DeprecatedAnnotationTest.java @@ -17,7 +17,6 @@ import org.eclipse.microprofile.openapi.models.OpenAPI; import org.eclipse.microprofile.openapi.models.media.Schema; -import org.jboss.jandex.Index; import org.json.JSONException; import org.junit.jupiter.api.Test; @@ -50,9 +49,7 @@ public Response getD2() { @Test void testDeprecatedClassSetsOperationsDeprecated() throws IOException, JSONException { - Index index = Index.of(DeprecatedResource.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); + OpenAPI result = scan(DeprecatedResource.class); assertTrue(result.getPaths().getPathItem("/deprecated/d1").getGET().getDeprecated()); assertTrue(result.getPaths().getPathItem("/deprecated/d2").getGET().getDeprecated()); } @@ -81,9 +78,7 @@ public Response getD1() { @Test void testDeprecatedMethodSetsOperationsDeprecated() throws IOException, JSONException { - Index index = Index.of(MixedDeprecationResource.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); + OpenAPI result = scan(MixedDeprecationResource.class); assertNull(result.getPaths().getPathItem("/mixed/m1").getGET().getDeprecated()); assertTrue(result.getPaths().getPathItem("/mixed/d1").getGET().getDeprecated()); } @@ -117,9 +112,7 @@ public Response getO3(@MatrixParam("p1") @Deprecated String p1, @MatrixParam("p2 @Test void testDeprecatedParametersSetDeprecated() throws IOException, JSONException { - Index index = Index.of(DeprecatedParamResource.class); - OpenApiAnnotationScanner scanner = new OpenApiAnnotationScanner(emptyConfig(), index); - OpenAPI result = scanner.scan(); + OpenAPI result = scan(DeprecatedParamResource.class); assertTrue(result.getPaths().getPathItem("/params/o1").getGET().getParameters().get(0).getDeprecated()); assertNull(result.getPaths().getPathItem("/params/o1").getGET().getParameters().get(1).getDeprecated()); diff --git a/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/VersionTest.java b/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/VersionTest.java index 80f0f78bc..3bb00f95e 100644 --- a/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/VersionTest.java +++ b/extension-jaxrs/src/test/java/io/smallrye/openapi/runtime/scanner/VersionTest.java @@ -2,18 +2,12 @@ import java.io.IOException; import java.io.InputStream; +import java.util.Collections; -import org.eclipse.microprofile.config.Config; -import org.eclipse.microprofile.config.ConfigProvider; import org.eclipse.microprofile.openapi.models.OpenAPI; -import org.jboss.jandex.Index; import org.json.JSONException; import org.junit.jupiter.api.Test; -import io.smallrye.openapi.api.OpenApiConfig; -import io.smallrye.openapi.runtime.OpenApiProcessor; -import io.smallrye.openapi.runtime.OpenApiStaticFile; -import io.smallrye.openapi.runtime.io.Format; import test.io.smallrye.openapi.runtime.scanner.entities.Greeting; /** @@ -33,44 +27,35 @@ class VersionTest extends JaxRsDataObjectScannerTestBase { */ @Test void testJavaxSettingViaProvidedSchema() throws IOException, JSONException { - Index i = indexOf(test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingGetResource.class, Greeting.class); - testSettingViaProvidedSchema(i); + testSettingViaProvidedSchema(test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingGetResource.class, Greeting.class); } @Test void testJakartaSettingViaProvidedSchema() throws IOException, JSONException { - Index i = indexOf(test.io.smallrye.openapi.runtime.scanner.resources.jakarta.GreetingGetResource.class, Greeting.class); - testSettingViaProvidedSchema(i); + testSettingViaProvidedSchema(test.io.smallrye.openapi.runtime.scanner.resources.jakarta.GreetingGetResource.class, Greeting.class); } - void testSettingViaProvidedSchema(Index i) throws IOException, JSONException { - OpenAPI result = OpenApiProcessor.bootstrap(emptyConfig(), i, loadStaticFile()); - - printToConsole(result); + void testSettingViaProvidedSchema(Class... classes) throws IOException, JSONException { + OpenAPI result = scan(config(Collections.emptyMap()), true, loadStaticFile(), classes); assertJsonEquals("resource.testVersionViaSchema.json", result); } @Test void testJavaxSettingViaConfig() throws IOException, JSONException { - Index i = indexOf(test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingGetResource.class, Greeting.class); - testSettingViaConfig(i); + testSettingViaConfig(test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingGetResource.class, Greeting.class); } @Test void testJakartaSettingViaConfig() throws IOException, JSONException { - Index i = indexOf(test.io.smallrye.openapi.runtime.scanner.resources.jakarta.GreetingGetResource.class, Greeting.class); - testSettingViaConfig(i); + testSettingViaConfig(test.io.smallrye.openapi.runtime.scanner.resources.jakarta.GreetingGetResource.class, Greeting.class); } - void testSettingViaConfig(Index i) throws IOException, JSONException { + void testSettingViaConfig(Class... classes) throws IOException, JSONException { System.setProperty(VERSION_PROPERTY, "3.0.0"); - Config config = ConfigProvider.getConfig(); - OpenApiConfig openApiConfig = OpenApiConfig.fromConfig(config); + try { - OpenAPI result = OpenApiProcessor.bootstrap(openApiConfig, i); - printToConsole(result); + OpenAPI result = scan(config(Collections.emptyMap()), true, null, classes); assertJsonEquals("resource.testVersionViaConfig.json", result); - } finally { System.clearProperty(VERSION_PROPERTY); } @@ -78,35 +63,29 @@ void testSettingViaConfig(Index i) throws IOException, JSONException { @Test void testJavaxSettingViaConfigWhenStaticPresent() throws IOException, JSONException { - Index i = indexOf(test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingGetResource.class, Greeting.class); - testSettingViaConfigWhenStaticPresent(i); + testSettingViaConfigWhenStaticPresent(test.io.smallrye.openapi.runtime.scanner.resources.javax.GreetingGetResource.class, Greeting.class); } @Test void testJakartaSettingViaConfigWhenStaticPresent() throws IOException, JSONException { - Index i = indexOf(test.io.smallrye.openapi.runtime.scanner.resources.jakarta.GreetingGetResource.class, Greeting.class); - testSettingViaConfigWhenStaticPresent(i); + testSettingViaConfigWhenStaticPresent(test.io.smallrye.openapi.runtime.scanner.resources.jakarta.GreetingGetResource.class, Greeting.class); } - void testSettingViaConfigWhenStaticPresent(Index i) throws IOException, JSONException { + void testSettingViaConfigWhenStaticPresent(Class... classes) throws IOException, JSONException { System.setProperty(VERSION_PROPERTY, "3.0.0"); - Config config = ConfigProvider.getConfig(); - OpenApiConfig openApiConfig = OpenApiConfig.fromConfig(config); - try { - OpenAPI result = OpenApiProcessor.bootstrap(openApiConfig, i, loadStaticFile()); - printToConsole(result); + try { + OpenAPI result = scan(config(Collections.emptyMap()), true, loadStaticFile(), classes); assertJsonEquals("resource.testVersionViaConfig.json", result); - } finally { System.clearProperty(VERSION_PROPERTY); } } - private OpenApiStaticFile loadStaticFile() { + private InputStream loadStaticFile() { ClassLoader classLoader = this.getClass().getClassLoader(); InputStream versionJson = classLoader.getResourceAsStream("io/smallrye/openapi/runtime/scanner/static/version.json"); - return new OpenApiStaticFile(versionJson, Format.JSON); + return versionJson; } }