From 8d27857f8328ebd772e64a5250734235872e2932 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Bresson?= Date: Thu, 4 Jul 2019 14:47:24 +0200 Subject: [PATCH] [core] consider content in parameters and headers when computing unused schemas (#3243) * [core] consider content in parameters and headers when computing unused schemas * consider parameters in PathItem --- .../codegen/utils/ModelUtils.java | 51 +++++++----- .../codegen/utils/ModelUtilsTest.java | 6 +- .../src/test/resources/3_0/unusedSchemas.yaml | 77 +++++++++++++++++++ 3 files changed, 112 insertions(+), 22 deletions(-) diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java index 814035e9e81b..4a3f943fd770 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java @@ -170,23 +170,12 @@ 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 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: @@ -194,19 +183,14 @@ private static void visitPathItem(PathItem pathItem, OpenAPI openAPI, OpenAPISch for (ApiResponse r : operation.getResponses().values()) { ApiResponse apiResponse = getReferencedApiResponse(openAPI, r); if (apiResponse != null) { - if (apiResponse.getContent() != null) { - for (Entry 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 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); } } } @@ -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 parameters, OpenAPISchemaVisitor visitor, + List 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 visitedSchemas) { + if (content != null) { + for (Entry 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 visitedSchemas, OpenAPISchemaVisitor visitor) { diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java index 34532c35ad4e..4f428705a19d 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java @@ -37,7 +37,7 @@ public class ModelUtilsTest { public void testGetAllUsedSchemas() { final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/unusedSchemas.yaml"); List 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'"); @@ -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 diff --git a/modules/openapi-generator/src/test/resources/3_0/unusedSchemas.yaml b/modules/openapi-generator/src/test/resources/3_0/unusedSchemas.yaml index 7b09f13004cd..57027f580276 100644 --- a/modules/openapi-generator/src/test/resources/3_0/unusedSchemas.yaml +++ b/modules/openapi-generator/src/test/resources/3_0/unusedSchemas.yaml @@ -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: @@ -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: