Skip to content

Commit

Permalink
Merge pull request #2131 from swagger-api/enhancement/ResolverFully-R…
Browse files Browse the repository at this point in the history
…esponses

Add logic to process responses with resolvFully parsing option
  • Loading branch information
micryc authored Oct 28, 2024
2 parents 6bdd4be + 0e945bd commit 3f51a62
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public ResolverFully(boolean aggregateCombinators) {
private Map<String, Schema> resolvedModels = new HashMap<>();
private Map<String, Example> examples;
private Map<String, Parameter> parameters;
private Map<String, ApiResponse> responses;
private Map<String, RequestBody> requestBodies;
private Map<String, Header> headers;
private Map<String, Link> links;
Expand All @@ -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) {
Expand Down Expand Up @@ -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<String, MediaType> content = response.getContent();
for(String mediaType: content.keySet()){
Expand All @@ -205,7 +215,9 @@ public void resolvePath(PathItem pathItem){
link.setValue(resolvedValue);
}
}
resolvedResponses.addApiResponse(code, response);
}
op.setResponses(resolvedResponses);
}
}
}
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());

}
}

0 comments on commit 3f51a62

Please sign in to comment.