Skip to content

Commit

Permalink
add FILTER to openapi normalizer (#17859)
Browse files Browse the repository at this point in the history
  • Loading branch information
wing328 committed Feb 15, 2024
1 parent 41bb5cd commit b0c9456
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 1 deletion.
7 changes: 7 additions & 0 deletions docs/customization.md
Original file line number Diff line number Diff line change
Expand Up @@ -584,3 +584,10 @@ 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 REMOVE_X_INTERNAL=true
```
- `FILTER`: When set to `operationId:addPet|getPetById` for example, it will add `x-internal:true` to operations with operationId not equal to addPet/getPetById (which will have x-internal set to false) so that these operations marked as internal won't be generated.
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 FILTER="operationId:addPet|getPetById"
```
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ public class OpenAPINormalizer {
final String X_INTERNAL = "x-internal";
boolean removeXInternal;

// when set (e.g. operationId:getPetById, addPet), filter out (or remove) everything else
final String FILTER = "FILTER";
HashSet<String> operationIdFilters = new HashSet<>();

// ============= end of rules =============

/**
Expand Down Expand Up @@ -131,6 +135,7 @@ public OpenAPINormalizer(OpenAPI openAPI, Map<String, String> inputRules) {
ruleNames.add(REFACTOR_ALLOF_WITH_PROPERTIES_ONLY);
ruleNames.add(NORMALIZE_31SPEC);
ruleNames.add(REMOVE_X_INTERNAL);
ruleNames.add(FILTER);

// rules that are default to true
rules.put(SIMPLIFY_ONEOF_ANYOF, true);
Expand Down Expand Up @@ -166,7 +171,7 @@ public void processRules(Map<String, String> inputRules) {
for (Map.Entry<String, String> rule : inputRules.entrySet()) {
LOGGER.debug("processing rule {} => {}", rule.getKey(), rule.getValue());
if (!ruleNames.contains(rule.getKey())) { // invalid rule name
LOGGER.warn("Invalid openapi-normalizer rule name: ", rule.getKey());
LOGGER.warn("Invalid openapi-normalizer rule name: {}", rule.getKey());
} else if (enableAll) {
rules.put(rule.getKey(), true); // set rule
} else {
Expand All @@ -179,6 +184,21 @@ public void processRules(Map<String, String> inputRules) {
if (setTagsForAllOperations != null) {
rules.put(SET_TAGS_FOR_ALL_OPERATIONS, true);
}

if (inputRules.get(FILTER) != null) {
rules.put(FILTER, true);

String[] filterStrs = inputRules.get(FILTER).split(":");
if (filterStrs.length != 2) { // only support operationId with : at the moment
LOGGER.error("FILTER rule must be in the form of `operationId:name1|name2|name3`: {}", inputRules.get(FILTER));
} else {
if ("operationId".equals(filterStrs[0])) {
operationIdFilters = new HashSet<>(Arrays.asList(filterStrs[1].split("[|]")));
} else {
LOGGER.error("FILTER rule must be in the form of `operationId:name1|name2|name3`: {}", inputRules.get(FILTER));
}
}
}
}

/**
Expand Down Expand Up @@ -230,6 +250,15 @@ private void normalizePaths() {
normalizeParameters(path.getParameters());

for (Operation operation : operations) {
if (operationIdFilters.size() > 0) {
if (operationIdFilters.contains(operation.getOperationId())) {
operation.addExtension("x-internal", false);
} else {
LOGGER.info("operation `{}` marked as internal only (x-internal: true) by the FILTER", operation.getOperationId());
operation.addExtension("x-internal", true);
}
}

normalizeOperation(operation);
normalizeRequestBody(operation);
normalizeParameters(operation.getParameters());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,4 +449,22 @@ public void testRemoveXInternal() {
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getExtensions().get("x-internal"), null);
assertEquals(s2.getExtensions().get("x-internal"), null);
}

@Test
public void testFilter() {
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml");

assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getExtensions(), null);
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getExtensions().get("x-internal"), true);
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getPut().getExtensions(), null);

Map<String, String> options = new HashMap<>();
options.put("FILTER", "operationId:delete|list");
OpenAPINormalizer openAPINormalizer = new OpenAPINormalizer(openAPI, options);
openAPINormalizer.normalize();

assertEquals(openAPI.getPaths().get("/person/display/{personId}").getGet().getExtensions().get("x-internal"), false);
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getDelete().getExtensions().get("x-internal"), false);
assertEquals(openAPI.getPaths().get("/person/display/{personId}").getPut().getExtensions().get("x-internal"), true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/Person"
put:
tags:
- person
parameters:
- name: personId
in: path
required: true
description: The id of the person to retrieve
schema:
type: string
operationId: put
responses:
'200':
description: OK
content:
application/json:
schema:
$ref: "#/components/schemas/Person"

components:
schemas:
Person:
Expand Down

0 comments on commit b0c9456

Please sign in to comment.