mirror of
https://github.com/jlengrand/openapi-generator.git
synced 2026-03-10 08:31:23 +00:00
[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
This commit is contained in:
@@ -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
|
||||
```
|
||||
|
||||
@@ -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));
|
||||
|
||||
@@ -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<String, String> 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");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user