Skip to content

Commit

Permalink
Add tests for setResolveResponses flag/ Fix existing ones
Browse files Browse the repository at this point in the history
  • Loading branch information
micryc committed Oct 21, 2024
1 parent a027b2e commit 6bed0f6
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ 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
//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

0 comments on commit 6bed0f6

Please sign in to comment.