|
34 | 34 | import java.util.*; |
35 | 35 | import java.util.stream.Collectors; |
36 | 36 |
|
| 37 | +import static org.openapitools.codegen.utils.StringUtils.getUniqueString; |
| 38 | +import static org.openapitools.codegen.utils.StringUtils.underscore; |
| 39 | + |
37 | 40 | public class OpenAPINormalizer { |
38 | 41 | private OpenAPI openAPI; |
39 | 42 | private Map<String, String> inputRules = new HashMap<>(); |
@@ -88,6 +91,12 @@ public class OpenAPINormalizer { |
88 | 91 | final String SET_TAGS_TO_OPERATIONID = "SET_TAGS_TO_OPERATIONID"; |
89 | 92 | String setTagsToOperationId; |
90 | 93 |
|
| 94 | + // when set to true, tags in all operations will be set to operationId or "default" if operationId |
| 95 | + // is empty |
| 96 | + final String FIX_DUPLICATED_OPERATIONID = "FIX_DUPLICATED_OPERATIONID"; |
| 97 | + String fixDuplicatedOperationId; |
| 98 | + HashSet<String> operationIdSet = new HashSet<>(); |
| 99 | + |
91 | 100 | // when set to true, auto fix integer with maximum value 4294967295 (2^32-1) or long with 18446744073709551615 (2^64-1) |
92 | 101 | // by adding x-unsigned to the schema |
93 | 102 | final String ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE = "ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE"; |
@@ -149,6 +158,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) { |
149 | 158 | ruleNames.add(KEEP_ONLY_FIRST_TAG_IN_OPERATION); |
150 | 159 | ruleNames.add(SET_TAGS_FOR_ALL_OPERATIONS); |
151 | 160 | ruleNames.add(SET_TAGS_TO_OPERATIONID); |
| 161 | + ruleNames.add(FIX_DUPLICATED_OPERATIONID); |
152 | 162 | ruleNames.add(ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE); |
153 | 163 | ruleNames.add(REFACTOR_ALLOF_WITH_PROPERTIES_ONLY); |
154 | 164 | ruleNames.add(NORMALIZE_31SPEC); |
@@ -361,6 +371,8 @@ private void normalizeOperation(Operation operation) { |
361 | 371 | processSetTagsForAllOperations(operation); |
362 | 372 |
|
363 | 373 | processSetTagsToOperationId(operation); |
| 374 | + |
| 375 | + processFixDuplicatedOperationId(operation); |
364 | 376 | } |
365 | 377 |
|
366 | 378 | /** |
@@ -939,6 +951,26 @@ private void processSetTagsToOperationId(Operation operation) { |
939 | 951 | } |
940 | 952 | } |
941 | 953 |
|
| 954 | + private void processFixDuplicatedOperationId(Operation operation) { |
| 955 | + if (!getRule(FIX_DUPLICATED_OPERATIONID)) { |
| 956 | + return; |
| 957 | + } |
| 958 | + |
| 959 | + // skip null as default codegen will automatically generate one using path, http verb, etc |
| 960 | + if (operation.getOperationId() == null) { |
| 961 | + return; |
| 962 | + } |
| 963 | + |
| 964 | + String uniqueName = getUniqueString(operationIdSet, operation.getOperationId()); |
| 965 | + |
| 966 | + if (!uniqueName.equals(operation.getOperationId())) { |
| 967 | + LOGGER.info("operationId {} renamed to {} to ensure uniqueness (enabled by openapi normalizer rule `FIX_DUPLICATED_OPERATIONID`)", operation.getOperationId(), uniqueName); |
| 968 | + operation.setOperationId(uniqueName); |
| 969 | + } |
| 970 | + } |
| 971 | + |
| 972 | + |
| 973 | + |
942 | 974 | /** |
943 | 975 | * If the schema contains anyOf/oneOf and properties, remove oneOf/anyOf as these serve as rules to |
944 | 976 | * ensure inter-dependency between properties. It's a workaround as such validation is not supported at the moment. |
|
0 commit comments