Skip to content

Commit 598bf0c

Browse files
jminiwing328
authored andcommitted
[core] consider schema in headers when computing unused schemas (#2138)
1 parent b6c2266 commit 598bf0c

File tree

3 files changed

+49
-6
lines changed

3 files changed

+49
-6
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/utils/ModelUtils.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,20 @@ private static void visitPathItem(PathItem pathItem, OpenAPI openAPI, OpenAPISch
192192
if (operation.getResponses() != null) {
193193
for (ApiResponse r : operation.getResponses().values()) {
194194
ApiResponse apiResponse = getReferencedApiResponse(openAPI, r);
195-
if (apiResponse != null && apiResponse.getContent() != null) {
196-
for (Entry<String, MediaType> e : apiResponse.getContent().entrySet()) {
197-
if (e.getValue().getSchema() != null) {
198-
visitSchema(openAPI, e.getValue().getSchema(), e.getKey(), visitedSchemas, visitor);
195+
if (apiResponse != null) {
196+
if (apiResponse.getContent() != null) {
197+
for (Entry<String, MediaType> e : apiResponse.getContent().entrySet()) {
198+
if (e.getValue().getSchema() != null) {
199+
visitSchema(openAPI, e.getValue().getSchema(), e.getKey(), visitedSchemas, visitor);
200+
}
201+
}
202+
}
203+
if (apiResponse.getHeaders() != null) {
204+
for (Entry<String, Header> e : apiResponse.getHeaders().entrySet()) {
205+
Header header = getReferencedHeader(openAPI, e.getValue());
206+
if (header.getSchema() != null) {
207+
visitSchema(openAPI, header.getSchema(), e.getKey(), visitedSchemas, visitor);
208+
}
199209
}
200210
}
201211
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/utils/ModelUtilsTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class ModelUtilsTest {
3737
public void testGetAllUsedSchemas() {
3838
final OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/unusedSchemas.yaml");
3939
List<String> allUsedSchemas = ModelUtils.getAllUsedSchemas(openAPI);
40-
Assert.assertEquals(allUsedSchemas.size(), 32);
40+
Assert.assertEquals(allUsedSchemas.size(), 34);
4141

4242
Assert.assertTrue(allUsedSchemas.contains("SomeObjShared"), "contains 'SomeObjShared'");
4343
Assert.assertTrue(allUsedSchemas.contains("SomeObj1"), "contains 'UnusedObj1'");
@@ -71,6 +71,8 @@ public void testGetAllUsedSchemas() {
7171
Assert.assertTrue(allUsedSchemas.contains("PingDataOutput21"), "contains 'PingDataOutput21'");
7272
Assert.assertTrue(allUsedSchemas.contains("SInput22"), "contains 'SInput22'");
7373
Assert.assertTrue(allUsedSchemas.contains("SOutput22"), "contains 'SInput22'");
74+
Assert.assertTrue(allUsedSchemas.contains("SomeHeader23"), "contains 'SomeHeader23'");
75+
Assert.assertTrue(allUsedSchemas.contains("SomeHeader24"), "contains 'SomeHeader24'");
7476
}
7577

7678
@Test

modules/openapi-generator/src/test/resources/3_0/unusedSchemas.yaml

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,27 @@ paths:
280280
callbacks:
281281
sharedCallback:
282282
$ref: "#/components/callbacks/sharedCallback22"
283+
/some/p23:
284+
post:
285+
operationId: op23
286+
responses:
287+
'2XX':
288+
description: OK
289+
headers:
290+
x-app-info:
291+
description: basic app info
292+
style: simple
293+
schema:
294+
$ref: "#/components/schemas/SomeHeader23"
295+
/some/p24:
296+
post:
297+
operationId: op24
298+
responses:
299+
'2XX':
300+
description: OK
301+
headers:
302+
x-app-info:
303+
$ref: "#/components/headers/sharedHeader24"
283304
components:
284305
schemas:
285306
UnusedObj1:
@@ -538,6 +559,11 @@ components:
538559
type: String
539560
response:
540561
type: String
562+
SomeHeader23:
563+
type: string
564+
SomeHeader24:
565+
type: integer
566+
format: int64
541567
SomeObjShared:
542568
type: object
543569
properties:
@@ -607,4 +633,9 @@ components:
607633
application/json:
608634
schema:
609635
$ref: '#/components/schemas/SOutput22'
610-
636+
headers:
637+
sharedHeader24:
638+
description: basic header
639+
style: simple
640+
schema:
641+
$ref: "#/components/schemas/SomeHeader24"

0 commit comments

Comments
 (0)