Skip to content

Commit

Permalink
Manually try Swagger2 and OpenAPI 3 parsers to fix issue with Swagger…
Browse files Browse the repository at this point in the history
… 2 specs not being parsed in native images
  • Loading branch information
en-milie committed Jan 18, 2022
1 parent b2f00de commit 23dd9dc
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/main/java/com/endava/cats/ReflectionConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
}
18 changes: 14 additions & 4 deletions src/main/java/com/endava/cats/util/OpenApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
}
}

Expand Down

0 comments on commit 23dd9dc

Please sign in to comment.