From 0e945bd1c16ecd5dac0879bf872f185ffd3690cd Mon Sep 17 00:00:00 2001 From: MicRyc Date: Mon, 28 Oct 2024 10:31:15 +0100 Subject: [PATCH] Add logic to process responses with resolvFully parsing option --- .../swagger/v3/parser/util/ResolverFully.java | 24 +++++++++++++++++++ .../v3/parser/test/OpenAPIV3ParserTest.java | 12 ++++++++++ 2 files changed, 36 insertions(+) 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..497260ab8a 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 @@ -52,6 +52,7 @@ public ResolverFully(boolean aggregateCombinators) { private Map resolvedModels = new HashMap<>(); private Map examples; private Map parameters; + private Map responses; private Map requestBodies; private Map headers; private Map links; @@ -74,6 +75,13 @@ public void resolveFully(OpenAPI openAPI) { } } + if (components != null && components.getResponses() != null) { + responses = components.getResponses(); + if (responses == null) { + responses = new HashMap<>(); + } + } + if (components != null && components.getExamples() != null) { examples = components.getExamples(); if (examples == null) { @@ -178,8 +186,10 @@ public void resolvePath(PathItem pathItem){ // responses ApiResponses responses = op.getResponses(); if(responses != null) { + ApiResponses resolvedResponses = new ApiResponses(); for(String code : responses.keySet()) { ApiResponse response = responses.get(code); + response = response.get$ref() != null ? resolveResponse(response) : response; if (response.getContent() != null) { Map content = response.getContent(); for(String mediaType: content.keySet()){ @@ -205,7 +215,9 @@ public void resolvePath(PathItem pathItem){ link.setValue(resolvedValue); } } + resolvedResponses.addApiResponse(code, response); } + op.setResponses(resolvedResponses); } } } @@ -274,6 +286,18 @@ public RequestBody resolveRequestBody(RequestBody requestBody){ return requestBody; } + public ApiResponse resolveResponse(ApiResponse apiResponse){ + RefFormat refFormat = computeRefFormat(apiResponse.get$ref()); + String $ref = apiResponse.get$ref(); + if (!isAnExternalRefFormat(refFormat)){ + if (responses != null && !responses.isEmpty()) { + String referenceKey = computeDefinitionName($ref); + return responses.getOrDefault(referenceKey, apiResponse); + } + } + return apiResponse; + } + public Callback resolveCallback(Callback callback){ RefFormat refFormat = computeRefFormat(callback.get$ref()); String $ref = callback.get$ref(); 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 e7e4a55566..24b3259f00 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 @@ -3352,4 +3352,16 @@ public void testResolveOASWithFlatten(){ assertNull(openAPI.getComponents().getSchemas().get("#/components/schemas/inline_response_404")); assertNull(openAPI.getComponents().getSchemas().get("#/components/schemas/inline_response_200")); } + + @Test(description = "responses should be inline with using resolveFully = true") + public void testResolveFullyResponses(){ + ParseOptions options = new ParseOptions(); + options.setResolve(true); + options.setResolveFully(true); + OpenAPIV3Parser openApiParser = new OpenAPIV3Parser(); + SwaggerParseResult parseResult = openApiParser.readLocation("resolve-responses-test.yaml", null, options); + OpenAPI openAPI = parseResult.getOpenAPI(); + assertNull(openAPI.getPaths().get("/users").getGet().getResponses().get("400").get$ref()); + + } } \ No newline at end of file