Skip to content

Commit 5078277

Browse files
committed
Additional unit test for invalid filter
1 parent 4835072 commit 5078277

2 files changed

Lines changed: 22 additions & 8 deletions

File tree

modules/openapi-generator/src/main/java/org/openapitools/codegen/OpenAPINormalizer.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -394,10 +394,11 @@ protected void normalizePaths() {
394394
if (Boolean.TRUE.equals(getRule(FILTER))) {
395395
String filters = inputRules.get(FILTER);
396396
Filter filter = createFilter(this.openAPI, filters);
397-
filter.parse();
398-
// Iterates over each HTTP method in methodMap, retrieves the corresponding Operations from the PathItem,
399-
// and marks it as internal (`x-internal=true`) if the method/operationId/tag/path is not in the filters.
400-
filter.apply(pathsEntry.getKey(), path, methodMap);
397+
if (filter.parse()) {
398+
// Iterates over each HTTP method in methodMap, retrieves the corresponding Operations from the PathItem,
399+
// and marks it as internal (`x-internal=true`) if the method/operationId/tag/path is not in the filters.
400+
filter.apply(pathsEntry.getKey(), path, methodMap);
401+
}
401402
}
402403

403404
// Include callback operation as well
@@ -1812,6 +1813,7 @@ protected static class Filter {
18121813
protected Set<String> methodFilters = Collections.emptySet();
18131814
protected Set<String> tagFilters = Collections.emptySet();
18141815
protected Set<String> pathStartingWithFilters = Collections.emptySet();
1816+
private boolean hasFilter;
18151817

18161818
protected Filter(String filters) {
18171819
this.filters = filters.trim();
@@ -1848,10 +1850,10 @@ private void doParse() {
18481850
String filterKey = filterStrs[0].trim();
18491851
String filterValue = filterStrs[1];
18501852
Set<String> parsedFilters = splitByPipe(filterValue);
1853+
hasFilter = true;
18511854
if (OPERATION_ID.equals(filterKey)) {
18521855
operationIdFilters = parsedFilters;
18531856
} else if (METHOD.equals(filterKey)) {
1854-
18551857
methodFilters = parsedFilters;
18561858
} else if (TAG.equals(filterKey)) {
18571859
tagFilters = parsedFilters;
@@ -1909,7 +1911,7 @@ protected boolean hasCustomFilterMatch(String path, Operation operation) {
19091911
}
19101912

19111913
public boolean hasFilter() {
1912-
return !operationIdFilters.isEmpty() || !methodFilters.isEmpty() || !tagFilters.isEmpty() || !pathStartingWithFilters.isEmpty ();
1914+
return hasFilter;
19131915
}
19141916

19151917
public void apply(String path, PathItem pathItem, Map<String, Function<PathItem, Operation>> methodMap) {

modules/openapi-generator/src/test/java/org/openapitools/codegen/OpenAPINormalizerTest.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,6 @@ public CustomRoleFilter(String filters) {
743743
super(filters);
744744
}
745745

746-
747746
@Override
748747
protected void parse(String filterName, String filterValue) {
749748
if ("role".equals(filterName)) {
@@ -760,7 +759,7 @@ protected boolean hasCustomFilterMatch(String path, Operation operation) {
760759
}
761760

762761
@Test
763-
public void testFilterInvalidDoesThrow() {
762+
public void testFilterInvalidSyntaxDoesThrow() {
764763
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml");
765764

766765
Map<String, String> options = Map.of("FILTER", "tag ; invalid");
@@ -772,6 +771,19 @@ public void testFilterInvalidDoesThrow() {
772771
}
773772
}
774773

774+
@Test
775+
public void testFilterInvalidFilterDoesThrow() {
776+
OpenAPI openAPI = TestUtils.parseSpec("src/test/resources/3_0/enableKeepOnlyFirstTagInOperation_test.yaml");
777+
778+
Map<String, String> options = Map.of("FILTER", "method:get ; unknown:test");
779+
try {
780+
new OpenAPINormalizer(openAPI, options).normalize();
781+
fail("Expected IllegalArgumentException");
782+
} catch (IllegalArgumentException e) {
783+
assertEquals(e.getMessage(), "FILTER rule [method:get ; unknown:test] must be in the form of `operationId:name1|name2|name3` or `method:get|post|put` or `tag:tag1|tag2|tag3` or `path:/v1|/v2`. Error: filter not supported :[unknown:test]");
784+
}
785+
}
786+
775787

776788
@Test
777789
public void testComposedSchemaDoesNotThrow() {

0 commit comments

Comments
 (0)