From 138687c5385556a648ed2a14528f522670242f99 Mon Sep 17 00:00:00 2001 From: gracekarina Date: Mon, 23 Jan 2023 20:52:19 -0500 Subject: [PATCH 1/6] fix for issue-1865 - dereferencing files with dots Co-authored-by: gracekarina Co-authored-by: frantuma --- .../io/swagger/v3/parser/util/RefUtils.java | 14 +++++---- .../v3/parser/test/OpenAPIV3ParserTest.java | 30 +++++++++++++++++++ .../resources/issue-1621/example.get.yaml | 0 .../resources/issue-1621/example.openapi.yaml | 0 .../resources/issue-1621/example.post.yaml | 0 .../test/resources/issue-1865/apis/foo.yaml | 24 +++++++++++++++ .../issue-1865/examples/foo.example.yaml | 3 ++ .../issue-1865/models/foo.model.yaml | 6 ++++ .../test/resources/issue-1865/openapi30.yaml | 9 ++++++ .../io/swagger/parser/OpenAPIParserTest.java | 20 ------------- 10 files changed, 81 insertions(+), 25 deletions(-) rename modules/{swagger-parser => swagger-parser-v3}/src/test/resources/issue-1621/example.get.yaml (100%) rename modules/{swagger-parser => swagger-parser-v3}/src/test/resources/issue-1621/example.openapi.yaml (100%) rename modules/{swagger-parser => swagger-parser-v3}/src/test/resources/issue-1621/example.post.yaml (100%) create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-1865/apis/foo.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-1865/examples/foo.example.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-1865/models/foo.model.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-1865/openapi30.yaml diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/RefUtils.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/RefUtils.java index 7fcf56a4c6..561a3b5679 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/RefUtils.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/util/RefUtils.java @@ -14,6 +14,7 @@ import java.net.URL; import java.nio.file.Files; import java.nio.file.Path; +import java.util.Arrays; import java.util.List; import java.util.Optional; @@ -47,10 +48,13 @@ public static String computeDefinitionName(String ref) { plausibleName = filePathElements[filePathElements.length - 1]; final String[] split = plausibleName.split("\\."); - // Fix for issue-1621 - plausibleName = split[0]; - for (int i = 1; i < split.length - 1; i++) { - plausibleName += "." + split[i]; + // Fix for issue-1621 and issue-1865 + //validate number of dots + if(split.length > 2) { + //Remove dot so ref can be interpreted as internal and relative in Swagger-Core schema class 'set$ref' + plausibleName = String.join("", Arrays.copyOf(split, split.length - 1)); + }else{ + plausibleName = split[0]; } } @@ -225,4 +229,4 @@ private static String readAll(Path path) throws IOException { return IOUtils.toString(inputStream, UTF_8); } } -} \ No newline at end of file +} diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java index 5bb8fc8182..97eac48a99 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java @@ -86,6 +86,36 @@ public class OpenAPIV3ParserTest { protected int serverPort = getDynamicPort(); protected WireMockServer wireMockServer; + @Test + public void testIssue1621() { + final ParseOptions parseOptions = new ParseOptions(); + parseOptions.setResolve(true); + parseOptions.setResolveFully(true); + parseOptions.setResolveCombinators(false); + SwaggerParseResult parseResult = new OpenAPIV3Parser().readLocation("issue-1621/example.openapi.yaml", null, parseOptions); + assertEquals(0, parseResult.getMessages().size()); + OpenAPI api = parseResult.getOpenAPI(); + assertEquals( api.getPaths() + .get("/example") + .getPost() + .getRequestBody() + .getContent() + .get("application/json") + .getSchema() + .getTitle(),"POST Example"); + } + + @Test + public void testIssue1865() throws Exception { + ParseOptions options = new ParseOptions(); + options.setResolve(true); + SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/issue-1865/openapi30.yaml", null, options); + + Assert.assertNotNull(result); + Assert.assertNotNull(result.getOpenAPI()); + Assert.assertEquals(result.getOpenAPI().getComponents().getSchemas().get("Foo").get$ref(), "#/components/schemas/foomodel"); + } + @Test public void testIssue1777() { OpenAPIV3Parser openApiParser = new OpenAPIV3Parser(); diff --git a/modules/swagger-parser/src/test/resources/issue-1621/example.get.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1621/example.get.yaml similarity index 100% rename from modules/swagger-parser/src/test/resources/issue-1621/example.get.yaml rename to modules/swagger-parser-v3/src/test/resources/issue-1621/example.get.yaml diff --git a/modules/swagger-parser/src/test/resources/issue-1621/example.openapi.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1621/example.openapi.yaml similarity index 100% rename from modules/swagger-parser/src/test/resources/issue-1621/example.openapi.yaml rename to modules/swagger-parser-v3/src/test/resources/issue-1621/example.openapi.yaml diff --git a/modules/swagger-parser/src/test/resources/issue-1621/example.post.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1621/example.post.yaml similarity index 100% rename from modules/swagger-parser/src/test/resources/issue-1621/example.post.yaml rename to modules/swagger-parser-v3/src/test/resources/issue-1621/example.post.yaml diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1865/apis/foo.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1865/apis/foo.yaml new file mode 100644 index 0000000000..2b0c3d42b5 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-1865/apis/foo.yaml @@ -0,0 +1,24 @@ +#broken-ref/apis/foo.yaml +openapi: 3.0.0 +info: + title: swagger parser bug, worked until 2.0.28 but not since 2.0.29 + version: '1.0' + +paths: + + '/foo': + post: + summary: create foo + description: Create a new foo. + responses: + '200': + description: a Foo + content: + application/json: + schema: + $ref: '#/components/schemas/Foo' + +components: + schemas: + Foo: + $ref: ../models/foo.model.yaml \ No newline at end of file diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1865/examples/foo.example.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1865/examples/foo.example.yaml new file mode 100644 index 0000000000..43f0089b9b --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-1865/examples/foo.example.yaml @@ -0,0 +1,3 @@ +# broken-ref/examples/foo.example.yaml +value: + foo: bar \ No newline at end of file diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1865/models/foo.model.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1865/models/foo.model.yaml new file mode 100644 index 0000000000..8687b26293 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-1865/models/foo.model.yaml @@ -0,0 +1,6 @@ +#broken-ref/models/foo.model.yaml +title: Foo +type: object +properties: + foo: + type: string \ No newline at end of file diff --git a/modules/swagger-parser-v3/src/test/resources/issue-1865/openapi30.yaml b/modules/swagger-parser-v3/src/test/resources/issue-1865/openapi30.yaml new file mode 100644 index 0000000000..f8895579a5 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-1865/openapi30.yaml @@ -0,0 +1,9 @@ +# broken-ref/openapi30.yaml +openapi: 3.0.3 +info: + title: ref chain + version: '1' + +paths: + /foo: + $ref: ./apis/foo.yaml#/paths/~1foo \ No newline at end of file diff --git a/modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java b/modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java index 26dde2981c..cfcdafe211 100644 --- a/modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java +++ b/modules/swagger-parser/src/test/java/io/swagger/parser/OpenAPIParserTest.java @@ -65,26 +65,6 @@ public void testIssue1143(){ assertNotNull(result.getOpenAPI().getComponents().getSchemas().get("identificacion_usuario_aplicacion")); } - @Test - public void testIssue1621() { - final ParseOptions parseOptions = new ParseOptions(); - parseOptions.setResolve(true); - parseOptions.setResolveFully(true); - parseOptions.setResolveCombinators(false); - OpenAPIParser openAPIParser = new OpenAPIParser(); - SwaggerParseResult swaggerParseResult = openAPIParser.readLocation("issue-1621/example.openapi.yaml", null, parseOptions); - assertEquals(0, swaggerParseResult.getMessages().size()); - OpenAPI api = swaggerParseResult.getOpenAPI(); - assertEquals("POST Example", api.getPaths() - .get("/example") - .getPost() - .getRequestBody() - .getContent() - .get("application/json") - .getSchema() - .getTitle()); - } - @Test public void testIssue749() { ParseOptions options = new ParseOptions(); From 63af905ebed11dbf7fd209155b0511bf5e987dac Mon Sep 17 00:00:00 2001 From: gracekarina Date: Mon, 6 Feb 2023 20:09:22 -0500 Subject: [PATCH 2/6] fix for issue when dereferencing OneOf --- .../processors/ExternalRefProcessor.java | 9 ++++-- .../v3/parser/test/OpenAPIV3ParserTest.java | 12 ++++++++ .../resolverIssue/Beta-1-swagger.yaml | 18 ++++++++++++ .../DOCUMENTATION_DOMAIN-3.0.0-domain.yaml | 29 +++++++++++++++++++ 4 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 modules/swagger-parser-v3/src/test/resources/resolverIssue/Beta-1-swagger.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/resolverIssue/DOCUMENTATION_DOMAIN-3.0.0-domain.yaml diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java index 6388d95b42..286f9ccac9 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/ExternalRefProcessor.java @@ -149,8 +149,8 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) { for(Schema item : composedSchema.getAllOf()){ if (item.get$ref() != null){ processRefSchema(item,file); - } else if (item.getProperties() != null) { - processProperties(item.getProperties(), file); + } else{ + processSchema(item, file); } } @@ -159,6 +159,8 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) { if (item.get$ref() != null){ if (item.get$ref() != null){ processRefSchema(item,file); + }else{ + processSchema(item, file); } } } @@ -167,10 +169,11 @@ public String processRefToExternalSchema(String $ref, RefFormat refFormat) { if (item.get$ref() != null){ if (item.get$ref() != null){ processRefSchema(item,file); + }else{ + processSchema(item, file); } } } - } } //Loop the properties and recursively call this method; diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java index 97eac48a99..c46c320ce4 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV3ParserTest.java @@ -86,6 +86,18 @@ public class OpenAPIV3ParserTest { protected int serverPort = getDynamicPort(); protected WireMockServer wireMockServer; + + @Test + public void testIssueDereferencingComposedSchemaOneOf() { + OpenAPIV3Parser openApiParser = new OpenAPIV3Parser(); + ParseOptions options = new ParseOptions(); + options.setResolve(true); + SwaggerParseResult parseResult = openApiParser.readLocation("resolverIssue/Beta-1-swagger.yaml", null, options); + OpenAPI openAPI = parseResult.getOpenAPI(); + assertNotNull(openAPI.getComponents().getSchemas().get("naNumber")); + assertNotNull(openAPI.getComponents().getSchemas().get("unNumber")); + } + @Test public void testIssue1621() { final ParseOptions parseOptions = new ParseOptions(); diff --git a/modules/swagger-parser-v3/src/test/resources/resolverIssue/Beta-1-swagger.yaml b/modules/swagger-parser-v3/src/test/resources/resolverIssue/Beta-1-swagger.yaml new file mode 100644 index 0000000000..b6c00d217c --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/resolverIssue/Beta-1-swagger.yaml @@ -0,0 +1,18 @@ +openapi: 3.0.3 +paths: + /v2/bookings: + post: + tags: + - Booking Request + summary: Post a booking request + description: | + Creates a new booking request + requestBody: + description: Parameters used to create a booking request + required: true + content: + application/json: + schema: + allOf: + - $ref: 'DOCUMENTATION_DOMAIN-3.0.0-domain.yaml#/components/schemas/dangerousGood' +components: diff --git a/modules/swagger-parser-v3/src/test/resources/resolverIssue/DOCUMENTATION_DOMAIN-3.0.0-domain.yaml b/modules/swagger-parser-v3/src/test/resources/resolverIssue/DOCUMENTATION_DOMAIN-3.0.0-domain.yaml new file mode 100644 index 0000000000..e202fbd72a --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/resolverIssue/DOCUMENTATION_DOMAIN-3.0.0-domain.yaml @@ -0,0 +1,29 @@ +openapi: 3.0.3 +paths: +components: + schemas: + dangerousGood: + type: object + description: 'Specification for Dangerous Goods' + allOf: + - type: object + oneOf: + - type: object + properties: + unNumber: + "$ref": "#/components/schemas/unNumber" + - type: object + properties: + naNumber: + "$ref": "#/components/schemas/naNumber" + naNumber: + type: string + pattern: "\\d{4}" + description: 'Four-digit number that is assigned to dangerous, hazardous, and + harmful substances. Used for `US`/ `CFR` regularted shipments this is used + instead of `unNumber`' + unNumber: + type: string + pattern: "\\d{4}" + description: 'Four-digit number that is assigned to dangerous, hazardous, and + harmful substances by United Nations' \ No newline at end of file From 7eabe3bc92a0b7d743e2412820f7e0824b59411b Mon Sep 17 00:00:00 2001 From: frantuma Date: Wed, 15 Feb 2023 15:36:04 +0100 Subject: [PATCH 3/6] implement support for custom URL resolvers --- .../reference/DereferencersFactory.java | 2 +- .../v3/parser/reference/ReferenceUtils.java | 57 ------------------- .../v3/parser/reference/ReferenceVisitor.java | 18 +++++- .../swagger/v3/parser/reference/Visitor.java | 49 +++++++++++++++- .../test/CustomOpenAPIDereferencer.java | 51 +++++++++++++++++ .../test/OpenAPIV31ParserFSFullTest.java | 9 +++ .../dereference/custom/dereferenced.yaml | 11 ++++ .../3.1.0/dereference/custom/root.json | 8 +++ ...er.v3.parser.reference.OpenAPIDereferencer | 1 + 9 files changed, 144 insertions(+), 62 deletions(-) create mode 100644 modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/CustomOpenAPIDereferencer.java create mode 100644 modules/swagger-parser-v3/src/test/resources/3.1.0/dereference/custom/dereferenced.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/3.1.0/dereference/custom/root.json create mode 100644 modules/swagger-parser-v3/src/test/resources/META-INF/services/io.swagger.v3.parser.reference.OpenAPIDereferencer diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/DereferencersFactory.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/DereferencersFactory.java index 17eb5ea689..f8fdd40195 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/DereferencersFactory.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/DereferencersFactory.java @@ -47,7 +47,7 @@ public List getDereferencers() { if (ext == null) { LOGGER.error("failed to load extension {}", ext); } else { - instance.addDereferencer(ext); + getInstance().addDereferencer(ext); LOGGER.debug("adding OpenAPIDereferencer: {}", ext); } } diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/ReferenceUtils.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/ReferenceUtils.java index d35c395c76..79f8542d50 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/ReferenceUtils.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/ReferenceUtils.java @@ -1,24 +1,13 @@ package io.swagger.v3.parser.reference; import com.fasterxml.jackson.databind.JsonNode; -import io.swagger.v3.core.util.Json31; -import io.swagger.v3.core.util.Yaml31; -import io.swagger.v3.parser.core.models.AuthorizationValue; -import io.swagger.v3.parser.util.ClasspathHelper; -import io.swagger.v3.parser.util.RemoteUrl; -import org.apache.commons.io.IOUtils; import org.apache.commons.lang3.StringUtils; -import java.io.FileInputStream; -import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.URI; import java.net.URLDecoder; -import java.util.List; import java.util.regex.Pattern; -import static java.nio.charset.StandardCharsets.UTF_8; - public class ReferenceUtils { public static String toBaseURI(String uri) throws Exception @@ -68,52 +57,6 @@ public static boolean isAnchor(String ref) { return false; } - public static String readURI(String absoluteUri, List auths) throws Exception { - URI resolved = new URI(absoluteUri); - if (StringUtils.isNotBlank(resolved.getScheme())) { - if (resolved.getScheme().startsWith("http")) { - return readHttp(absoluteUri, auths); - } else if (resolved.getScheme().startsWith("file")) { - return readFile(absoluteUri); - } else if (resolved.getScheme().startsWith("classpath")) { - return readClasspath(absoluteUri); - } - } - // If no matches exists, try file - String content = null; - try { - content = readFile(absoluteUri); - } catch (Exception e) { - // - } - if (StringUtils.isBlank(content)) { - content = readClasspath(absoluteUri); - } - return content; - } - - public static JsonNode deserializeIntoTree(String content) throws Exception { - boolean isJson = content.trim().startsWith("{"); - return isJson ? Json31.mapper().readTree(content) : Yaml31.mapper().readTree(content); - } - - public static JsonNode parse(String absoluteUri, List auths) throws Exception { - return deserializeIntoTree(readURI(absoluteUri, auths)); - } - - public static String readFile(String uri) throws Exception { - try (InputStream inputStream = new FileInputStream(uri)) { - return IOUtils.toString(inputStream, UTF_8); - } - } - - public static String readClasspath(String uri) throws Exception { - return ClasspathHelper.loadFileFromClasspath(uri); - } - public static String readHttp(String uri, List auths) throws Exception { - return RemoteUrl.urlToString(uri, auths); - } - public static String unescapePointer(String jsonPathElement) { // URL decode the fragment try { diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/ReferenceVisitor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/ReferenceVisitor.java index 555babbddf..9b9a656141 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/ReferenceVisitor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/ReferenceVisitor.java @@ -2,6 +2,8 @@ import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.node.ArrayNode; +import io.swagger.v3.core.util.Json31; +import io.swagger.v3.core.util.Yaml31; import io.swagger.v3.oas.models.PathItem; import io.swagger.v3.oas.models.examples.Example; import io.swagger.v3.oas.models.headers.Header; @@ -11,6 +13,7 @@ import io.swagger.v3.oas.models.parameters.RequestBody; import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.security.SecurityScheme; +import io.swagger.v3.parser.core.models.AuthorizationValue; import org.apache.commons.lang3.StringUtils; import org.slf4j.LoggerFactory; @@ -50,7 +53,7 @@ public Reference toReference(String uri) throws Exception{ if (referenceSet.containsKey(baseUri)) { return referenceSet.get(baseUri); } - JsonNode node = ReferenceUtils.parse(baseUri, this.reference.getAuths()); + JsonNode node = parse(baseUri, this.reference.getAuths()); Reference ref = new Reference() .auths(this.reference.getAuths()) @@ -205,11 +208,11 @@ public Schema resolveSchemaRef(Schema visiting, String ref, List inherit else { JsonNode node = null; try { - node = ReferenceUtils.parse(baseURI, this.reference.getAuths()); + node = parse(baseURI, this.reference.getAuths()); } catch (Exception e) { // we can not parse, try ref baseURI = toBaseURI(ref); - node = ReferenceUtils.parse(baseURI, this.reference.getAuths()); + node = parse(baseURI, this.reference.getAuths()); } reference = toSchemaReference(baseURI, node); } @@ -266,4 +269,13 @@ public JsonNode findAnchor(JsonNode root, String anchor) { return null; } + + public JsonNode deserializeIntoTree(String content) throws Exception { + boolean isJson = content.trim().startsWith("{"); + return isJson ? Json31.mapper().readTree(content) : Yaml31.mapper().readTree(content); + } + + public JsonNode parse(String absoluteUri, List auths) throws Exception { + return deserializeIntoTree(readURI(absoluteUri, auths)); + } } diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/Visitor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/Visitor.java index 651d18f765..d41b21cc22 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/Visitor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/reference/Visitor.java @@ -16,9 +16,19 @@ import io.swagger.v3.oas.models.responses.ApiResponse; import io.swagger.v3.oas.models.responses.ApiResponses; import io.swagger.v3.oas.models.security.SecurityScheme; - +import io.swagger.v3.parser.core.models.AuthorizationValue; +import io.swagger.v3.parser.util.ClasspathHelper; +import io.swagger.v3.parser.util.RemoteUrl; +import org.apache.commons.io.IOUtils; +import org.apache.commons.lang3.StringUtils; + +import java.io.FileInputStream; +import java.io.InputStream; +import java.net.URI; import java.util.List; +import static java.nio.charset.StandardCharsets.UTF_8; + public interface Visitor { OpenAPI visitOpenApi(OpenAPI openAPI); @@ -52,4 +62,41 @@ public interface Visitor { Example visitExample(Example example); + default String readFile(String uri) throws Exception { + try (InputStream inputStream = new FileInputStream(uri)) { + return IOUtils.toString(inputStream, UTF_8); + } + } + + default String readClasspath(String uri) throws Exception { + return ClasspathHelper.loadFileFromClasspath(uri); + } + default String readHttp(String uri, List auths) throws Exception { + return RemoteUrl.urlToString(uri, auths); + } + + default String readURI(String absoluteUri, List auths) throws Exception { + URI resolved = new URI(absoluteUri); + if (StringUtils.isNotBlank(resolved.getScheme())) { + if (resolved.getScheme().startsWith("http")) { + return readHttp(absoluteUri, auths); + } else if (resolved.getScheme().startsWith("file")) { + return readFile(absoluteUri); + } else if (resolved.getScheme().startsWith("classpath")) { + return readClasspath(absoluteUri); + } + } + // If no matches exists, try file + String content = null; + try { + content = readFile(absoluteUri); + } catch (Exception e) { + // + } + if (StringUtils.isBlank(content)) { + content = readClasspath(absoluteUri); + } + return content; + } + } diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/CustomOpenAPIDereferencer.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/CustomOpenAPIDereferencer.java new file mode 100644 index 0000000000..1482cf25ff --- /dev/null +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/CustomOpenAPIDereferencer.java @@ -0,0 +1,51 @@ +package io.swagger.v3.parser.test; + +import io.swagger.v3.parser.core.models.AuthorizationValue; +import io.swagger.v3.parser.reference.DereferencerContext; +import io.swagger.v3.parser.reference.OpenAPI31Traverser; +import io.swagger.v3.parser.reference.OpenAPIDereferencer31; +import io.swagger.v3.parser.reference.Reference; +import io.swagger.v3.parser.reference.ReferenceVisitor; +import io.swagger.v3.parser.reference.Traverser; +import io.swagger.v3.parser.reference.Visitor; + +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; + +public class CustomOpenAPIDereferencer extends OpenAPIDereferencer31 { + @Override + public Visitor buildReferenceVisitor(DereferencerContext context, Reference reference, Traverser traverser) { + return new CustomVisitor(reference, (OpenAPI31Traverser)traverser, new HashSet<>(), new HashMap<>()); + } + + static public class CustomVisitor extends ReferenceVisitor { + + public CustomVisitor(Reference reference, OpenAPI31Traverser openAPITraverser, HashSet visited, + HashMap visitedMap) { + super(reference, openAPITraverser, visited, visitedMap); + } + + @Override + public String readHttp(String uri, List auths) throws Exception { + + if (uri.startsWith("http://example.com/custom")) { + return "openapi: 3.1.0\n" + + "info:\n" + + " title: Domain\n" + + " version: '1.0'\n" + + "components:\n" + + " pathItems:\n" + + " ExternalRef:\n" + + " get:\n" + + " description: ExternalRef domain\n" + + " operationId: ExternalRef PathItem\n" + + " responses:\n" + + " '200':\n" + + " description: OK"; + } else { + return super.readHttp(uri, auths); + } + } + } +} diff --git a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserFSFullTest.java b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserFSFullTest.java index 88d302f0dd..31248ad26f 100644 --- a/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserFSFullTest.java +++ b/modules/swagger-parser-v3/src/test/java/io/swagger/v3/parser/test/OpenAPIV31ParserFSFullTest.java @@ -19,4 +19,13 @@ public void testFull() throws Exception { SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(uri, null, p); org.testng.Assert.assertEquals(Yaml31.pretty(swaggerParseResult.getOpenAPI()), FileUtils.readFileToString(new File("src/test/resources/3.1.0/dereference/fullFS/dereferenced.yaml"))); } + + @Test + public void testCustomUrlResolver() throws Exception { + ParseOptions p = new ParseOptions(); + p.setResolve(true); + String uri = "3.1.0/dereference/custom/root.json"; + SwaggerParseResult swaggerParseResult = new OpenAPIV3Parser().readLocation(uri, null, p); + org.testng.Assert.assertEquals(Yaml31.pretty(swaggerParseResult.getOpenAPI()), FileUtils.readFileToString(new File("src/test/resources/3.1.0/dereference/custom/dereferenced.yaml"))); + } } diff --git a/modules/swagger-parser-v3/src/test/resources/3.1.0/dereference/custom/dereferenced.yaml b/modules/swagger-parser-v3/src/test/resources/3.1.0/dereference/custom/dereferenced.yaml new file mode 100644 index 0000000000..aff2c3cfb4 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/3.1.0/dereference/custom/dereferenced.yaml @@ -0,0 +1,11 @@ +openapi: 3.1.0 +servers: +- url: / +paths: + /externalref: + get: + description: ExternalRef domain + operationId: ExternalRef PathItem + responses: + "200": + description: OK diff --git a/modules/swagger-parser-v3/src/test/resources/3.1.0/dereference/custom/root.json b/modules/swagger-parser-v3/src/test/resources/3.1.0/dereference/custom/root.json new file mode 100644 index 0000000000..a966dbaf57 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/3.1.0/dereference/custom/root.json @@ -0,0 +1,8 @@ +{ + "openapi": "3.1.0", + "paths": { + "/externalref": { + "$ref": "http://example.com/custom/nested/domain.yaml#/components/pathItems/ExternalRef" + } + } +} diff --git a/modules/swagger-parser-v3/src/test/resources/META-INF/services/io.swagger.v3.parser.reference.OpenAPIDereferencer b/modules/swagger-parser-v3/src/test/resources/META-INF/services/io.swagger.v3.parser.reference.OpenAPIDereferencer new file mode 100644 index 0000000000..8a5323b9e0 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/META-INF/services/io.swagger.v3.parser.reference.OpenAPIDereferencer @@ -0,0 +1 @@ +io.swagger.v3.parser.test.CustomOpenAPIDereferencer From 12a4a2e7ebd26ba26df054c975843bbc02e71779 Mon Sep 17 00:00:00 2001 From: frantuma Date: Wed, 15 Feb 2023 14:50:17 +0000 Subject: [PATCH 4/6] prepare release 2.1.12 --- README.md | 2 +- modules/swagger-parser-cli/pom.xml | 4 ++-- modules/swagger-parser-core/pom.xml | 2 +- modules/swagger-parser-v2-converter/pom.xml | 2 +- modules/swagger-parser-v3/pom.xml | 2 +- modules/swagger-parser/pom.xml | 2 +- pom.xml | 2 +- 7 files changed, 8 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index c0112f7f25..74e60a9288 100644 --- a/README.md +++ b/README.md @@ -114,7 +114,7 @@ You can include this library from Sonatype OSS for SNAPSHOTS, or Maven central f io.swagger.parser.v3 swagger-parser - 2.1.11 + 2.1.12 ``` diff --git a/modules/swagger-parser-cli/pom.xml b/modules/swagger-parser-cli/pom.xml index 67188962cd..66bb619183 100644 --- a/modules/swagger-parser-cli/pom.xml +++ b/modules/swagger-parser-cli/pom.xml @@ -5,7 +5,7 @@ swagger-parser-project io.swagger.parser.v3 - 2.1.12-SNAPSHOT + 2.1.12 ../.. 4.0.0 @@ -69,7 +69,7 @@ io.swagger.parser.v3 swagger-parser-v3 - 2.1.12-SNAPSHOT + 2.1.12 compile diff --git a/modules/swagger-parser-core/pom.xml b/modules/swagger-parser-core/pom.xml index 04d72f946c..0dbbd6d676 100644 --- a/modules/swagger-parser-core/pom.xml +++ b/modules/swagger-parser-core/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.12-SNAPSHOT + 2.1.12 ../.. 4.0.0 diff --git a/modules/swagger-parser-v2-converter/pom.xml b/modules/swagger-parser-v2-converter/pom.xml index d7a1168d88..b084cf525f 100644 --- a/modules/swagger-parser-v2-converter/pom.xml +++ b/modules/swagger-parser-v2-converter/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.12-SNAPSHOT + 2.1.12 ../.. 4.0.0 diff --git a/modules/swagger-parser-v3/pom.xml b/modules/swagger-parser-v3/pom.xml index e9a152248e..a185e238eb 100644 --- a/modules/swagger-parser-v3/pom.xml +++ b/modules/swagger-parser-v3/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.12-SNAPSHOT + 2.1.12 ../.. 4.0.0 diff --git a/modules/swagger-parser/pom.xml b/modules/swagger-parser/pom.xml index c6d29efc52..a337e76ec3 100644 --- a/modules/swagger-parser/pom.xml +++ b/modules/swagger-parser/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.12-SNAPSHOT + 2.1.12 ../.. 4.0.0 diff --git a/pom.xml b/pom.xml index f8bba8ebfc..37c6903deb 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ 4.0.0 io.swagger.parser.v3 swagger-parser-project - 2.1.12-SNAPSHOT + 2.1.12 pom swagger-parser-project From 2fa620f6f37b2ea11744c3441cfc991c55e54d8b Mon Sep 17 00:00:00 2001 From: frantuma Date: Wed, 15 Feb 2023 15:01:15 +0000 Subject: [PATCH 5/6] bump snapshot 2.1.13-SNAPSHOT --- modules/swagger-parser-cli/pom.xml | 4 ++-- modules/swagger-parser-core/pom.xml | 2 +- modules/swagger-parser-v2-converter/pom.xml | 2 +- modules/swagger-parser-v3/pom.xml | 2 +- modules/swagger-parser/pom.xml | 2 +- pom.xml | 2 +- 6 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/swagger-parser-cli/pom.xml b/modules/swagger-parser-cli/pom.xml index 66bb619183..2f3557bdb1 100644 --- a/modules/swagger-parser-cli/pom.xml +++ b/modules/swagger-parser-cli/pom.xml @@ -5,7 +5,7 @@ swagger-parser-project io.swagger.parser.v3 - 2.1.12 + 2.1.13-SNAPSHOT ../.. 4.0.0 @@ -69,7 +69,7 @@ io.swagger.parser.v3 swagger-parser-v3 - 2.1.12 + 2.1.13-SNAPSHOT compile diff --git a/modules/swagger-parser-core/pom.xml b/modules/swagger-parser-core/pom.xml index 0dbbd6d676..b567642091 100644 --- a/modules/swagger-parser-core/pom.xml +++ b/modules/swagger-parser-core/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.12 + 2.1.13-SNAPSHOT ../.. 4.0.0 diff --git a/modules/swagger-parser-v2-converter/pom.xml b/modules/swagger-parser-v2-converter/pom.xml index b084cf525f..e364e2a6ab 100644 --- a/modules/swagger-parser-v2-converter/pom.xml +++ b/modules/swagger-parser-v2-converter/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.12 + 2.1.13-SNAPSHOT ../.. 4.0.0 diff --git a/modules/swagger-parser-v3/pom.xml b/modules/swagger-parser-v3/pom.xml index a185e238eb..51216143bc 100644 --- a/modules/swagger-parser-v3/pom.xml +++ b/modules/swagger-parser-v3/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.12 + 2.1.13-SNAPSHOT ../.. 4.0.0 diff --git a/modules/swagger-parser/pom.xml b/modules/swagger-parser/pom.xml index a337e76ec3..79005d74ed 100644 --- a/modules/swagger-parser/pom.xml +++ b/modules/swagger-parser/pom.xml @@ -3,7 +3,7 @@ io.swagger.parser.v3 swagger-parser-project - 2.1.12 + 2.1.13-SNAPSHOT ../.. 4.0.0 diff --git a/pom.xml b/pom.xml index 37c6903deb..8875231041 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ 4.0.0 io.swagger.parser.v3 swagger-parser-project - 2.1.12 + 2.1.13-SNAPSHOT pom swagger-parser-project From 092438dd57b05c4a949b182c66728d8b066bcb69 Mon Sep 17 00:00:00 2001 From: Francesco Tumanischvili Date: Fri, 17 Feb 2023 09:23:44 +0100 Subject: [PATCH 6/6] Initial dependabot for Swagger version updates --- .github/dependabot.yml | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 .github/dependabot.yml diff --git a/.github/dependabot.yml b/.github/dependabot.yml new file mode 100644 index 0000000000..2d1daffbfd --- /dev/null +++ b/.github/dependabot.yml @@ -0,0 +1,18 @@ +version: 2 +updates: + - package-ecosystem: "maven" + target-branch: "master" + directory: "/" + schedule: + interval: "daily" + allow: + - dependency-name: "io.swagger.core.v3:*" + - dependency-name: "io.swagger:*" + - package-ecosystem: "maven" + target-branch: "v1" + directory: "/" + schedule: + interval: "daily" + allow: + - dependency-name: "io.swagger.core.v3:*" + - dependency-name: "io.swagger:*"