Skip to content

Commit e6f94c5

Browse files
committed
Auto fix invalid schemas in inline resolver
1 parent 816befc commit e6f94c5

4 files changed

Lines changed: 78 additions & 13 deletions

File tree

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -353,18 +353,22 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
353353
}
354354
}
355355
}
356-
} else if (schema.getProperties() != null) {
357-
// If non-object type is specified but also properties
358-
LOGGER.error("Illegal schema found with non-object type combined with properties," +
359-
" no properties should be defined:\n " + schema.toString());
360-
return;
361-
} else if (schema.getAdditionalProperties() != null) {
362-
// If non-object type is specified but also additionalProperties
363-
LOGGER.error("Illegal schema found with non-object type combined with" +
364-
" additionalProperties, no additionalProperties should be defined:\n " +
365-
schema.toString());
366-
return;
356+
} else {
357+
if (schema.getProperties() != null) {
358+
// If non-object type is specified but also properties
359+
LOGGER.warn("Illegal schema found with non-object type ({}) combined with properties. Properties automatically removed.", schema.getType());
360+
schema.setProperties(null);
361+
return;
362+
}
363+
364+
if (schema.getAdditionalProperties() != null) {
365+
// If non-object type is specified but also additionalProperties
366+
LOGGER.error("Illegal schema found with non-object type ({}) combined with additionalProperties. AdditionalProperties automatically removed.", schema.getType());
367+
schema.setAdditionalProperties(null);
368+
return;
369+
}
367370
}
371+
368372
// Check array items
369373
if (ModelUtils.isArraySchema(schema)) {
370374
Schema items = ModelUtils.getSchemaItems(schema);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1205,4 +1205,13 @@ public void doNotWrapSingleAllOfRefs() {
12051205
assertNotNull(allOfRefWithDescriptionAndReadonly.getAllOf());
12061206
assertEquals(numberRangeRef, ((Schema) allOfRefWithDescriptionAndReadonly.getAllOf().get(0)).get$ref());
12071207
}
1208+
1209+
@Test
1210+
public void testNonNullTypeWithProperties() {
1211+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/issue_21680_array_with_properties.yaml");
1212+
new InlineModelResolver().flatten(openAPI);
1213+
Schema<?> schema = (Schema<?>) openAPI.getComponents().getSchemas().get("errors");
1214+
assertNotNull(schema);
1215+
assertNull(schema.getProperties());
1216+
}
12081217
}

modules/openapi-generator/src/test/java/org/openapitools/codegen/java/spring/SpringCodegenTest.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2272,8 +2272,12 @@ public void testResponseWithArray_issue12524() throws Exception {
22722272
additionalProperties.put(RETURN_SUCCESS_CODE, "true");
22732273
Map<String, File> files = generateFromContract("src/test/resources/bugs/issue_12524.json", SPRING_BOOT, additionalProperties);
22742274

2275-
JavaFileAssert.assertThat(files.get("API01ListOfStuff.java"))
2276-
.hasImports("com.fasterxml.jackson.annotation.JsonTypeName");
2275+
// class extending array is no longer generated as it's automatically fixed by inline resolver
2276+
// by removing the properties for array type
2277+
//JavaFileAssert.assertThat(files.get("API01ListOfStuff.java"))
2278+
// .hasImports("com.fasterxml.jackson.annotation.JsonTypeName");
2279+
File notExisting = files.get("API01ListOfStuff.java");
2280+
assertThat(notExisting).isNull();
22772281
} finally {
22782282
GlobalSettings.reset();
22792283
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
---
2+
# Corresponds to bug report 21680: https://github.com/openapitools/openapi-generator/issues/21680
3+
openapi: 3.0.1
4+
info:
5+
title: API that has problem with OpenAPI Generator
6+
version: 1.0.0
7+
paths:
8+
"/forbiddenaccesscsrf":
9+
get:
10+
summary: Forbidden access CSRF
11+
operationId: forbiddenAccessCsrfGet
12+
responses:
13+
'403':
14+
description: Expected response
15+
content:
16+
application/json:
17+
schema:
18+
"$ref": "#/components/schemas/errors"
19+
components:
20+
schemas:
21+
error:
22+
required:
23+
- code
24+
- horodatage
25+
- message
26+
type: object
27+
properties:
28+
code:
29+
type: string
30+
description: Short error description
31+
message:
32+
type: string
33+
description: Complete human readable description
34+
error_uri:
35+
type: string
36+
description: Detailed error description URI
37+
format: uri
38+
horodatage:
39+
type: string
40+
description: Date time of occurence
41+
format: date-time
42+
errors:
43+
type: array
44+
properties:
45+
empty:
46+
type: boolean
47+
items:
48+
"$ref": "#/components/schemas/error"

0 commit comments

Comments
 (0)