Skip to content

Commit

Permalink
Detect static file format when parsing (#2060)
Browse files Browse the repository at this point in the history
* Detect static file format when parsing

Ensure that an openapi.json file is parsed as JSON, rather than as YAML.

* JSON static file test

Test we correctly load a JSON static file. This test will fail if we try
to parse the JSON as YAML.
  • Loading branch information
Azquelt authored Nov 8, 2024
1 parent 0ed2760 commit 1905b55
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,8 @@ protected <V, A extends V, O extends V, AB, OB> void buildStaticModel(BuildConte
for (URL staticFile : OpenApiProcessor.locateStaticFiles(loadFn)) {
String source = "static file " + staticFile;
try {
ClassPathUtils.consumeStream(staticFile, stream -> addStaticModel(ctx, stream, source));
Format format = staticFile.toString().endsWith(".json") ? Format.JSON : Format.YAML;
ClassPathUtils.consumeStream(staticFile, stream -> addStaticModel(ctx, stream, source, format));
} catch (IOException e) {
String msg = "IOException reading " + source;
throw new OpenApiRuntimeException(msg, e);
Expand All @@ -578,14 +579,14 @@ protected <V, A extends V, O extends V, AB, OB> void buildStaticModel(BuildConte
InputStream customFile = customStaticFile.get();

if (customFile != null) {
addStaticModel(ctx, customFile, "custom static file");
addStaticModel(ctx, customFile, "custom static file", Format.YAML);
}
}

private <V, A extends V, O extends V, AB, OB> void addStaticModel(BuildContext<V, A, O, AB, OB> ctx, InputStream stream,
String source) {
String source, Format fileFormat) {
try (Reader reader = new InputStreamReader(stream)) {
V dom = ctx.modelIO.jsonIO().fromReader(reader);
V dom = ctx.modelIO.jsonIO().fromReader(reader, fileFormat);
OpenAPI fileModel = ctx.modelIO.readValue(dom);
debugModel(source, fileModel);
ctx.staticModel = MergeUtil.merge(fileModel, ctx.staticModel);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,31 @@ void testStaticFileLoadedFromClasspath() throws Exception {
assertEquals("Loaded from the class path", model.getInfo().getTitle());
}

@Test
void testStaticJsonFileLoadedFromClasspath() throws Exception {
URL loaderRoot = getClass()
.getClassLoader()
.getResource("classloaderjson/META-INF/openapi.json")
.toURI()
.resolve("..")
.toURL();

ClassLoader custom = new URLClassLoader(
new URL[] { loaderRoot },
Thread.currentThread().getContextClassLoader());

SmallRyeOpenAPI result = SmallRyeOpenAPI.builder()
.withApplicationClassLoader(custom)
.enableModelReader(false)
.enableStandardFilter(false)
.enableAnnotationScan(false)
.enableStandardStaticFiles(true)
.build();

OpenAPI model = result.model();
assertEquals("Loaded as JSON from the class path", model.getInfo().getTitle());
}

@Test
void testInvalidTypesInStaticFileDropped() {
URL invalidResource = getClass()
Expand Down
9 changes: 9 additions & 0 deletions core/src/test/resources/classloaderjson/META-INF/openapi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"openapi": "3.1.0",
"info": {
"title": "Loaded as JSON from the class path",
"description": "This document uses tabs so that it doesn't parse as YAML",
"version": "1.0.0"
},
"paths": {}
}

0 comments on commit 1905b55

Please sign in to comment.