Add openapi-normalizer rule to set tags to operationId (#17161)

* add normalizer rule to set tags to operationId

* update
This commit is contained in:
William Cheng 2023-11-23 11:09:09 +08:00 committed by GitHub
parent aaed846f5f
commit a93bab077f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 44 additions and 0 deletions

View File

@ -80,6 +80,11 @@ public class OpenAPINormalizer {
final String SET_TAGS_FOR_ALL_OPERATIONS = "SET_TAGS_FOR_ALL_OPERATIONS"; final String SET_TAGS_FOR_ALL_OPERATIONS = "SET_TAGS_FOR_ALL_OPERATIONS";
String setTagsForAllOperations; String setTagsForAllOperations;
// when set to true, tags in all operations will be set to operationId or "default" if operationId
// is empty
final String SET_TAGS_TO_OPERATIONID = "SET_TAGS_TO_OPERATIONID";
String setTagsToOperationId;
// when set to true, auto fix integer with maximum value 4294967295 (2^32-1) or long with 18446744073709551615 (2^64-1) // 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 // by adding x-unsigned to the schema
final String ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE = "ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE"; final String ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE = "ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE";
@ -117,6 +122,7 @@ public class OpenAPINormalizer {
ruleNames.add(SIMPLIFY_BOOLEAN_ENUM); ruleNames.add(SIMPLIFY_BOOLEAN_ENUM);
ruleNames.add(KEEP_ONLY_FIRST_TAG_IN_OPERATION); ruleNames.add(KEEP_ONLY_FIRST_TAG_IN_OPERATION);
ruleNames.add(SET_TAGS_FOR_ALL_OPERATIONS); ruleNames.add(SET_TAGS_FOR_ALL_OPERATIONS);
ruleNames.add(SET_TAGS_TO_OPERATIONID);
ruleNames.add(ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE); ruleNames.add(ADD_UNSIGNED_TO_INTEGER_WITH_INVALID_MAX_VALUE);
ruleNames.add(REFACTOR_ALLOF_WITH_PROPERTIES_ONLY); ruleNames.add(REFACTOR_ALLOF_WITH_PROPERTIES_ONLY);
ruleNames.add(NORMALIZE_31SPEC); ruleNames.add(NORMALIZE_31SPEC);
@ -233,6 +239,8 @@ public class OpenAPINormalizer {
processKeepOnlyFirstTagInOperation(operation); processKeepOnlyFirstTagInOperation(operation);
processSetTagsForAllOperations(operation); processSetTagsForAllOperations(operation);
processSetTagsToOperationId(operation);
} }
/** /**
@ -619,6 +627,24 @@ public class OpenAPINormalizer {
operation.addTagsItem(setTagsForAllOperations); operation.addTagsItem(setTagsForAllOperations);
} }
/**
* Set the tag name to operationId (or "default" if operationId is empty)
*
* @param operation Operation
*/
private void processSetTagsToOperationId(Operation operation) {
if (!getRule(SET_TAGS_TO_OPERATIONID)) {
return;
}
operation.setTags(null);
if (StringUtils.isNotEmpty(operation.getOperationId())) {
operation.addTagsItem(operation.getOperationId());
} else { // default to "default" if operationId is empty
operation.addTagsItem("default");
}
}
/** /**
* If the schema contains anyOf/oneOf and properties, remove oneOf/anyOf as these serve as rules to * 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. * ensure inter-dependency between properties. It's a workaround as such validation is not supported at the moment.

View File

@ -242,6 +242,24 @@ public class OpenAPINormalizerTest {
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().get(0), "core"); assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().get(0), "core");
} }
@Test
public void testOpenAPINormalizerSetTagsToOperationId() {
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_TO_OPERATIONID", "true");
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), "list");
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getTags().get(0), "delete");
}
@Test @Test
public void testAddUnsignedToIntegerWithInvalidMaxValue() { public void testAddUnsignedToIntegerWithInvalidMaxValue() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/addUnsignedToIntegerWithInvalidMaxValue_test.yaml"); OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/addUnsignedToIntegerWithInvalidMaxValue_test.yaml");