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 814035e9e81b6950e23f10bcbe4752786d90a7fb..4a3f943fd77062ba55038a0ccc86439060680e14 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 @@ public class ModelUtils { 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: @@ -194,19 +183,14 @@ public class ModelUtils { 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); } } } @@ -226,6 +210,31 @@ public class ModelUtils { } } } + //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) { 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 34532c35ad4eec120ee340b0e479fc23cd8e6726..4f428705a19dacac183cc2478e30436c00633460 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<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'"); @@ -73,6 +73,10 @@ public class ModelUtilsTest { 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 7b09f13004cd87d4726242703d71b29c7e19c5ad..57027f58027629e1230df14edddc89be593bb781 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: