Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add logic to process responses with resolvFully parsing option #2131

Merged
merged 1 commit into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());

}
}
Loading