From 8b2de3741490340976b981095c3ce4ae0cc7eb6e Mon Sep 17 00:00:00 2001 From: William Cheng Date: Fri, 24 Feb 2023 15:26:34 +0800 Subject: [PATCH] [openapi-normalizer] add a new rule to set tags for all operations (#14794) * add doc for KEEP_ONLY_FIRST_TAG_IN_OPERATION * add new rule SET_TAGS_FOR_ALL_OPERATIONS * better null check * improve doc --- docs/customization.md | 14 ++++++++ .../codegen/OpenAPINormalizer.java | 32 ++++++++++++++++++- .../codegen/DefaultCodegenTest.java | 18 +++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/docs/customization.md b/docs/customization.md index 1e21e9494f..f9be18ff07 100644 --- a/docs/customization.md +++ b/docs/customization.md @@ -491,3 +491,17 @@ Example: ``` java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/simplifyOneOfAnyOf_test.yaml -o /tmp/java-okhttp/ --openapi-normalizer SIMPLIFY_ONEOF_ANYOF=true ``` + +- `KEEP_ONLY_FIRST_TAG_IN_OPERATION`: when set to `true`, only keep the first tag in operation if there are more than one tag defined. + +Example: +``` +java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml -o /tmp/java-okhttp/ --openapi-normalizer KEEP_ONLY_FIRST_TAG_IN_OPERATION=true +``` + +- `SET_TAGS_FOR_ALL_OPERATIONS`: when set to a string value, tags in all operatinos will reset to the string value provided. + +Example: +``` +java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate -g java -i modules/openapi-generator/src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml -o /tmp/java-okhttp/ --openapi-normalizer SET_TAGS_FOR_ALL_OPERATIONS=true +``` 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 9a2dbd17bb..3e883e47b1 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 @@ -39,7 +39,7 @@ public class OpenAPINormalizer { final Logger LOGGER = LoggerFactory.getLogger(OpenAPINormalizer.class); // ============= a list of rules ============= - // when set to true, all rules are enabled + // when set to true, all rules (true or false) are enabled final String ALL = "ALL"; boolean enableAll; @@ -72,6 +72,10 @@ public class OpenAPINormalizer { final String SIMPLIFY_BOOLEAN_ENUM = "SIMPLIFY_BOOLEAN_ENUM"; boolean simplifyBooleanEnum; + // when set to a string value, tags in all operations will be reset to the string value provided + final String SET_TAGS_FOR_ALL_OPERATIONS = "SET_TAGS_FOR_ALL_OPERATIONS"; + String setTagsForAllOperations; + // ============= end of rules ============= /** @@ -123,6 +127,11 @@ public class OpenAPINormalizer { if (enableAll || "true".equalsIgnoreCase(rules.get(SIMPLIFY_BOOLEAN_ENUM))) { simplifyBooleanEnum = true; } + + if (StringUtils.isNotEmpty(rules.get(SET_TAGS_FOR_ALL_OPERATIONS))) { + setTagsForAllOperations = rules.get(SET_TAGS_FOR_ALL_OPERATIONS); + } + } /** @@ -186,6 +195,8 @@ public class OpenAPINormalizer { */ private void normalizeOperation(Operation operation) { processKeepOnlyFirstTagInOperation(operation); + + processSetTagsForAllOperations(operation); } /** @@ -501,6 +512,20 @@ public class OpenAPINormalizer { } } + /** + * Set the tag name for all operations + * + * @param operation Operation + */ + private void processSetTagsForAllOperations(Operation operation) { + if (StringUtils.isEmpty(setTagsForAllOperations)) { + return; + } + + operation.setTags(null); + operation.addTagsItem(setTagsForAllOperations); + } + /** * If the schema contains anyOf/oneOf and properties, remove oneOf/anyOf as these serve as rules to * ensure inter-dependency between properties. It's a workaround as such validation is not supported at the moment. @@ -534,6 +559,11 @@ public class OpenAPINormalizer { return schema; } + if (schema.getAnyOf() == null) { + // ComposedSchema, Schema with `type: null` + return schema; + } + Schema s0 = null, s1 = null; if (schema.getAnyOf().size() == 2) { s0 = ModelUtils.unaliasSchema(openAPI, (Schema) schema.getAnyOf().get(0)); diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java index b656e8c1c9..9308bb14df 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/DefaultCodegenTest.java @@ -4453,4 +4453,22 @@ public class DefaultCodegenTest { BooleanSchema bs2 = (BooleanSchema) schema.getProperties().get("boolean_enum"); assertNull(bs2.getEnum()); //ensure the enum has been erased } + + @Test + public void testOpenAPINormalizerSetTagsInAllOperations() { + OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml"); + + assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().size(), 2); + assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().size(), 1); + + Map options = new HashMap<>(); + options.put("SET_TAGS_FOR_ALL_OPERATIONS", "core"); + OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options); + openAPINormalizer.normalize(); + + assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().size(), 1); + assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().size(), 1); + assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getTags().get(0), "core"); + assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().get(0), "core"); + } }