From 53e14f0636a0585f2dad78e6749921f7b4a7109e Mon Sep 17 00:00:00 2001 From: abrevet-dev Date: Sun, 23 Feb 2025 12:18:20 +0100 Subject: [PATCH] Add openapi-normalizer rule to set tags to vendor extension --- docs/customization.md | 7 +++ .../codegen/OpenAPINormalizer.java | 43 ++++++++++++++++++- .../codegen/OpenAPINormalizerTest.java | 15 +++++++ .../enableSetTagsToVendorExtension_test.yaml | 43 +++++++++++++++++++ 4 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 modules/openapi-generator/src/test/resources/3_0/enableSetTagsToVendorExtension_test.yaml diff --git a/docs/customization.md b/docs/customization.md index 89b6a4e4bd51..d74e5087b108 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -573,6 +573,13 @@ Example: 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 ``` +- `SET_TAGS_TO_VENDOR_EXTENSION`: when set to a string value, tags will be set to the value of the provided vendor extension + +Example: +``` +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 +``` + - `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 Example: diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java index 25265ef0f548..263b81a6ede9 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java @@ -91,6 +91,10 @@ public class OpenAPINormalizer { final String SET_TAGS_TO_OPERATIONID = "SET_TAGS_TO_OPERATIONID"; String setTagsToOperationId; + // when set to a string value, tags will be set to the value of the provided vendor extension + final String SET_TAGS_TO_VENDOR_EXTENSION = "SET_TAGS_TO_VENDOR_EXTENSION"; + String setTagsToVendorExtension; + // when set to true, tags in all operations will be set to operationId or "default" if operationId // is empty final String FIX_DUPLICATED_OPERATIONID = "FIX_DUPLICATED_OPERATIONID"; @@ -158,6 +162,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map inputRules) { ruleNames.add(KEEP_ONLY_FIRST_TAG_IN_OPERATION); ruleNames.add(SET_TAGS_FOR_ALL_OPERATIONS); ruleNames.add(SET_TAGS_TO_OPERATIONID); + ruleNames.add(SET_TAGS_TO_VENDOR_EXTENSION); ruleNames.add(FIX_DUPLICATED_OPERATIONID); ruleNames.add(ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE); ruleNames.add(REFACTOR_ALLOF_WITH_PROPERTIES_ONLY); @@ -224,6 +229,11 @@ public void processRules(Map inputRules) { rules.put(SET_TAGS_FOR_ALL_OPERATIONS, true); } + setTagsToVendorExtension = inputRules.get(SET_TAGS_TO_VENDOR_EXTENSION); + if (setTagsToVendorExtension != null) { + rules.put(SET_TAGS_TO_VENDOR_EXTENSION, true); + } + if (inputRules.get(FILTER) != null) { rules.put(FILTER, true); @@ -375,6 +385,8 @@ private void normalizeOperation(Operation operation) { processSetTagsToOperationId(operation); + processSetTagsToVendorExtension(operation); + processFixDuplicatedOperationId(operation); } @@ -885,8 +897,7 @@ private void processUseAllOfRefAsParent(Schema schema) { } /** - * Keep only first tag in the operation if the operation has more than - * one tag. + * Remove/hide the x-internal in operations and model. * * @param operation Operation */ @@ -955,6 +966,34 @@ private void processSetTagsToOperationId(Operation operation) { } } + /** + * Set the tag name to the value of the provided vendor extension + * + * @param operation Operation + */ + private void processSetTagsToVendorExtension(Operation operation) { + if (StringUtils.isEmpty(setTagsToVendorExtension)) { + return; + } + + if (operation.getExtensions() == null) { + return; + } + + if (operation.getExtensions().containsKey(setTagsToVendorExtension)) { + operation.setTags(null); + Object argObj = operation.getExtensions().get(setTagsToVendorExtension); + if (argObj instanceof List) { + List tags = (List) argObj; + for (String tag : tags) { + operation.addTagsItem(tag); + } + } else { + operation.addTagsItem(String.valueOf(argObj)); + } + } + } + private void processFixDuplicatedOperationId(Operation operation) { if (!getRule(FIX_DUPLICATED_OPERATIONID)) { return; diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java index 8bed0f7382eb..8a5e80d40ebb 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java @@ -294,6 +294,21 @@ public void testOpenAPINormalizerSetTagsToOperationId() { assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().get(0), "delete"); } + @Test + public void testOpenAPINormalizerSetTagsToVendorExtension() { + OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableSetTagsToVendorExtension_test.yaml"); + + assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().size(), 2); + assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getExtensions().size(), 1); + + Map options = new HashMap<>(); + options.put("SET_TAGS_TO_VENDOR_EXTENSION", "x-tags"); + OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options); + openAPINormalizer.normalize(); + + assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().size(), 1); + } + @Test public void testAddUnsignedToIntegerWithInvalidMaxValue() { OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/addUnsignedToIntegerWithInvalidMaxValue_test.yaml"); diff --git a/modules/openapi-generator/src/test/resources/3_0/enableSetTagsToVendorExtension_test.yaml b/modules/openapi-generator/src/test/resources/3_0/enableSetTagsToVendorExtension_test.yaml new file mode 100644 index 000000000000..247222352045 --- /dev/null +++ b/modules/openapi-generator/src/test/resources/3_0/enableSetTagsToVendorExtension_test.yaml @@ -0,0 +1,43 @@ +openapi: 3.0.1 +info: + version: 1.0.0 + title: Example + license: + name: MIT +servers: + - url: http://api.example.xyz/v1 +paths: + /person/display/{personId}: + get: + x-tags: + - display + tags: + - person + - basic + parameters: + - name: personId + in: path + required: true + description: The id of the person to retrieve + schema: + type: string + operationId: list + responses: + '200': + description: OK + content: + application/json: + schema: + $ref: "#/components/schemas/Person" +components: + schemas: + Person: + description: person + type: object + properties: + $_type: + type: string + lastName: + type: string + firstName: + type: string