Skip to content

Commit

Permalink
Merge pull request eclipse#445 from phillip-kruger/master
Browse files Browse the repository at this point in the history
Maven plugin for SmallRye Open API
  • Loading branch information
phillip-kruger authored Aug 26, 2020
2 parents ccaf94c + 93b17cc commit ca9bde9
Show file tree
Hide file tree
Showing 18 changed files with 910 additions and 197 deletions.
149 changes: 122 additions & 27 deletions core/src/main/java/io/smallrye/openapi/api/OpenApiConfig.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package io.smallrye.openapi.api;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import io.smallrye.openapi.api.constants.OpenApiConstants;

/**
* Accessor to OpenAPI configuration options.
Expand All @@ -14,65 +19,155 @@
*/
public interface OpenApiConfig {

public String modelReader();
default String modelReader() {
return null;
}

public String filter();
default String filter() {
return null;
}

public boolean scanDisable();
default boolean scanDisable() {
return false;
}

public Pattern scanPackages();
default Pattern scanPackages() {
return null;
}

public Pattern scanClasses();
default Pattern scanClasses() {
return null;
}

public Pattern scanExcludePackages();
default Pattern scanExcludePackages() {
return Pattern.compile(
"(" + OpenApiConstants.NEVER_SCAN_PACKAGES.stream().map(Pattern::quote).collect(Collectors.joining("|")) + ")");
}

public Pattern scanExcludeClasses();
default Pattern scanExcludeClasses() {
return Pattern.compile(
"(" + OpenApiConstants.NEVER_SCAN_CLASSES.stream().map(Pattern::quote).collect(Collectors.joining("|")) + ")");
}

public Set<String> servers();
default Set<String> servers() {
return new HashSet<>();
}

public Set<String> pathServers(String path);
default Set<String> pathServers(String path) {
return new HashSet<>();
}

public Set<String> operationServers(String operationId);
default Set<String> operationServers(String operationId) {
return new HashSet<>();
}

public boolean scanDependenciesDisable();
default boolean scanDependenciesDisable() {
return false;
}

public Set<String> scanDependenciesJars();
default Set<String> scanDependenciesJars() {
return new HashSet<>();
}

public boolean schemaReferencesEnable();
default boolean schemaReferencesEnable() {
return true;
}

public String customSchemaRegistryClass();
default String customSchemaRegistryClass() {
return null;
}

public boolean applicationPathDisable();
default boolean applicationPathDisable() {
return false;
}

public Map<String, String> getSchemas();
default Map<String, String> getSchemas() {
return new HashMap<>();
}

// Here we extend this in SmallRye with some more configure options (mp.openapi.extensions)
public String getOpenApiVersion();
default String getOpenApiVersion() {
return null;
}

public String getInfoTitle();
default String getInfoTitle() {
return null;
}

public String getInfoVersion();
default String getInfoVersion() {
return null;
}

public String getInfoDescription();
default String getInfoDescription() {
return null;
}

public String getInfoTermsOfService();
default String getInfoTermsOfService() {
return null;
}

public String getInfoContactEmail();
default String getInfoContactEmail() {
return null;
}

public String getInfoContactName();
default String getInfoContactName() {
return null;
}

public String getInfoContactUrl();
default String getInfoContactUrl() {
return null;
}

public String getInfoLicenseName();
default String getInfoLicenseName() {
return null;
}

public String getInfoLicenseUrl();
default String getInfoLicenseUrl() {
return null;
}

public OperationIdStrategy getOperationIdStrategy();
default OperationIdStrategy getOperationIdStrategy() {
return null;
}

enum OperationIdStrategy {
METHOD,
CLASS_METHOD,
PACKAGE_CLASS_METHOD
}

default Pattern patternOf(String configValue) {
return patternOf(configValue, null);
}

default Pattern patternOf(String configValue, Set<String> buildIn) {
Pattern pattern = null;

if (configValue != null && (configValue.startsWith("^") || configValue.endsWith("$"))) {
pattern = Pattern.compile(configValue);
} else {
Set<String> literals = asCsvSet(configValue);
if (buildIn != null && !buildIn.isEmpty()) {
literals.addAll(buildIn);
}
if (literals.isEmpty()) {
return Pattern.compile("", Pattern.LITERAL);
} else {
pattern = Pattern.compile("(" + literals.stream().map(Pattern::quote).collect(Collectors.joining("|")) + ")");
}
}

return pattern;
}

default Set<String> asCsvSet(String items) {
Set<String> rval = new HashSet<>();
if (items != null) {
String[] split = items.split(",");
for (String item : split) {
rval.add(item.trim());
}
}
return rval;
}
}
48 changes: 6 additions & 42 deletions core/src/main/java/io/smallrye/openapi/api/OpenApiConfigImpl.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package io.smallrye.openapi.api;

import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
Expand Down Expand Up @@ -108,7 +107,7 @@ public boolean scanDisable() {
@Override
public Pattern scanPackages() {
if (scanPackages == null) {
scanPackages = patternOf(OASConfig.SCAN_PACKAGES);
scanPackages = patternOf(getStringConfigValue(OASConfig.SCAN_PACKAGES));
}
return scanPackages;
}
Expand All @@ -119,7 +118,7 @@ public Pattern scanPackages() {
@Override
public Pattern scanClasses() {
if (scanClasses == null) {
scanClasses = patternOf(OASConfig.SCAN_CLASSES);
scanClasses = patternOf(getStringConfigValue(OASConfig.SCAN_CLASSES));
}
return scanClasses;
}
Expand All @@ -130,7 +129,8 @@ public Pattern scanClasses() {
@Override
public Pattern scanExcludePackages() {
if (scanExcludePackages == null) {
scanExcludePackages = patternOf(OASConfig.SCAN_EXCLUDE_PACKAGES, OpenApiConstants.NEVER_SCAN_PACKAGES);
scanExcludePackages = patternOf(getStringConfigValue(OASConfig.SCAN_EXCLUDE_PACKAGES),
OpenApiConstants.NEVER_SCAN_PACKAGES);
}
return scanExcludePackages;
}
Expand All @@ -141,7 +141,8 @@ public Pattern scanExcludePackages() {
@Override
public Pattern scanExcludeClasses() {
if (scanExcludeClasses == null) {
scanExcludeClasses = patternOf(OASConfig.SCAN_EXCLUDE_CLASSES, OpenApiConstants.NEVER_SCAN_CLASSES);
scanExcludeClasses = patternOf(getStringConfigValue(OASConfig.SCAN_EXCLUDE_CLASSES),
OpenApiConstants.NEVER_SCAN_CLASSES);
}
return scanExcludeClasses;
}
Expand Down Expand Up @@ -348,41 +349,4 @@ public OperationIdStrategy getOperationIdStrategy() {
String getStringConfigValue(String key) {
return getConfig().getOptionalValue(key, String.class).map(v -> "".equals(v.trim()) ? null : v).orElse(null);
}

Pattern patternOf(String key) {
return patternOf(key, null);
}

Pattern patternOf(String key, Set<String> buildIn) {
String configValue = getStringConfigValue(key);
Pattern pattern;

if (configValue != null && (configValue.startsWith("^") || configValue.endsWith("$"))) {
pattern = Pattern.compile(configValue);
} else {
Set<String> literals = asCsvSet(configValue);
if (buildIn != null && !buildIn.isEmpty()) {
literals.addAll(buildIn);
}
if (literals.isEmpty()) {
return Pattern.compile("", Pattern.LITERAL);
} else {
pattern = Pattern.compile("(" + literals.stream().map(Pattern::quote).collect(Collectors.joining("|")) + ")");
}
}

return pattern;
}

private static Set<String> asCsvSet(String items) {
Set<String> rval = new HashSet<>();
if (items != null) {
String[] split = items.split(",");
for (String item : split) {
rval.add(item.trim());
}
}
return rval;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,13 @@ public boolean isPackageIncluded() {
}

public boolean isImpliedInclusion() {
return scanClasses.pattern().isEmpty() && scanPackages.pattern().isEmpty();
return (scanClasses == null || scanClasses.pattern().isEmpty())
&& (scanPackages == null || scanPackages.pattern().isEmpty());
}
}

String matchingGroup(String value, Pattern pattern) {
if (pattern.pattern().isEmpty() || value.isEmpty()) {
if (pattern == null || pattern.pattern().isEmpty() || value.isEmpty()) {
return "";
}
Matcher m = pattern.matcher(value);
Expand Down Expand Up @@ -257,24 +258,29 @@ public Collection<ClassInfo> getAllKnownImplementors(DotName interfaceName) {
*/
@Override
public Collection<AnnotationInstance> getAnnotations(DotName annotationName) {
return this.delegate.getAnnotations(annotationName).stream().filter(ai -> {
AnnotationTarget target = ai.target();
switch (target.kind()) {
case CLASS:
return accepts(target.asClass().name());
case FIELD:
return accepts(target.asField().declaringClass().name());
case METHOD:
return accepts(target.asMethod().declaringClass().name());
case METHOD_PARAMETER:
return accepts(target.asMethodParameter().method().declaringClass().name());
case TYPE:
// TODO properly handle filtering of "type" annotation targets
return true;
default:
return false;
}
}).collect(Collectors.toList());
Collection<AnnotationInstance> annotations = this.delegate.getAnnotations(annotationName);
if (annotations != null && !annotations.isEmpty()) {
return annotations.stream().filter(ai -> {
AnnotationTarget target = ai.target();
switch (target.kind()) {
case CLASS:
return accepts(target.asClass().name());
case FIELD:
return accepts(target.asField().declaringClass().name());
case METHOD:
return accepts(target.asMethod().declaringClass().name());
case METHOD_PARAMETER:
return accepts(target.asMethodParameter().method().declaringClass().name());
case TYPE:
// TODO properly handle filtering of "type" annotation targets
return true;
default:
return false;
}
}).collect(Collectors.toList());
} else {
return annotations;
}
}

}
7 changes: 0 additions & 7 deletions extension-jaxrs/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@
<artifactId>smallrye-open-api-core</artifactId>
</dependency>

<!-- Provided Dependencies -->
<dependency>
<groupId>jakarta.ws.rs</groupId>
<artifactId>jakarta.ws.rs-api</artifactId>
<scope>provided</scope>
</dependency>

<!-- Third Party Libraries -->
<dependency>
<groupId>org.jboss.logging</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
import java.util.function.Function;
import java.util.stream.Collectors;

import javax.ws.rs.core.Application;
import javax.ws.rs.core.Response.Status;

import org.eclipse.microprofile.openapi.models.OpenAPI;
import org.eclipse.microprofile.openapi.models.Operation;
import org.eclipse.microprofile.openapi.models.PathItem;
Expand Down Expand Up @@ -96,12 +93,6 @@ public boolean isMultipartInput(Type inputType) {
return RestEasyConstants.MULTIPART_INPUTS.contains(inputType.name());
}

@Override
public String getReasonPhrase(int statusCode) {
Status status = Status.fromStatusCode(statusCode);
return status != null ? status.getReasonPhrase() : null;
}

@Override
public boolean containsScannerAnnotations(List<AnnotationInstance> instances,
List<AnnotationScannerExtension> extensions) {
Expand Down
Loading

0 comments on commit ca9bde9

Please sign in to comment.