From ccf39a35fb37ca6c79d16f68131fd13ca1c3ea44 Mon Sep 17 00:00:00 2001 From: Willem Salembier Date: Tue, 30 Jul 2024 13:01:47 +0200 Subject: [PATCH] Absent type doesn't imply type object #2113 --- .editorconfig | 1 + .../swagger/v3/parser/util/ResolverFully.java | 7 ----- .../v3/parser/test/OpenAPIResolverTest.java | 9 ++++++ .../test/OpenAPIV31ParserSchemaTest.java | 18 +++++------- .../src/test/resources/issue_2113.yaml | 29 +++++++++++++++++++ 5 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 modules/swagger-parser-v3/src/test/resources/issue_2113.yaml diff --git a/.editorconfig b/.editorconfig index fba6473b28..ab2fe76e06 100644 --- a/.editorconfig +++ b/.editorconfig @@ -8,6 +8,7 @@ root = true # Indentation style # Possible values - tab, space indent_style = space +end_of_line = lf # File character encoding # Possible values - latin1, utf-8, utf-16be, utf-16le diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java index a6b33c6f29..fec58962b9 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/ResolverFully.java @@ -469,13 +469,6 @@ public Schema resolveSchema(Schema schema) { Schema property = updated.get(key); if (property.getProperties() != model.getProperties()) { - if (!hasSchemaType(property)) { - if (SpecVersion.V30.equals(property.getSpecVersion())) { - property.setType("object"); - } else { - property.addType("object"); - } - } model.addProperties(key, property); } else { LOGGER.debug("not adding recursive properties, using generic object"); diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java index 5820e23775..5a6099cbb8 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIResolverTest.java @@ -782,7 +782,16 @@ public void testIssue1706() { assertTrue(openAPI.getPaths().get("/resource").getPost().getResponses().get("200").getContent().get("application/json").getSchema() instanceof ObjectSchema); } + @Test + public void testIssue2113() { + ParseOptions options = new ParseOptions(); + options.setResolve(true); + options.setResolveFully(true); + OpenAPI openAPI = new OpenAPIV3Parser().readLocation("issue_2113.yaml", auths, options).getOpenAPI(); + ObjectSchema schema = (ObjectSchema) openAPI.getPaths().get("/foo").getPost().getRequestBody().getContent().get("application/json").getSchema(); + assertNull(schema.getProperties().get("goo").getType()); + } @Test public void selfReferenceTest() { diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserSchemaTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserSchemaTest.java index 0768c1aaf2..883cd2ab06 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserSchemaTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserSchemaTest.java @@ -25,8 +25,6 @@ import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching; -import static org.testng.Assert.assertNotNull; -import static org.testng.Assert.assertTrue; public class OpenAPIV31ParserSchemaTest { protected int serverPort = getDynamicPort(); @@ -167,7 +165,7 @@ private void tearDownWireMockServer() { public void test$idUrlExternal() throws Exception { ParseOptions p = new ParseOptions(); p.setResolve(true); - SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-external/root.json").getAbsolutePath(), null, p); + SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-external/root.json").toURI().toASCIIString(), null, p); compare("$id-uri-external", swaggerParseResult); } @@ -175,7 +173,7 @@ private void tearDownWireMockServer() { public void test$idUrlEnclosing() throws Exception { ParseOptions p = new ParseOptions(); p.setResolve(true); - SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-enclosing/root.json").getAbsolutePath(), null, p); + SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-enclosing/root.json").toURI().toASCIIString(), null, p); compare("$id-uri-enclosing", swaggerParseResult); } @@ -183,14 +181,14 @@ private void tearDownWireMockServer() { public void test$idUrlDirect() throws Exception { ParseOptions p = new ParseOptions(); p.setResolve(true); - SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-direct/root.json").getAbsolutePath(), null, p); + SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-uri-direct/root.json").toURI().toASCIIString(), null, p); compare("$id-uri-direct", swaggerParseResult); } @Test public void test$idUrlUnresolvable() throws Exception { ParseOptions p = new ParseOptions(); p.setResolve(true); - SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-unresolvable/root.json").getAbsolutePath(), null, p); + SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$id-unresolvable/root.json").toURI().toASCIIString(), null, p); compare("$id-unresolvable", swaggerParseResult); } @@ -198,7 +196,7 @@ private void tearDownWireMockServer() { public void testAnchorExt() throws Exception { ParseOptions p = new ParseOptions(); p.setResolve(true); - SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-external/root.json").getAbsolutePath(), null, p); + SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-external/root.json").toURI().toASCIIString(), null, p); compare("$anchor-external", swaggerParseResult); } @@ -206,7 +204,7 @@ public void testAnchorExt() throws Exception { public void testAnchorInt() throws Exception { ParseOptions p = new ParseOptions(); p.setResolve(true); - SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-internal/root.json").getAbsolutePath(), null, p); + SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-internal/root.json").toURI().toASCIIString(), null, p); compare("$anchor-internal", swaggerParseResult); } @@ -214,7 +212,7 @@ public void testAnchorInt() throws Exception { public void testAnchorUnresolve() throws Exception { ParseOptions p = new ParseOptions(); p.setResolve(true); - SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-not-found/root.json").getAbsolutePath(), null, p); + SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(new File("src/test/resources/3.1.0/dereference/schema/$anchor-not-found/root.json").toURI().toASCIIString(), null, p); compare("$anchor-not-found", swaggerParseResult); } @@ -223,7 +221,7 @@ public void compare(String dir, SwaggerParseResult result) throws Exception { ObjectMapper mapper = Json31.mapper().copy(); mapper.configure(SerializationFeature.ORDER_MAP_ENTRIES_BY_KEYS, true); mapper.configure(MapperFeature.SORT_PROPERTIES_ALPHABETICALLY, true); - String actual = mapper.writer(new DefaultPrettyPrinter()).writeValueAsString(result.getOpenAPI()); + String actual = mapper.writer(new DefaultPrettyPrinter()).writeValueAsString(result.getOpenAPI()).replace("\r\n", "\n"); org.testng.Assert.assertEquals(actual, FileUtils.readFileToString(new File("src/test/resources/3.1.0/dereference/schema/" + dir + "/dereferenced.json"))); } diff --git a/modules/swagger-parser-v3/src/test/resources/issue_2113.yaml b/modules/swagger-parser-v3/src/test/resources/issue_2113.yaml new file mode 100644 index 0000000000..4076cb01ef --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue_2113.yaml @@ -0,0 +1,29 @@ +openapi: "3.0.0" +info: + version: 1.0.0 + title: Issue X +servers: + - url: http://petstore.swagger.io/api +paths: + /foo: + post: + requestBody: + content: + application/json: + schema: + $ref: '#/components/schemas/Foo' + required: true + responses: + 200: + description: ok +components: + schemas: + Foo: + type: object + allOf: + - $ref: "#/components/schemas/Goo" + Goo: + type: object + properties: + goo: + title: "Goo" \ No newline at end of file