-
Notifications
You must be signed in to change notification settings - Fork 89
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
maven-plugin make it easier to scan dependend schemas
Added new options scan.profiles and scan.exclude.profiles to filter the operations that are kept in the schema. Only one of the configured profiles is needed to either include, or exclude, the operation. Exclusions are checked first, then inclusions. Extensions containing profiles have to start with "x-smallrye-profile", and will not be included in the openapi document. Naming of these new options are kept in line with the openapi scan.package and scan.external.package options.
- Loading branch information
Showing
13 changed files
with
277 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
package io.smallrye.openapi.runtime.scanner.spi; | ||
|
||
import io.smallrye.openapi.api.OpenApiConfig; | ||
import io.smallrye.openapi.api.constants.OpenApiConstants; | ||
import org.eclipse.microprofile.openapi.models.Extensible; | ||
|
||
import java.util.HashMap; | ||
import java.util.HashSet; | ||
import java.util.Map; | ||
import java.util.Set; | ||
|
||
/** | ||
* Abstract base class for annotation scanners | ||
* | ||
* @author Phillip Kruger ([email protected]) | ||
*/ | ||
public abstract class AbstractAnnotationScanner implements AnnotationScanner { | ||
private static final String EMPTY = ""; | ||
|
||
protected String currentAppPath = EMPTY; | ||
private String contextRoot = EMPTY; | ||
|
||
|
@@ -20,7 +31,7 @@ protected String makePath(String operationPath) { | |
|
||
/** | ||
* Make a path out of a number of path segments. | ||
* | ||
* | ||
* @param segments String paths | ||
* @return Path built from the segments | ||
*/ | ||
|
@@ -46,5 +57,45 @@ protected static String createPathFromSegments(String... segments) { | |
return rval; | ||
} | ||
|
||
private static final String EMPTY = ""; | ||
/** | ||
* Checks if the given extensible contains profiles, and if the extensible should be included in the final openapi document. | ||
* Any extension containing a profile is removed from the extensible. | ||
* inclusion is then calculated based on all collected profiles. | ||
* | ||
* @param config current config | ||
* @param extensible the extensible to check for profiles | ||
* @return true, if the given extensible should be included in the final openapi document, otherwise false | ||
*/ | ||
protected static boolean processProfiles(OpenApiConfig config, Extensible<?> extensible) { | ||
|
||
Set<String> profiles = new HashSet<>(); | ||
Map<String, Object> extensions = extensible.getExtensions(); | ||
if (extensions != null && !extensions.isEmpty()) { | ||
extensions = new HashMap<>(extensions); | ||
|
||
for (String name : extensions.keySet()) { | ||
if (!name.startsWith(OpenApiConstants.EXTENSION_PROFILE_PREFIX)) { | ||
continue; | ||
} | ||
|
||
String profile = name.substring(OpenApiConstants.EXTENSION_PROFILE_PREFIX.length()); | ||
profiles.add(profile); | ||
extensible.removeExtension(name); | ||
} | ||
} | ||
|
||
return profileIncluded(config, profiles); | ||
} | ||
|
||
private static boolean profileIncluded(OpenApiConfig config, Set<String> profiles) { | ||
if (!config.getScanExcludeProfiles().isEmpty()) { | ||
return config.getScanExcludeProfiles().stream().noneMatch(profiles::contains); | ||
} | ||
|
||
if (config.getScanProfiles().isEmpty()) { | ||
return true; | ||
} | ||
|
||
return config.getScanProfiles().stream().anyMatch(profiles::contains); | ||
} | ||
} |
96 changes: 96 additions & 0 deletions
96
.../src/test/java/io/smallrye/openapi/runtime/scanner/spi/AbstractAnnotationScannerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package io.smallrye.openapi.runtime.scanner.spi; | ||
|
||
import io.smallrye.openapi.api.OpenApiConfig; | ||
import io.smallrye.openapi.api.models.OperationImpl; | ||
import org.junit.jupiter.api.Assertions; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.util.Collections; | ||
import java.util.Set; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
|
||
class AbstractAnnotationScannerTest { | ||
/** | ||
* Test method for {@link AbstractAnnotationScanner#makePath(String)}. | ||
*/ | ||
@Test | ||
void testMakePath() { | ||
|
||
String path = AbstractAnnotationScanner.createPathFromSegments("", "", ""); | ||
Assertions.assertEquals("/", path); | ||
|
||
path = AbstractAnnotationScanner.createPathFromSegments("/", "/"); | ||
Assertions.assertEquals("/", path); | ||
|
||
path = AbstractAnnotationScanner.createPathFromSegments("", "/bookings"); | ||
Assertions.assertEquals("/bookings", path); | ||
|
||
path = AbstractAnnotationScanner.createPathFromSegments("/api", "/bookings"); | ||
Assertions.assertEquals("/api/bookings", path); | ||
|
||
path = AbstractAnnotationScanner.createPathFromSegments("api", "bookings"); | ||
Assertions.assertEquals("/api/bookings", path); | ||
|
||
path = AbstractAnnotationScanner.createPathFromSegments("/", "/bookings", "{id}"); | ||
Assertions.assertEquals("/bookings/{id}", path); | ||
} | ||
|
||
@Test | ||
void testNoConfiguredProfile() { | ||
OpenApiConfig config = new OpenApiConfig() { | ||
}; | ||
|
||
OperationImpl operation = new OperationImpl(); | ||
operation.setExtensions(Collections.singletonMap("x-smallrye-profile-external", "")); | ||
|
||
boolean result = AbstractAnnotationScanner.processProfiles(config, operation); | ||
|
||
assertTrue(result); | ||
assertEquals(0, operation.getExtensions().size()); | ||
} | ||
|
||
@Test | ||
void testConfiguredIncludeProfile() { | ||
OpenApiConfig config = new OpenApiConfig() { | ||
@Override | ||
public Set<String> getScanProfiles() { | ||
return Collections.singleton("external"); | ||
} | ||
}; | ||
|
||
OperationImpl operation = new OperationImpl(); | ||
|
||
boolean result = AbstractAnnotationScanner.processProfiles(config, operation); | ||
assertFalse(result); | ||
|
||
operation.setExtensions(Collections.singletonMap("x-smallrye-profile-external", "")); | ||
result = AbstractAnnotationScanner.processProfiles(config, operation); | ||
|
||
assertTrue(result); | ||
assertEquals(0, operation.getExtensions().size()); | ||
} | ||
|
||
@Test | ||
void testConfiguredExcludeProfile() { | ||
OpenApiConfig config = new OpenApiConfig() { | ||
@Override | ||
public Set<String> getScanExcludeProfiles() { | ||
return Collections.singleton("external"); | ||
} | ||
}; | ||
|
||
OperationImpl operation = new OperationImpl(); | ||
|
||
boolean result = AbstractAnnotationScanner.processProfiles(config, operation); | ||
assertTrue(result); | ||
|
||
operation.setExtensions(Collections.singletonMap("x-smallrye-profile-external", "")); | ||
result = AbstractAnnotationScanner.processProfiles(config, operation); | ||
|
||
assertFalse(result); | ||
assertEquals(0, operation.getExtensions().size()); | ||
} | ||
} |
36 changes: 0 additions & 36 deletions
36
core/src/test/java/io/smallrye/openapi/runtime/scanner/spi/PathMakerTest.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.