Skip to content

Commit 351b601

Browse files
wing328galamome
andauthored
openapi-normalizer: Add REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT normalize option (#22236)
* fix bug 21680: Introduce REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT normalize option * Advise to use REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT * update doc --------- Co-authored-by: Guillaume ALAMOME <[email protected]>
1 parent 2c248e6 commit 351b601

File tree

5 files changed

+102
-1
lines changed

5 files changed

+102
-1
lines changed

docs/customization.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,13 @@ Example:
644644
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/required-properties.yaml -o /tmp/java-okhttp/ --openapi-normalizer NORMALIZER_CLASS=org.openapitools.codegen.OpenAPINormalizerTest$RemoveRequiredNormalizer
645645
```
646646
647+
- `REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT`: When set to true, remove the "properties" of a schema with type other than "object".
648+
649+
Example:
650+
```
651+
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/required-properties.yaml -o /tmp/java-okhttp/ --openapi-normalizer REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT=true
652+
```
653+
647654
- `FILTER`
648655
649656
The `FILTER` parameter allows selective inclusion of API operations based on specific criteria. It applies the `x-internal: true` property to operations that do **not** match the specified values, preventing them from being generated. Multiple filters can be separated by a semicolon.

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,9 @@ private void gatherInlineModels(Schema schema, String modelPrefix) {
356356
} else if (schema.getProperties() != null) {
357357
// If non-object type is specified but also properties
358358
LOGGER.error("Illegal schema found with non-object type combined with properties," +
359-
" no properties should be defined:\n " + schema.toString());
359+
" no properties should be defined:" +
360+
" consider using --openapi-normalizer REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT=true\n " +
361+
schema.toString());
360362
return;
361363
} else if (schema.getAdditionalProperties() != null) {
362364
// If non-object type is specified but also additionalProperties

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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ public class OpenAPINormalizer {
124124
// the allOf contains a new schema containing the properties in the top level
125125
final String REFACTOR_ALLOF_WITH_PROPERTIES_ONLY = "REFACTOR_ALLOF_WITH_PROPERTIES_ONLY";
126126

127+
// when set to true, remove the "properties" of a schema with type other than "object"
128+
final String REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT = "REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT";
129+
127130
// when set to true, normalize OpenAPI 3.1 spec to make it work with the generator
128131
final String NORMALIZE_31SPEC = "NORMALIZE_31SPEC";
129132

@@ -206,6 +209,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
206209
ruleNames.add(SET_CONTAINER_TO_NULLABLE);
207210
ruleNames.add(SET_PRIMITIVE_TYPES_TO_NULLABLE);
208211
ruleNames.add(SIMPLIFY_ONEOF_ANYOF_ENUM);
212+
ruleNames.add(REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT);
209213

210214
// rules that are default to true
211215
rules.put(SIMPLIFY_ONEOF_ANYOF, true);
@@ -702,6 +706,8 @@ public Schema normalizeSchema(Schema schema, Set<Schema> visitedSchemas) {
702706

703707
markSchemaAsVisited(schema, visitedSchemas);
704708

709+
processNormalizeOtherThanObjectWithProperties(schema);
710+
705711
if (ModelUtils.isArraySchema(schema)) { // array
706712
Schema result = normalizeArraySchema(schema);
707713
normalizeSchema(result.getItems(), visitedSchemas);
@@ -1961,6 +1967,23 @@ private boolean hasOperationId(Operation operation) {
19611967
private boolean hasMethod(String method) {
19621968
return methodFilters.contains(method);
19631969
}
1970+
}
19641971

1972+
/**
1973+
* When set to true, remove "properties" attribute on schema other than "object"
1974+
* since it should be ignored and may result in odd generated code
1975+
*
1976+
* @param schema Schema
1977+
* @return Schema
1978+
*/
1979+
protected void processNormalizeOtherThanObjectWithProperties(Schema schema) {
1980+
if (getRule(REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT)) {
1981+
// Check object models / any type models / composed models for properties,
1982+
// if the schema has a type defined that is not "object" it should not define
1983+
// any properties
1984+
if (schema.getType() != null && !"object".equals(schema.getType())) {
1985+
schema.setProperties(null);
1986+
}
1987+
}
19651988
}
19661989
}

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

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.openapitools.codegen.utils.ModelUtils;
2727
import org.testng.annotations.Test;
2828

29+
import java.lang.reflect.Array;
2930
import java.util.*;
3031

3132
import static org.testng.Assert.*;
@@ -36,6 +37,26 @@ public class OpenAPINormalizerTest {
3637
private static final String X_PARENT = "x-parent";
3738
private static final String X_INTERNAL = "x-internal";
3839

40+
@Test
41+
public void testOpenAPINormalizerOtherThanObjectWithProperties()
42+
{
43+
// to test the rule REF_AS_PARENT_IN_ALLOF
44+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/issue_21680_array_with_properties.yaml");
45+
46+
Schema schema = openAPI.getComponents().getSchemas().get("errors");
47+
assertNotNull(schema);
48+
assertNotNull(schema.getProperties());
49+
50+
Map<String, String> options = new HashMap<>();
51+
options.put("REMOVE_PROPERTIES_FROM_TYPE_OTHER_THAN_OBJECT", "true");
52+
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
53+
openAPINormalizer.normalize();
54+
55+
Schema schema2 = openAPI.getComponents().getSchemas().get("errors");
56+
assertNotNull(schema2);
57+
assertNull(schema2.getProperties());
58+
}
59+
3960
@Test
4061
public void testOpenAPINormalizerRefAsParentInAllOf() {
4162
// 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)