From 89be06a0c19caf83b0b3b80fd984f519ab37df62 Mon Sep 17 00:00:00 2001 From: gracekarina Date: Fri, 2 Feb 2024 14:32:13 -0500 Subject: [PATCH] add test and fix response resolver --- .../v3/parser/processors/PathsProcessor.java | 18 ++++++++++++++--- .../v3/parser/test/OpenAPIV3ParserTest.java | 12 +++++++++++ .../test/resources/issue-2037/openapi.yaml | 8 ++++++++ .../test/resources/issue-2037/paths/get.yaml | 20 +++++++++++++++++++ 4 files changed, 55 insertions(+), 3 deletions(-) create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-2037/openapi.yaml create mode 100644 modules/swagger-parser-v3/src/test/resources/issue-2037/paths/get.yaml diff --git a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java index fdbbc48d57..467cb8538a 100644 --- a/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java +++ b/modules/swagger-parser-v3/src/main/java/io/swagger/v3/parser/processors/PathsProcessor.java @@ -172,6 +172,10 @@ protected void updateRefs(ApiResponse response, String pathRef) { if(response.getContent() != null) { Map content = response.getContent(); for (String key: content.keySet()) { + MediaType mediaType = content.get(key); + if (mediaType.getSchema() != null) { + updateRefs(mediaType.getSchema(), pathRef); + } Map examples = content.get(key).getExamples(); if (examples != null) { for( Example example:examples.values()){ @@ -285,10 +289,18 @@ protected boolean isAbsoluteRef(String ref) { return false; } + private boolean isInternalSchemaRef(String $ref) { + if($ref.startsWith("#/components/schemas")) { + return true; + } + return false; + } + protected String computeRef(String ref, String prefix) { - if(isLocalRef(ref)) return computeLocalRef(ref, prefix); - if(isAbsoluteRef(ref)) return ref; - return computeRelativeRef(ref, prefix); + if(isLocalRef(ref)&& !isInternalSchemaRef(ref)) return computeLocalRef(ref, prefix); + if(isAbsoluteRef(ref)) return ref; + if(isInternalSchemaRef(ref)) return ref; + return computeRelativeRef(ref, prefix); } protected String computeRelativeRef(String ref, String prefix) { 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 01f69e1b0c..9a6aa67f08 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 @@ -43,6 +43,18 @@ public class OpenAPIV3ParserTest { List auths = new ArrayList<>(); + @Test + public void testFailedToResolveResponseReferences() { + OpenAPIV3Parser openApiParser = new OpenAPIV3Parser(); + ParseOptions options = new ParseOptions(); + options.setResolve(true); + SwaggerParseResult parseResult = openApiParser.readLocation("issue-2037/openapi.yaml", null, options); + OpenAPI openAPI = parseResult.getOpenAPI(); + + Assert.assertTrue(openAPI.getPaths().get("/get").get$ref() == null); + Assert.assertEquals(openAPI.getPaths().get("/get").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ResponsesRef"); + } + @Test public void testFailedToResolveExternalReferences() { diff --git a/modules/swagger-parser-v3/src/test/resources/issue-2037/openapi.yaml b/modules/swagger-parser-v3/src/test/resources/issue-2037/openapi.yaml new file mode 100644 index 0000000000..a356cb4532 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-2037/openapi.yaml @@ -0,0 +1,8 @@ +openapi: 3.0.3 +info: + title: title + version: LATEST + +paths: + /get: + $ref: 'paths/get.yaml#/endpoint' \ No newline at end of file diff --git a/modules/swagger-parser-v3/src/test/resources/issue-2037/paths/get.yaml b/modules/swagger-parser-v3/src/test/resources/issue-2037/paths/get.yaml new file mode 100644 index 0000000000..9f5b7b0738 --- /dev/null +++ b/modules/swagger-parser-v3/src/test/resources/issue-2037/paths/get.yaml @@ -0,0 +1,20 @@ +endpoint: + get: + operationId: get + requestBody: + content: + application/json: + schema: + $ref: '#/RequestBodyRef' + responses: + '200': + content: + application/json: + schema: + $ref: '#/ResponsesRef' + +RequestBodyRef: + type: string + +ResponsesRef: + type: string \ No newline at end of file