Skip to content

Commit 8040e9a

Browse files
authored
Add openapi-normalizer rule to set tags to vendor extension (#20713)
1 parent 5581a2d commit 8040e9a

File tree

4 files changed

+106
-2
lines changed

4 files changed

+106
-2
lines changed

docs/customization.md

+7
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,13 @@ Example:
573573
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/petstore.yaml -o /tmp/java-okhttp/ --openapi-normalizer SET_TAGS_TO_OPERATIONID=true
574574
```
575575
576+
- `SET_TAGS_TO_VENDOR_EXTENSION`: when set to a string value, tags will be set to the value of the provided vendor extension
577+
578+
Example:
579+
```
580+
java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/enableSetTagsToVendorExtension_test.yaml -o /tmp/java-okhttp/ --openapi-normalizer SET_TAGS_TO_VENDOR_EXTENSION=x-tags
581+
```
582+
576583
- `ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE`: when set to true, auto fix integer with maximum value 4294967295 (2^32-1) or long with 18446744073709551615 (2^64-1) by adding x-unsigned to the schema
577584
578585
Example:

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

+41-2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ public class OpenAPINormalizer {
9191
final String SET_TAGS_TO_OPERATIONID = "SET_TAGS_TO_OPERATIONID";
9292
String setTagsToOperationId;
9393

94+
// when set to a string value, tags will be set to the value of the provided vendor extension
95+
final String SET_TAGS_TO_VENDOR_EXTENSION = "SET_TAGS_TO_VENDOR_EXTENSION";
96+
String setTagsToVendorExtension;
97+
9498
// when set to true, tags in all operations will be set to operationId or "default" if operationId
9599
// is empty
96100
final String FIX_DUPLICATED_OPERATIONID = "FIX_DUPLICATED_OPERATIONID";
@@ -158,6 +162,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
158162
ruleNames.add(KEEP_ONLY_FIRST_TAG_IN_OPERATION);
159163
ruleNames.add(SET_TAGS_FOR_ALL_OPERATIONS);
160164
ruleNames.add(SET_TAGS_TO_OPERATIONID);
165+
ruleNames.add(SET_TAGS_TO_VENDOR_EXTENSION);
161166
ruleNames.add(FIX_DUPLICATED_OPERATIONID);
162167
ruleNames.add(ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE);
163168
ruleNames.add(REFACTOR_ALLOF_WITH_PROPERTIES_ONLY);
@@ -224,6 +229,11 @@ public void processRules(Map<String, String> inputRules) {
224229
rules.put(SET_TAGS_FOR_ALL_OPERATIONS, true);
225230
}
226231

232+
setTagsToVendorExtension = inputRules.get(SET_TAGS_TO_VENDOR_EXTENSION);
233+
if (setTagsToVendorExtension != null) {
234+
rules.put(SET_TAGS_TO_VENDOR_EXTENSION, true);
235+
}
236+
227237
if (inputRules.get(FILTER) != null) {
228238
rules.put(FILTER, true);
229239

@@ -375,6 +385,8 @@ private void normalizeOperation(Operation operation) {
375385

376386
processSetTagsToOperationId(operation);
377387

388+
processSetTagsToVendorExtension(operation);
389+
378390
processFixDuplicatedOperationId(operation);
379391
}
380392

@@ -885,8 +897,7 @@ private void processUseAllOfRefAsParent(Schema schema) {
885897
}
886898

887899
/**
888-
* Keep only first tag in the operation if the operation has more than
889-
* one tag.
900+
* Remove/hide the x-internal in operations and model.
890901
*
891902
* @param operation Operation
892903
*/
@@ -955,6 +966,34 @@ private void processSetTagsToOperationId(Operation operation) {
955966
}
956967
}
957968

969+
/**
970+
* Set the tag name to the value of the provided vendor extension
971+
*
972+
* @param operation Operation
973+
*/
974+
private void processSetTagsToVendorExtension(Operation operation) {
975+
if (StringUtils.isEmpty(setTagsToVendorExtension)) {
976+
return;
977+
}
978+
979+
if (operation.getExtensions() == null) {
980+
return;
981+
}
982+
983+
if (operation.getExtensions().containsKey(setTagsToVendorExtension)) {
984+
operation.setTags(null);
985+
Object argObj = operation.getExtensions().get(setTagsToVendorExtension);
986+
if (argObj instanceof List) {
987+
List<String> tags = (List<String>) argObj;
988+
for (String tag : tags) {
989+
operation.addTagsItem(tag);
990+
}
991+
} else {
992+
operation.addTagsItem(String.valueOf(argObj));
993+
}
994+
}
995+
}
996+
958997
private void processFixDuplicatedOperationId(Operation operation) {
959998
if (!getRule(FIX_DUPLICATED_OPERATIONID)) {
960999
return;

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

+15
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,21 @@ public void testOpenAPINormalizerSetTagsToOperationId() {
294294
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().get(0), "delete");
295295
}
296296

297+
@Test
298+
public void testOpenAPINormalizerSetTagsToVendorExtension() {
299+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableSetTagsToVendorExtension_test.yaml");
300+
301+
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().size(), 2);
302+
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getExtensions().size(), 1);
303+
304+
Map<String, String> options = new HashMap<>();
305+
options.put("SET_TAGS_TO_VENDOR_EXTENSION", "x-tags");
306+
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
307+
openAPINormalizer.normalize();
308+
309+
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().size(), 1);
310+
}
311+
297312
@Test
298313
public void testAddUnsignedToIntegerWithInvalidMaxValue() {
299314
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/addUnsignedToIntegerWithInvalidMaxValue_test.yaml");
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
openapi: 3.0.1
2+
info:
3+
version: 1.0.0
4+
title: Example
5+
license:
6+
name: MIT
7+
servers:
8+
- url: http://api.example.xyz/v1
9+
paths:
10+
/person/display/{personId}:
11+
get:
12+
x-tags:
13+
- display
14+
tags:
15+
- person
16+
- basic
17+
parameters:
18+
- name: personId
19+
in: path
20+
required: true
21+
description: The id of the person to retrieve
22+
schema:
23+
type: string
24+
operationId: list
25+
responses:
26+
'200':
27+
description: OK
28+
content:
29+
application/json:
30+
schema:
31+
$ref: "#/components/schemas/Person"
32+
components:
33+
schemas:
34+
Person:
35+
description: person
36+
type: object
37+
properties:
38+
$_type:
39+
type: string
40+
lastName:
41+
type: string
42+
firstName:
43+
type: string

0 commit comments

Comments
 (0)