From 23dd9dc0987949976fdf2e6abe61f8a7eb183f3d Mon Sep 17 00:00:00 2001 From: Madalin Ilie Date: Tue, 18 Jan 2022 20:48:54 +0200 Subject: [PATCH] Manually try Swagger2 and OpenAPI 3 parsers to fix issue with Swagger 2 specs not being parsed in native images --- .../java/com/endava/cats/ReflectionConfig.java | 3 ++- .../com/endava/cats/util/OpenApiUtils.java | 18 ++++++++++++++---- 2 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/endava/cats/ReflectionConfig.java b/src/main/java/com/endava/cats/ReflectionConfig.java index 512ba24bd..cc1212554 100644 --- a/src/main/java/com/endava/cats/ReflectionConfig.java +++ b/src/main/java/com/endava/cats/ReflectionConfig.java @@ -94,6 +94,7 @@ import io.swagger.v3.oas.models.servers.ServerVariables; import io.swagger.v3.oas.models.tags.Tag; import io.swagger.v3.parser.converter.SwaggerConverter; +import io.swagger.v3.parser.core.extensions.SwaggerParserExtension; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.RandomUtils; import org.apache.commons.lang3.time.DateFormatUtils; @@ -168,7 +169,7 @@ CompositeStringExpression.class, TemplateAwareExpressionParser.class, LiteralExpression.class, TemplateParserContext.class, ConstructorExecutor.class, TypeComparator.class, EvaluationContext.class, AccessException.class, PropertyAccessor.class, TypeConverter.class, BeanResolver.class, TypedValue.class, CatsDSLParser.class, Base64.Encoder.class, Base64.Decoder.class, Base64.class, RandomUtils.class, RandomStringUtils.class, DateFormatUtils.class, DateUtils.class, DurationUtils.class, LocalDate.class, LocalDateTime.class, - OffsetDateTime.class, String.class, SwaggerConverter.class + OffsetDateTime.class, String.class, SwaggerConverter.class, SwaggerParserExtension.class }) public class ReflectionConfig { } diff --git a/src/main/java/com/endava/cats/util/OpenApiUtils.java b/src/main/java/com/endava/cats/util/OpenApiUtils.java index 60621b1aa..3ec694c64 100644 --- a/src/main/java/com/endava/cats/util/OpenApiUtils.java +++ b/src/main/java/com/endava/cats/util/OpenApiUtils.java @@ -2,12 +2,14 @@ import io.github.ludovicianul.prettylogger.PrettyLogger; import io.github.ludovicianul.prettylogger.PrettyLoggerFactory; -import io.swagger.parser.OpenAPIParser; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.media.ArraySchema; import io.swagger.v3.oas.models.media.Content; import io.swagger.v3.oas.models.media.MediaType; import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.parser.OpenAPIV3Parser; +import io.swagger.v3.parser.converter.SwaggerConverter; +import io.swagger.v3.parser.core.extensions.SwaggerParserExtension; import io.swagger.v3.parser.core.models.ParseOptions; import java.io.IOException; @@ -26,15 +28,23 @@ private OpenApiUtils() { } public static OpenAPI readOpenApi(String location) throws IOException { - OpenAPIParser openAPIV3Parser = new OpenAPIParser(); ParseOptions options = new ParseOptions(); options.setResolve(true); options.setFlatten(true); + OpenAPI openAPI = getOpenAPI(new OpenAPIV3Parser(), location, options); + + if (openAPI == null) { + openAPI = getOpenAPI(new SwaggerConverter(), location, options); + } + return openAPI; + } + + public static OpenAPI getOpenAPI(SwaggerParserExtension parserExtension, String location, ParseOptions options) throws IOException { if (location.startsWith("http")) { - return openAPIV3Parser.readLocation(location, null, options).getOpenAPI(); + return parserExtension.readLocation(location, null, options).getOpenAPI(); } else { - return openAPIV3Parser.readContents(Files.readString(Paths.get(location)), null, options).getOpenAPI(); + return parserExtension.readContents(Files.readString(Paths.get(location)), null, options).getOpenAPI(); } }