Skip to content

Commit

Permalink
Add logic to process responses with resolvFully parsing option
Browse files Browse the repository at this point in the history
  • Loading branch information
micryc committed Oct 28, 2024
1 parent 6bdd4be commit 0e945bd
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 0e945bd

Please sign in to comment.