Skip to content

Commit a93bab0

Browse files
authored
Add openapi-normalizer rule to set tags to operationId (#17161)
* add normalizer rule to set tags to operationId * update
1 parent aaed846 commit a93bab0

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ public class OpenAPINormalizer {
8080
final String SET_TAGS_FOR_ALL_OPERATIONS = "SET_TAGS_FOR_ALL_OPERATIONS";
8181
String setTagsForAllOperations;
8282

83+
// when set to true, tags in all operations will be set to operationId or "default" if operationId
84+
// is empty
85+
final String SET_TAGS_TO_OPERATIONID = "SET_TAGS_TO_OPERATIONID";
86+
String setTagsToOperationId;
87+
8388
// when set to true, auto fix integer with maximum value 4294967295 (2^32-1) or long with 18446744073709551615 (2^64-1)
8489
// by adding x-unsigned to the schema
8590
final String ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE = "ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE";
@@ -117,6 +122,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
117122
ruleNames.add(SIMPLIFY_BOOLEAN_ENUM);
118123
ruleNames.add(KEEP_ONLY_FIRST_TAG_IN_OPERATION);
119124
ruleNames.add(SET_TAGS_FOR_ALL_OPERATIONS);
125+
ruleNames.add(SET_TAGS_TO_OPERATIONID);
120126
ruleNames.add(ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE);
121127
ruleNames.add(REFACTOR_ALLOF_WITH_PROPERTIES_ONLY);
122128
ruleNames.add(NORMALIZE_31SPEC);
@@ -233,6 +239,8 @@ private void normalizeOperation(Operation operation) {
233239
processKeepOnlyFirstTagInOperation(operation);
234240

235241
processSetTagsForAllOperations(operation);
242+
243+
processSetTagsToOperationId(operation);
236244
}
237245

238246
/**
@@ -619,6 +627,24 @@ private void processSetTagsForAllOperations(Operation operation) {
619627
operation.addTagsItem(setTagsForAllOperations);
620628
}
621629

630+
/**
631+
* Set the tag name to operationId (or "default" if operationId is empty)
632+
*
633+
* @param operation Operation
634+
*/
635+
private void processSetTagsToOperationId(Operation operation) {
636+
if (!getRule(SET_TAGS_TO_OPERATIONID)) {
637+
return;
638+
}
639+
640+
operation.setTags(null);
641+
if (StringUtils.isNotEmpty(operation.getOperationId())) {
642+
operation.addTagsItem(operation.getOperationId());
643+
} else { // default to "default" if operationId is empty
644+
operation.addTagsItem("default");
645+
}
646+
}
647+
622648
/**
623649
* If the schema contains anyOf/oneOf and properties, remove oneOf/anyOf as these serve as rules to
624650
* ensure inter-dependency between properties. It's a workaround as such validation is not supported at the moment.

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,24 @@ public void testOpenAPINormalizerSetTagsInAllOperations() {
242242
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().get(0), "core");
243243
}
244244

245+
@Test
246+
public void testOpenAPINormalizerSetTagsToOperationId() {
247+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml");
248+
249+
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().size(), 2);
250+
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().size(), 1);
251+
252+
Map<String, String> options = new HashMap<>();
253+
options.put("SET_TAGS_TO_OPERATIONID", "true");
254+
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
255+
openAPINormalizer.normalize();
256+
257+
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().size(), 1);
258+
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().size(), 1);
259+
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().get(0), "list");
260+
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().get(0), "delete");
261+
}
262+
245263
@Test
246264
public void testAddUnsignedToIntegerWithInvalidMaxValue() {
247265
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/addUnsignedToIntegerWithInvalidMaxValue_test.yaml");

0 commit comments

Comments
 (0)