From 1905b555e406b2c17029673d8141f25560a3a09c Mon Sep 17 00:00:00 2001 From: Andrew Rouse Date: Fri, 8 Nov 2024 14:02:28 +0000 Subject: [PATCH] Detect static file format when parsing (#2060) * 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. --- .../smallrye/openapi/api/SmallRyeOpenAPI.java | 9 ++++--- .../api/SmallRyeOpenAPIBuilderTest.java | 25 +++++++++++++++++++ .../classloaderjson/META-INF/openapi.json | 9 +++++++ 3 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 core/src/test/resources/classloaderjson/META-INF/openapi.json 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 6aed575d6..cf924927e 100644 --- a/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java +++ b/core/src/main/java/io/smallrye/openapi/api/SmallRyeOpenAPI.java @@ -567,7 +567,8 @@ protected 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); @@ -578,14 +579,14 @@ protected 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 void addStaticModel(BuildContext 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); diff --git a/core/src/test/java/io/smallrye/openapi/api/SmallRyeOpenAPIBuilderTest.java b/core/src/test/java/io/smallrye/openapi/api/SmallRyeOpenAPIBuilderTest.java index 9baee01dc..879a78d81 100644 --- a/core/src/test/java/io/smallrye/openapi/api/SmallRyeOpenAPIBuilderTest.java +++ b/core/src/test/java/io/smallrye/openapi/api/SmallRyeOpenAPIBuilderTest.java @@ -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() diff --git a/core/src/test/resources/classloaderjson/META-INF/openapi.json b/core/src/test/resources/classloaderjson/META-INF/openapi.json new file mode 100644 index 000000000..01688e8e4 --- /dev/null +++ b/core/src/test/resources/classloaderjson/META-INF/openapi.json @@ -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": {} +}