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

Enhancement/parser resolve responses flag #2127

Merged
merged 2 commits into from
Oct 22, 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 @@ -15,7 +15,7 @@ public class ParseOptions {
private boolean validateInternalRefs = true;
private boolean legacyYamlDeserialization = false;
private boolean resolveRequestBody = false;

private boolean resolveResponses = false;
private boolean oaiAuthor;
private boolean inferSchemaType = true;
private boolean safelyResolveURL;
Expand Down Expand Up @@ -161,4 +161,12 @@ public List<String> getRemoteRefBlockList() {
public void setRemoteRefBlockList(List<String> remoteRefBlockList) {
this.remoteRefBlockList = remoteRefBlockList;
}

public boolean isResolveResponses() {
return resolveResponses;
}

public void setResolveResponses(boolean resolveResponses) {
this.resolveResponses = resolveResponses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ public void processOperation(Operation operation) {
for (String responseCode : responses.keySet()) {
ApiResponse response = responses.get(responseCode);
if(response != null) {
//This part allows parser to put response inline without the resolveFully option set to true
if (response.get$ref() != null) {
//This part allows parser to put response inline when resolveResponses = true
if (response.get$ref() != null && cache != null && cache.getParseOptions() != null && cache.getParseOptions().isResolveResponses()) {

responseProcessor.processResponse(response);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ public void testIssue411() throws Exception {
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveResponses(true);
SwaggerParseResult result = parser.readLocation("http://remote1/resources/swagger.yaml", auths, options);

OpenAPI swagger = result.getOpenAPI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,8 +766,9 @@ public void testIssue1706() {

ParseOptions options = new ParseOptions();
options.setResolve(true);
// Added this parseOption to make requestBody inline in the operation resolve processing flow.
// Added this parseOption to make requestBody/response inline in the operation resolve processing flow.
options.setResolveRequestBody(true);
options.setResolveResponses(true);

OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, null, options).getOpenAPI();

Expand All @@ -776,7 +777,7 @@ public void testIssue1706() {
assertTrue(openAPI.getPaths().get("/resource").getPost().getRequestBody().getContent() != null);
assertTrue(openAPI.getPaths().get("/resource").getPost().getRequestBody().getContent().get("application/json").getSchema() instanceof ObjectSchema);

// Responses are already by default made inline in case referenced.
// Responses should be inline
assertTrue(openAPI.getPaths().get("/resource").getPost().getResponses().get("200").get$ref() == null);
assertTrue(openAPI.getPaths().get("/resource").getPost().getResponses().get("200").getContent() != null);
assertTrue(openAPI.getPaths().get("/resource").getPost().getResponses().get("200").getContent().get("application/json").getSchema() instanceof ObjectSchema);
Expand Down Expand Up @@ -1285,6 +1286,8 @@ public void testSharedSwaggerParametersTest() {
@Test(description = "resolve top-level responses")
public void testSharedResponses() {
final OpenAPI swagger = new OpenAPI();
ParseOptions parseOptions = new ParseOptions();
parseOptions.setResolveResponses(true);
List<Parameter> parameters = new ArrayList<>();
parameters.add(0,new Parameter().$ref("username"));
swagger.path("/fun", new PathItem()
Expand All @@ -1294,7 +1297,7 @@ public void testSharedResponses() {

swagger.components(new Components().addResponses("foo", new ApiResponse().description("ok!")));

final OpenAPI resolved = new OpenAPIResolver(swagger, null).resolve();
final OpenAPI resolved = new OpenAPIResolver(swagger, null, null, null, parseOptions).resolve();
ApiResponse response = resolved.getPaths().get("/fun").getGet().getResponses().get("200");
assertTrue(response.getDescription().equals("ok!"));
assertTrue(response instanceof ApiResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ public void testIssue1780() {
public void testParametersAndResponsesAsNumbers() throws Exception {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveResponses(true);
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("src/test/resources/parametersAsNumbers/swagger.yaml", null, options);

Assert.assertNotNull(result);
Expand Down Expand Up @@ -1214,6 +1215,7 @@ public void testIssue1131() {
public void testIssue834() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveResponses(true);
SwaggerParseResult result = new OpenAPIV3Parser().readLocation("issue-834/index.yaml", null, options);
assertNotNull(result.getOpenAPI());

Expand Down Expand Up @@ -1247,8 +1249,8 @@ public void testIssue811_RefSchema_ToRefSchema() {
public void testIssue811() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveResponses(true);
final OpenAPI openAPI = new OpenAPIV3Parser().readLocation("oapi-reference-test/index.yaml", null, options).getOpenAPI();

Assert.assertNotNull(openAPI);
Assert.assertEquals(openAPI.getPaths().get("/").getGet().getResponses().get("200").getContent().get("application/json").getSchema().get$ref(),"#/components/schemas/schema-with-reference");
}
Expand Down Expand Up @@ -1940,7 +1942,9 @@ public void testRelativePath() {
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveResponses(true);
SwaggerParseResult readResult = parser.readLocation("src/test/resources/relative-issue/api.yaml", null, options);
Yaml.prettyPrint(readResult.getOpenAPI());
Assert.assertEquals(readResult.getOpenAPI().getPaths().get("/scans").getGet().getResponses().get("500").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ErrorMessage");
}

Expand All @@ -1949,6 +1953,7 @@ public void testRelativePath2() {
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveResponses(true);
SwaggerParseResult readResult = parser.readLocation("src/test/resources/codegen-remote-responses/openapi.yaml", null, options);
Assert.assertEquals(readResult.getOpenAPI().getPaths().get("/pet/findByTags").getGet().getResponses().get("default").getContent().get("application/json").getSchema().get$ref(), "#/components/schemas/ErrorModel");
}
Expand All @@ -1957,6 +1962,7 @@ private OpenAPI doRelativeFileTest(String location) {
OpenAPIV3Parser parser = new OpenAPIV3Parser();
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveResponses(true);
SwaggerParseResult readResult = parser.readLocation(location, null, options);

if (readResult.getMessages().size() > 0) {
Expand Down Expand Up @@ -2769,6 +2775,7 @@ public void testIssue1177() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveFully(true);
options.setResolveResponses(true);

OpenAPI openAPI = new OpenAPIV3Parser().readLocation(path, auths, options).getOpenAPI();

Expand Down Expand Up @@ -3298,4 +3305,51 @@ public void testIssue2081() {
assertEquals(openAPI.getComponents().getSchemas().get("PetCreate").getRequired().size(), 1);
assertEquals(openAPI.getComponents().getSchemas().get("PetCreate").getProperties().size(), 2);
}

@Test(description = "responses should be inline")
public void testFullyResolveResponses() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setResolveResponses(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());
assertNull(openAPI.getPaths().get("/users").getPost().getResponses().get("400").get$ref());
assertNull(openAPI.getPaths().get("/users").getPost().getResponses().get("422").get$ref());
assertNull(openAPI.getPaths().get("/users/{userId}").getGet().getResponses().get("404").get$ref());
assertNull(openAPI.getPaths().get("/users/{userId}").getPut().getResponses().get("400").get$ref());
assertNull(openAPI.getPaths().get("/users/{userId}").getPut().getResponses().get("404").get$ref());
assertNull(openAPI.getPaths().get("/users/{userId}").getDelete().getResponses().get("400").get$ref());
assertNull(openAPI.getPaths().get("/users/{userId}").getDelete().getResponses().get("404").get$ref());
}

@Test(description = "responses should not be inline")
public void testResolveResponsesRef() {
ParseOptions options = new ParseOptions();
options.setResolve(true);
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
SwaggerParseResult parseResult = openApiParser.readLocation("resolve-responses-test.yaml", null, options);
OpenAPI openAPI = parseResult.getOpenAPI();
assertEquals(openAPI.getPaths().get("/users").getGet().getResponses().get("400").get$ref(), "#/components/responses/BadRequest");
assertEquals(openAPI.getPaths().get("/users").getPost().getResponses().get("400").get$ref(), "#/components/responses/BadRequest");
assertEquals(openAPI.getPaths().get("/users").getPost().getResponses().get("422").get$ref(), "#/components/responses/UnprocessableEntity");
assertEquals(openAPI.getPaths().get("/users/{userId}").getGet().getResponses().get("404").get$ref(), "#/components/responses/NotFound");
assertEquals(openAPI.getPaths().get("/users/{userId}").getPut().getResponses().get("400").get$ref(), "#/components/responses/BadRequest");
assertEquals(openAPI.getPaths().get("/users/{userId}").getPut().getResponses().get("404").get$ref(), "#/components/responses/NotFound");
assertEquals(openAPI.getPaths().get("/users/{userId}").getDelete().getResponses().get("400").get$ref(), "#/components/responses/BadRequest");
assertEquals(openAPI.getPaths().get("/users/{userId}").getDelete().getResponses().get("404").get$ref(), "#/components/responses/NotFound");
}

@Test
public void testResolveOASWithFlatten(){
ParseOptions options = new ParseOptions();
options.setResolve(true);
options.setFlatten(true);
OpenAPIV3Parser openApiParser = new OpenAPIV3Parser();
SwaggerParseResult parseResult = openApiParser.readLocation("resolve-flatten-SH-configuration-test.yaml", null, options);
OpenAPI openAPI = parseResult.getOpenAPI();
assertNull(openAPI.getComponents().getSchemas().get("#/components/schemas/inline_response_404"));
assertNull(openAPI.getComponents().getSchemas().get("#/components/schemas/inline_response_200"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
openapi: 3.0.1
info:
title: Simple API
version: 1.0.0

paths:
/users:
get:
summary: Get list of users
operationId: getUsers
responses:
'200':
$ref: '#/components/responses/SuccessResponse'
'404':
$ref: '#/components/responses/NotFoundResponse'

components:
responses:
SuccessResponse:
description: A successful response
content:
application/json:
schema:
type: array
items:
type: object
properties:
id:
type: integer
name:
type: string
email:
type: string
NotFoundResponse:
description: Not Found response
content:
application/json:
schema:
type: object
properties:
message:
type: string
example: "Resource not found"
Loading
Loading