Skip to content

Commit ff238c2

Browse files
committed
fix bug 21680: Introduce REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT normalize option
1 parent 814f406 commit ff238c2

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

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

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ public class OpenAPINormalizer {
119119
// the allOf contains a new schema containing the properties in the top level
120120
final String REFACTOR_ALLOF_WITH_PROPERTIES_ONLY = "REFACTOR_ALLOF_WITH_PROPERTIES_ONLY";
121121

122+
// when set to true, remove the "properties" of a schema with type other than "object"
123+
final String REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT = "REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT";
124+
122125
// when set to true, normalize OpenAPI 3.1 spec to make it work with the generator
123126
final String NORMALIZE_31SPEC = "NORMALIZE_31SPEC";
124127

@@ -205,6 +208,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
205208
ruleNames.add(FILTER);
206209
ruleNames.add(SET_CONTAINER_TO_NULLABLE);
207210
ruleNames.add(SET_PRIMITIVE_TYPES_TO_NULLABLE);
211+
ruleNames.add(REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT);
208212

209213

210214
// rules that are default to true
@@ -721,6 +725,8 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
721725
}
722726
markSchemaAsVisited(schema, visitedSchemas);
723727

728+
processNormalizeOtherThanObjectWithProperties(schema);
729+
724730
if (ModelUtils.isArraySchema(schema)) { // array
725731
Schema result = normalizeArraySchema(schema);
726732
normalizeSchema(result.getItems(), visitedSchemas);
@@ -1644,5 +1650,21 @@ protected Schema processNormalize31Spec(Schema schema, Set<Schema> visitedSchema
16441650
return schema;
16451651
}
16461652

1647-
// ===================== end of rules =====================
1653+
/**
1654+
* When set to true, remove "properties" attribute on schema other than "object"
1655+
* since it should be ignored and may result in odd generated code
1656+
*
1657+
* @param schema Schema
1658+
* @return Schema
1659+
*/
1660+
protected void processNormalizeOtherThanObjectWithProperties(Schema schema) {
1661+
if (getRule(REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT)) {
1662+
// Check object models / any type models / composed models for properties,
1663+
// if the schema has a type defined that is not "object" it should not define
1664+
// any properties
1665+
if (schema.getType() != null && !"object".equals(schema.getType())) {
1666+
schema.setProperties(null);
1667+
}
1668+
}
1669+
}
16481670
}

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,33 @@
2424
import org.openapitools.codegen.utils.ModelUtils;
2525
import org.testng.annotations.Test;
2626

27+
import java.lang.reflect.Array;
2728
import java.util.*;
2829

2930
import static org.testng.Assert.*;
3031

3132
public class OpenAPINormalizerTest {
33+
34+
@Test
35+
public void testOpenAPINormalizerOtherThanObjectWithProperties()
36+
{
37+
// to test the rule REF_AS_PARENT_IN_ALLOF
38+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/issue_21680_array_with_properties.yaml");
39+
40+
Schema schema = openAPI.getComponents().getSchemas().get("errors");
41+
assertNotNull(schema);
42+
assertNotNull(schema.getProperties());
43+
44+
Map<String, String> options = new HashMap<>();
45+
options.put("REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT", "true");
46+
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
47+
openAPINormalizer.normalize();
48+
49+
Schema schema2 = openAPI.getComponents().getSchemas().get("errors");
50+
assertNotNull(schema2);
51+
assertNull(schema2.getProperties());
52+
}
53+
3254
@Test
3355
public void testOpenAPINormalizerRefAsParentInAllOf() {
3456
// to test the rule REF_AS_PARENT_IN_ALLOF
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)