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

[core] consider content in parameters and headers when computing unused schemas #3243

Merged
merged 2 commits into from
Jul 4, 2019
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 @@ -170,43 +170,27 @@ private static void visitPathItem(PathItem pathItem, OpenAPI openAPI, OpenAPISch
if (allOperations != null) {
for (Operation operation : allOperations) {
//Params:
if (operation.getParameters() != null) {
for (Parameter p : operation.getParameters()) {
Parameter parameter = getReferencedParameter(openAPI, p);
if (parameter.getSchema() != null) {
visitSchema(openAPI, parameter.getSchema(), null, visitedSchemas, visitor);
}
}
}
visitParameters(openAPI, operation.getParameters(), visitor, visitedSchemas);

//RequestBody:
RequestBody requestBody = getReferencedRequestBody(openAPI, operation.getRequestBody());
if (requestBody != null && requestBody.getContent() != null) {
for (Entry<String, MediaType> e : requestBody.getContent().entrySet()) {
if (e.getValue().getSchema() != null) {
visitSchema(openAPI, e.getValue().getSchema(), e.getKey(), visitedSchemas, visitor);
}
}
if (requestBody != null) {
visitContent(openAPI, requestBody.getContent(), visitor, visitedSchemas);
}

//Responses:
if (operation.getResponses() != null) {
for (ApiResponse r : operation.getResponses().values()) {
ApiResponse apiResponse = getReferencedApiResponse(openAPI, r);
if (apiResponse != null) {
if (apiResponse.getContent() != null) {
for (Entry<String, MediaType> e : apiResponse.getContent().entrySet()) {
if (e.getValue().getSchema() != null) {
visitSchema(openAPI, e.getValue().getSchema(), e.getKey(), visitedSchemas, visitor);
}
}
}
visitContent(openAPI, apiResponse.getContent(), visitor, visitedSchemas);
if (apiResponse.getHeaders() != null) {
for (Entry<String, Header> e : apiResponse.getHeaders().entrySet()) {
Header header = getReferencedHeader(openAPI, e.getValue());
if (header.getSchema() != null) {
visitSchema(openAPI, header.getSchema(), e.getKey(), visitedSchemas, visitor);
}
visitContent(openAPI, header.getContent(), visitor, visitedSchemas);
}
}
}
Expand All @@ -226,6 +210,31 @@ private static void visitPathItem(PathItem pathItem, OpenAPI openAPI, OpenAPISch
}
}
}
//Params:
visitParameters(openAPI, pathItem.getParameters(), visitor, visitedSchemas);
}

private static void visitParameters(OpenAPI openAPI, List<Parameter> parameters, OpenAPISchemaVisitor visitor,
List<String> visitedSchemas) {
if (parameters != null) {
for (Parameter p : parameters) {
Parameter parameter = getReferencedParameter(openAPI, p);
if (parameter.getSchema() != null) {
visitSchema(openAPI, parameter.getSchema(), null, visitedSchemas, visitor);
}
visitContent(openAPI, parameter.getContent(), visitor, visitedSchemas);
}
}
}

private static void visitContent(OpenAPI openAPI, Content content, OpenAPISchemaVisitor visitor, List<String> visitedSchemas) {
if (content != null) {
for (Entry<String, MediaType> e : content.entrySet()) {
if (e.getValue().getSchema() != null) {
visitSchema(openAPI, e.getValue().getSchema(), e.getKey(), visitedSchemas, visitor);
}
}
}
}

private static void visitSchema(OpenAPI openAPI, Schema schema, String mimeType, List<String> visitedSchemas, OpenAPISchemaVisitor visitor) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public class ModelUtilsTest {
public void testGetAllUsedSchemas() {
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/unusedSchemas.yaml");
List<String> allUsedSchemas = ModelUtils.getAllUsedSchemas(openAPI);
Assert.assertEquals(allUsedSchemas.size(), 34);
Assert.assertEquals(allUsedSchemas.size(), 38);

Assert.assertTrue(allUsedSchemas.contains("SomeObjShared"), "contains 'SomeObjShared'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj1"), "contains 'UnusedObj1'");
Expand Down Expand Up @@ -73,6 +73,10 @@ public void testGetAllUsedSchemas() {
Assert.assertTrue(allUsedSchemas.contains("SOutput22"), "contains 'SInput22'");
Assert.assertTrue(allUsedSchemas.contains("SomeHeader23"), "contains 'SomeHeader23'");
Assert.assertTrue(allUsedSchemas.contains("SomeHeader24"), "contains 'SomeHeader24'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj25"), "contains 'SomeObj25'");
Assert.assertTrue(allUsedSchemas.contains("SomeObj26"), "contains 'SomeObj26'");
Assert.assertTrue(allUsedSchemas.contains("Param27"), "contains 'Param27'");
Assert.assertTrue(allUsedSchemas.contains("Param28"), "contains 'Param28'");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,57 @@ paths:
headers:
x-app-info:
$ref: "#/components/headers/sharedHeader24"
/some/p25:
post:
operationId: op25
parameters:
- name: q
in: query
content:
application/json:
schema:
$ref: "#/components/schemas/SomeObj25"
responses:
'200':
description: OK
/some/p26:
post:
operationId: op26
responses:
'200':
description: OK
headers:
x-something:
description: basic app info
style: simple
content:
application/json:
schema:
$ref: "#/components/schemas/SomeObj26"
/some/p27/{q}:
parameters:
- name: q
in: path
schema:
$ref: "#/components/schemas/Param27"
post:
operationId: op27
responses:
'200':
description: OK
/some/p28/{q}:
parameters:
- name: q
in: path
content:
application/json:
schema:
$ref: "#/components/schemas/Param28"
post:
operationId: op27
responses:
'200':
description: OK
components:
schemas:
UnusedObj1:
Expand Down Expand Up @@ -564,6 +615,32 @@ components:
SomeHeader24:
type: integer
format: int64
SomeObj25:
type: object
properties:
p1:
type: string
p2:
type: string
SomeObj26:
type: object
properties:
q1:
type: string
q2:
type: string
Param27:
type: string
enum:
- alpha
- beta
Param28:
type: object
properties:
r1:
type: string
r2:
type: string
SomeObjShared:
type: object
properties:
Expand Down