diff --git a/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/OpenApiResourceLoader.java b/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/OpenApiResourceLoader.java index ad3e4bc971..17c79ba4c3 100644 --- a/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/OpenApiResourceLoader.java +++ b/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/OpenApiResourceLoader.java @@ -47,9 +47,9 @@ */ public final class OpenApiResourceLoader { - static final RawResolver RAW_RESOLVER = new RawResolver(); + private static final RawResolver RAW_RESOLVER = new RawResolver(); - static final OasResolver OAS_RESOLVER = new OasResolver(); + private static final OasResolver OAS_RESOLVER = new OasResolver(); /** * Prevent instantiation of utility class. diff --git a/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/OpenApiTestDataGenerator.java b/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/OpenApiTestDataGenerator.java index 30b0e45247..d98ed5d5c9 100644 --- a/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/OpenApiTestDataGenerator.java +++ b/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/OpenApiTestDataGenerator.java @@ -285,6 +285,15 @@ public static String createValidationExpression(OasSchema schema, Map T delegate(OasParameter parameter, Function generic return value + * @return + */ + private static T delegate(OasSchema schema, Function oas20Function, Function oas30Function) { + if (schema instanceof Oas20Schema oas20Schema) { + return oas20Function.apply(oas20Schema); + } else if (schema instanceof Oas30Schema oas30Schema) { + return oas30Function.apply(oas30Schema); + } + + throw new IllegalArgumentException(String.format("Unsupported operation parameter type: %s", schema.getClass())); + } + /** * Delegate method to version specific model helpers for Open API v2 or v3. * @param operation @@ -537,4 +561,5 @@ public static List resolveAllTypes(@Nullable List acceptedMediaT return acceptedMediaTypes.stream() .flatMap(types -> Arrays.stream(types.split(","))).map(String::trim).toList(); } + } diff --git a/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v2/Oas20ModelHelper.java b/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v2/Oas20ModelHelper.java index bb0cdf977f..cb00a6ef46 100644 --- a/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v2/Oas20ModelHelper.java +++ b/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v2/Oas20ModelHelper.java @@ -25,11 +25,9 @@ import io.apicurio.datamodels.openapi.v2.models.Oas20Parameter; import io.apicurio.datamodels.openapi.v2.models.Oas20Response; import io.apicurio.datamodels.openapi.v2.models.Oas20Schema; +import io.apicurio.datamodels.openapi.v2.models.Oas20Schema.Oas20AllOfSchema; import io.apicurio.datamodels.openapi.v2.models.Oas20SchemaDefinition; import jakarta.annotation.Nullable; -import org.citrusframework.openapi.model.OasAdapter; -import org.citrusframework.openapi.model.OasModelHelper; - import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -37,6 +35,8 @@ import java.util.Map; import java.util.Optional; import java.util.stream.Collectors; +import org.citrusframework.openapi.model.OasAdapter; +import org.citrusframework.openapi.model.OasModelHelper; /** * @author Christoph Deppisch @@ -90,6 +90,11 @@ public static Optional> getSchema(Oas20Operation o return selectedSchema == null && selectedMediaType == null ? Optional.empty() : Optional.of(new OasAdapter<>(selectedSchema, selectedMediaType)); } + public static boolean isCompositeSchema(Oas20Schema schema) { + // Note that oneOf and anyOf is not supported by Oas20. + return schema instanceof Oas20AllOfSchema; + } + public static Optional getRequestBodySchema(@Nullable Oas20Document ignoredOpenApiDoc, Oas20Operation operation) { if (operation.parameters == null) { return Optional.empty(); @@ -204,4 +209,5 @@ public static Optional getParameterSchema(Oas20Parameter parameter) { return Optional.of(schema); } + } diff --git a/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v3/Oas30ModelHelper.java b/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v3/Oas30ModelHelper.java index 75b977b9f5..e21e242705 100644 --- a/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v3/Oas30ModelHelper.java +++ b/connectors/citrus-openapi/src/main/java/org/citrusframework/openapi/model/v3/Oas30ModelHelper.java @@ -88,6 +88,10 @@ public static List getSchemes(Oas30Document openApiDoc) { .toList(); } + public static boolean isCompositeSchema(Oas30Schema schema) { + return schema.anyOf != null || schema.oneOf != null || schema.allOf != null; + } + public static String getBasePath(Oas30Document openApiDoc) { if (openApiDoc.servers == null || openApiDoc.servers.isEmpty()) { return "/"; @@ -248,4 +252,5 @@ private static String resolveUrl(Server server) { public static Optional getParameterSchema(Oas30Parameter parameter) { return Optional.ofNullable((OasSchema) parameter.schema); } + } diff --git a/connectors/citrus-openapi/src/test/java/org/citrusframework/openapi/OpenApiTestDataGeneratorTest.java b/connectors/citrus-openapi/src/test/java/org/citrusframework/openapi/OpenApiTestDataGeneratorTest.java index ab063e27c9..a16ea69d09 100644 --- a/connectors/citrus-openapi/src/test/java/org/citrusframework/openapi/OpenApiTestDataGeneratorTest.java +++ b/connectors/citrus-openapi/src/test/java/org/citrusframework/openapi/OpenApiTestDataGeneratorTest.java @@ -16,35 +16,55 @@ package org.citrusframework.openapi; -import com.fasterxml.jackson.core.JsonProcessingException; -import io.apicurio.datamodels.openapi.models.OasSchema; -import org.citrusframework.openapi.model.OasModelHelper; -import org.citrusframework.spi.Resources; -import org.testng.Assert; +import static org.mockito.Mockito.mock; +import static org.testng.Assert.assertEquals; + +import io.apicurio.datamodels.openapi.v2.models.Oas20Schema; +import io.apicurio.datamodels.openapi.v2.models.Oas20Schema.Oas20AllOfSchema; +import io.apicurio.datamodels.openapi.v3.models.Oas30Schema; +import java.util.HashMap; +import java.util.List; import org.testng.annotations.Test; -import java.util.Map; +public class OpenApiTestDataGeneratorTest { -import static org.testng.Assert.assertFalse; -import static org.testng.Assert.assertNotNull; + @Test + public void anyOfIsIgnoredForOas3() { -// TODO: Add more tests -public class OpenApiTestDataGeneratorTest { + Oas30Schema anyOfSchema = new Oas30Schema(); + anyOfSchema.anyOf = List.of(new Oas30Schema(), new Oas30Schema()); + + assertEquals(OpenApiTestDataGenerator.createValidationExpression( + anyOfSchema, new HashMap<>(), true, mock()), "\"@ignore@\""); + } + + @Test + public void allOfIsIgnoredForOas3() { - private final OpenApiSpecification pingSpec = OpenApiSpecification.from( - Resources.create("classpath:org/citrusframework/openapi/ping/ping-api.yaml")); + Oas30Schema allOfSchema = new Oas30Schema(); + allOfSchema.allOf = List.of(new Oas30Schema(), new Oas30Schema()); + + assertEquals(OpenApiTestDataGenerator.createValidationExpression( + allOfSchema, new HashMap<>(), true, mock()), "\"@ignore@\""); + } + + @Test + public void oneOfIsIgnoredForOas3() { + + Oas30Schema oneOfSchema = new Oas30Schema(); + oneOfSchema.oneOf = List.of(new Oas30Schema(), new Oas30Schema()); + + assertEquals(OpenApiTestDataGenerator.createValidationExpression( + oneOfSchema, new HashMap<>(), true, mock()), "\"@ignore@\""); + } - // TODO: fix this by introducing mature validation @Test - public void failsToValidateAnyOf() throws JsonProcessingException { + public void allOfIsIgnoredForOas2() { - Map schemaDefinitions = OasModelHelper.getSchemaDefinitions( - pingSpec.getOpenApiDoc(null)); - assertNotNull(schemaDefinitions); - assertFalse(schemaDefinitions.isEmpty()); - Assert.assertEquals(schemaDefinitions.size(), 15); + Oas20AllOfSchema allOfSchema = new Oas20AllOfSchema(); + allOfSchema.allOf = List.of(new Oas20Schema(), new Oas20Schema()); - Assert.assertThrows(() -> OpenApiTestDataGenerator.createValidationExpression( - schemaDefinitions.get("PingRespType"), schemaDefinitions, true, pingSpec)); + assertEquals(OpenApiTestDataGenerator.createValidationExpression( + allOfSchema, new HashMap<>(), true, mock()), "\"@ignore@\""); } }