Skip to content

Commit

Permalink
Merge pull request #24170 from phillip-kruger/apicurio-hide-openapi
Browse files Browse the repository at this point in the history
Apicurio hide static openapi document
  • Loading branch information
gsmet committed Mar 8, 2022
2 parents 45b8274 + 409f61b commit e8b0c1e
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 21 deletions.
2 changes: 1 addition & 1 deletion bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<smallrye-config.version>2.9.1</smallrye-config.version>
<smallrye-health.version>3.2.0</smallrye-health.version>
<smallrye-metrics.version>3.0.4</smallrye-metrics.version>
<smallrye-open-api.version>2.1.20</smallrye-open-api.version>
<smallrye-open-api.version>2.1.21</smallrye-open-api.version>
<smallrye-graphql.version>1.4.3</smallrye-graphql.version>
<smallrye-opentracing.version>2.1.0</smallrye-opentracing.version>
<smallrye-fault-tolerance.version>5.3.1</smallrye-fault-tolerance.version>
Expand Down
4 changes: 4 additions & 0 deletions extensions/apicurio-registry-avro/deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<groupId>io.quarkus</groupId>
<artifactId>quarkus-vertx-deployment</artifactId>
</dependency>
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-openapi-spi</artifactId>
</dependency>

<dependency>
<groupId>org.testcontainers</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import io.quarkus.deployment.builditem.FeatureBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ReflectiveClassBuildItem;
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.smallrye.openapi.deployment.spi.IgnoreStaticDocumentBuildItem;
import io.quarkus.vertx.deployment.VertxBuildItem;

public class ApicurioRegistryAvroProcessor {
Expand Down Expand Up @@ -64,6 +65,13 @@ void registerSPIClient(BuildProducer<ServiceProviderBuildItem> services) throws
"io.apicurio.rest.client.VertxHttpClientProvider"));
}

@BuildStep
void ignoreIncludedOpenAPIDocument(BuildProducer<IgnoreStaticDocumentBuildItem> ignoreStaticDocumentProducer) {
// This will ignore the OpenAPI Document in META-INF/openapi.yaml in the apicurio-registry-common dependency
ignoreStaticDocumentProducer.produce(new IgnoreStaticDocumentBuildItem(
".*/io/apicurio/apicurio-registry-common/.*/apicurio-registry-common-.*.jar.*"));
}

@BuildStep
@Record(ExecutionTime.RUNTIME_INIT)
public void apicurioRegistryClient(VertxBuildItem vertx, ApicurioRegistryClient client) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package io.quarkus.smallrye.openapi.deployment;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
Expand All @@ -13,12 +16,15 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

Expand Down Expand Up @@ -66,6 +72,7 @@
import io.quarkus.deployment.builditem.nativeimage.ServiceProviderBuildItem;
import io.quarkus.deployment.logging.LogCleanupFilterBuildItem;
import io.quarkus.deployment.pkg.builditem.OutputTargetBuildItem;
import io.quarkus.deployment.util.IoUtil;
import io.quarkus.resteasy.common.spi.ResteasyDotNames;
import io.quarkus.resteasy.server.common.spi.AllowedJaxRsAnnotationPrefixBuildItem;
import io.quarkus.resteasy.server.common.spi.ResteasyJaxrsConfigBuildItem;
Expand All @@ -76,6 +83,7 @@
import io.quarkus.smallrye.openapi.deployment.filter.AutoTagFilter;
import io.quarkus.smallrye.openapi.deployment.filter.SecurityConfigFilter;
import io.quarkus.smallrye.openapi.deployment.spi.AddToOpenAPIDefinitionBuildItem;
import io.quarkus.smallrye.openapi.deployment.spi.IgnoreStaticDocumentBuildItem;
import io.quarkus.smallrye.openapi.runtime.OpenApiConstants;
import io.quarkus.smallrye.openapi.runtime.OpenApiDocumentService;
import io.quarkus.smallrye.openapi.runtime.OpenApiRecorder;
Expand Down Expand Up @@ -610,11 +618,18 @@ public void build(BuildProducer<FeatureBuildItem> feature,
OutputTargetBuildItem out,
SmallRyeOpenApiConfig openApiConfig,
Optional<ResteasyJaxrsConfigBuildItem> resteasyJaxrsConfig,
OutputTargetBuildItem outputTargetBuildItem) throws Exception {
OutputTargetBuildItem outputTargetBuildItem,
List<IgnoreStaticDocumentBuildItem> ignoreStaticDocumentBuildItems) throws Exception {
FilteredIndexView index = openApiFilteredIndexViewBuildItem.getIndex();

feature.produce(new FeatureBuildItem(Feature.SMALLRYE_OPENAPI));
OpenAPI staticModel = generateStaticModel(openApiConfig, outputTargetBuildItem.getOutputDirectory());

List<Pattern> urlIgnorePatterns = new ArrayList<>();
for (IgnoreStaticDocumentBuildItem isdbi : ignoreStaticDocumentBuildItems) {
urlIgnorePatterns.add(isdbi.getUrlIgnorePattern());
}

OpenAPI staticModel = generateStaticModel(openApiConfig, urlIgnorePatterns, outputTargetBuildItem.getOutputDirectory());

OpenAPI annotationModel;

Expand Down Expand Up @@ -712,11 +727,12 @@ private boolean isUsingVertxRoute(IndexView index) {
return false;
}

private OpenAPI generateStaticModel(SmallRyeOpenApiConfig openApiConfig, Path target) throws IOException {
private OpenAPI generateStaticModel(SmallRyeOpenApiConfig openApiConfig, List<Pattern> ignorePatterns, Path target)
throws IOException {
if (openApiConfig.ignoreStaticDocument) {
return null;
} else {
List<Result> results = findStaticModels(openApiConfig, target);
List<Result> results = findStaticModels(openApiConfig, ignorePatterns, target);
if (!results.isEmpty()) {
OpenAPI mergedStaticModel = new OpenAPIImpl();
for (Result result : results) {
Expand Down Expand Up @@ -778,16 +794,16 @@ private String[] getScanners(Capabilities capabilities, IndexView index) {
return scanners.toArray(new String[] {});
}

private List<Result> findStaticModels(SmallRyeOpenApiConfig openApiConfig, Path target) {
private List<Result> findStaticModels(SmallRyeOpenApiConfig openApiConfig, List<Pattern> ignorePatterns, Path target) {
List<Result> results = new ArrayList<>();

// First check for the file in both META-INF and WEB-INF/classes/META-INF
addStaticModelIfExist(results, Format.YAML, META_INF_OPENAPI_YAML);
addStaticModelIfExist(results, Format.YAML, WEB_INF_CLASSES_META_INF_OPENAPI_YAML);
addStaticModelIfExist(results, Format.YAML, META_INF_OPENAPI_YML);
addStaticModelIfExist(results, Format.YAML, WEB_INF_CLASSES_META_INF_OPENAPI_YML);
addStaticModelIfExist(results, Format.JSON, META_INF_OPENAPI_JSON);
addStaticModelIfExist(results, Format.JSON, WEB_INF_CLASSES_META_INF_OPENAPI_JSON);
addStaticModelIfExist(results, ignorePatterns, Format.YAML, META_INF_OPENAPI_YAML);
addStaticModelIfExist(results, ignorePatterns, Format.YAML, WEB_INF_CLASSES_META_INF_OPENAPI_YAML);
addStaticModelIfExist(results, ignorePatterns, Format.YAML, META_INF_OPENAPI_YML);
addStaticModelIfExist(results, ignorePatterns, Format.YAML, WEB_INF_CLASSES_META_INF_OPENAPI_YML);
addStaticModelIfExist(results, ignorePatterns, Format.JSON, META_INF_OPENAPI_JSON);
addStaticModelIfExist(results, ignorePatterns, Format.JSON, WEB_INF_CLASSES_META_INF_OPENAPI_JSON);

// Add any aditional directories if configured
if (openApiConfig.additionalDocsDirectory.isPresent()) {
Expand All @@ -797,7 +813,7 @@ private List<Result> findStaticModels(SmallRyeOpenApiConfig openApiConfig, Path
try {
List<String> filesInDir = getResourceFiles(path, target);
for (String possibleModelFile : filesInDir) {
addStaticModelIfExist(results, possibleModelFile);
addStaticModelIfExist(results, ignorePatterns, possibleModelFile);
}
} catch (IOException ioe) {
ioe.printStackTrace();
Expand All @@ -808,27 +824,53 @@ private List<Result> findStaticModels(SmallRyeOpenApiConfig openApiConfig, Path
return results;
}

private void addStaticModelIfExist(List<Result> results, String path) {
private void addStaticModelIfExist(List<Result> results, List<Pattern> ignorePatterns, String path) {
if (path.endsWith(".json")) {
// Scan a specific json file
addStaticModelIfExist(results, Format.JSON, path);
addStaticModelIfExist(results, ignorePatterns, Format.JSON, path);
} else if (path.endsWith(".yaml") || path.endsWith(".yml")) {
// Scan a specific yaml file
addStaticModelIfExist(results, Format.YAML, path);
addStaticModelIfExist(results, ignorePatterns, Format.YAML, path);
}
}

private void addStaticModelIfExist(List<Result> results, Format format, String path) {
private void addStaticModelIfExist(List<Result> results, List<Pattern> ignorePatterns, Format format, String path) {
ClassLoader cl = Thread.currentThread().getContextClassLoader();
try (InputStream inputStream = cl.getResourceAsStream(path)) {
if (inputStream != null) {
results.add(new Result(format, inputStream));

try {
Enumeration<URL> urls = cl.getResources(path);
while (urls.hasMoreElements()) {
URL url = urls.nextElement();
// Check if we should ignore
if (!shouldIgnore(ignorePatterns, url.toString())) {
// Add as static model
try (InputStream inputStream = url.openStream()) {
if (inputStream != null) {
byte[] contents = IoUtil.readBytes(inputStream);

results.add(new Result(format, new ByteArrayInputStream(contents)));
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
}

} catch (IOException ex) {
ex.printStackTrace();
throw new UncheckedIOException(ex);
}
}

private boolean shouldIgnore(List<Pattern> ignorePatterns, String url) {
for (Pattern ignorePattern : ignorePatterns) {
Matcher matcher = ignorePattern.matcher(url);
if (matcher.matches()) {
return true;
}
}
return false;
}

private List<String> getResourceFiles(Path resourcePath, Path target) throws IOException {
final String resourceName = ClassPathUtils.toResourceName(resourcePath);
List<String> filenames = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.quarkus.smallrye.openapi.deployment.spi;

import java.util.regex.Pattern;

import io.quarkus.builder.item.MultiBuildItem;

/**
* Ignore a static OpenAPI document included in extension and/or dependencies. Supports regular expressions.
*/
public final class IgnoreStaticDocumentBuildItem extends MultiBuildItem {

private Pattern urlIgnorePattern = null;

/**
* @param urlIgnorePattern pattern to ignore when scanning static documents
*/
public IgnoreStaticDocumentBuildItem(String urlIgnorePattern) {
this.urlIgnorePattern = Pattern.compile(urlIgnorePattern);
}

public Pattern getUrlIgnorePattern() {
return this.urlIgnorePattern;
}
}

0 comments on commit e8b0c1e

Please sign in to comment.