Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Configuration improvements #1149

Merged
merged 2 commits into from
Aug 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Class<OpenAPIManagement> annotationType() {
}

@Override
public List<AnnotationValue<?>> map(AnnotationValue<OpenAPIManagement> annotation, VisitorContext visitorContext) {
public List<AnnotationValue<?>> map(AnnotationValue<OpenAPIManagement> annotation, VisitorContext context) {
return Collections.singletonList(
AnnotationValue.builder(OpenAPIInclude.class)
.values(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public Class<OpenAPISecurity> annotationType() {
}

@Override
public List<AnnotationValue<?>> map(AnnotationValue<OpenAPISecurity> annotation, VisitorContext visitorContext) {
public List<AnnotationValue<?>> map(AnnotationValue<OpenAPISecurity> annotation, VisitorContext context) {
return Collections.singletonList(
AnnotationValue.builder(OpenAPIInclude.class)
.values(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ abstract class AbstractRetentionPolicyAnnotationTransformer<T extends Annotation
}

@Override
public List<AnnotationValue<?>> transform(AnnotationValue<T> annotation, VisitorContext visitorContext) {
public List<AnnotationValue<?>> transform(AnnotationValue<T> annotation, VisitorContext context) {
return Collections.singletonList(
AnnotationValue.builder(annotation, RetentionPolicy.SOURCE).build());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@

import io.micronaut.core.util.StringUtils;
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.openapi.visitor.OpenApiApplicationVisitor;
import io.micronaut.openapi.visitor.ConfigUtils;
import io.micronaut.openapi.visitor.Pair;
import io.micronaut.openapi.visitor.Utils;
import io.micronaut.openapi.visitor.group.GroupProperties;
import io.micronaut.openapi.visitor.group.OpenApiInfo;

import static io.micronaut.openapi.visitor.OpenApiApplicationVisitor.getGroupProperties;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_OPENAPI_CONTEXT_SERVER_PATH;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_SERVER_CONTEXT_PATH;
import static io.micronaut.openapi.visitor.ConfigUtils.getGroupProperties;

/**
* Abstract View Config.
Expand Down Expand Up @@ -109,7 +111,7 @@ protected void addAttribute(Map.Entry<String, String> entry) {
*/
protected String toHtmlAttributes() {
return options.entrySet().stream().map(e -> e.getKey() + "=\"" + e.getValue() + '"')
.collect(Collectors.joining(" "));
.collect(Collectors.joining(" "));
}

protected String getFinalUrlPrefix(OpenApiViewConfig.RendererType rendererType, VisitorContext context) {
Expand All @@ -118,7 +120,7 @@ protected String getFinalUrlPrefix(OpenApiViewConfig.RendererType rendererType,
}

// process micronaut.openapi.server.context.path
String serverContextPath = OpenApiApplicationVisitor.getConfigurationProperty(OpenApiApplicationVisitor.MICRONAUT_OPENAPI_CONTEXT_SERVER_PATH, context);
String serverContextPath = ConfigUtils.getConfigProperty(MICRONAUT_OPENAPI_CONTEXT_SERVER_PATH, context);
if (serverContextPath == null) {
serverContextPath = StringUtils.EMPTY_STRING;
}
Expand All @@ -128,7 +130,7 @@ protected String getFinalUrlPrefix(OpenApiViewConfig.RendererType rendererType,
}

// process micronaut.server.context-path
String contextPath = OpenApiApplicationVisitor.getConfigurationProperty(OpenApiApplicationVisitor.MICRONAUT_SERVER_CONTEXT_PATH, context);
String contextPath = ConfigUtils.getConfigProperty(MICRONAUT_SERVER_CONTEXT_PATH, context);
if (contextPath == null) {
contextPath = StringUtils.EMPTY_STRING;
}
Expand Down Expand Up @@ -215,7 +217,7 @@ static <T extends AbstractViewConfig> T fromProperties(T cfg, Map<String, Object
}

cfg.specUrl = specUrl.replace(Utils.PLACEHOLDER_PREFIX + "filename" + Utils.PLACEHOLDER_POSTFIX,
filenameFromContext != null ? filenameFromContext : "");
filenameFromContext != null ? filenameFromContext : "");
}
}

Expand All @@ -237,7 +239,7 @@ static <T extends AbstractViewConfig> T fromProperties(T cfg, Map<String, Object

cfg.options.putAll(defaultOptions);
properties.entrySet().stream().filter(entry -> entry.getKey().startsWith(cfg.prefix))
.forEach(cfg::addAttribute);
.forEach(cfg::addAttribute);
return cfg;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,13 @@
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
import io.micronaut.inject.visitor.VisitorContext;
import io.micronaut.openapi.visitor.OpenApiApplicationVisitor;
import io.micronaut.openapi.visitor.Pair;
import io.micronaut.openapi.visitor.Utils;
import io.micronaut.openapi.visitor.group.OpenApiInfo;

import static io.micronaut.openapi.visitor.OpenApiApplicationVisitor.MICRONAUT_SERVER_CONTEXT_PATH;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_SERVER_CONTEXT_PATH;
import static io.micronaut.openapi.visitor.ConfigUtils.getConfigProperty;
import static io.micronaut.openapi.visitor.ConfigUtils.getProjectPath;
import static io.micronaut.openapi.visitor.FileUtils.resolve;

/**
* OpenApi view configuration for Swagger-ui, ReDoc and RapiDoc.
Expand Down Expand Up @@ -166,38 +167,38 @@ public boolean isEnabled() {
* Generates the views given this configuration.
*
* @param outputDir The destination directory of the generated views.
* @param visitorContext The visitor context
* @param context The visitor context
*
* @throws IOException When the generation fails.
*/
public void render(Path outputDir, VisitorContext visitorContext) throws IOException {
public void render(Path outputDir, VisitorContext context) throws IOException {
if (redocConfig != null) {
Path redocDir = outputDir.resolve(REDOC);
render(redocConfig, redocDir, TEMPLATES + SLASH + TEMPLATES_REDOC + SLASH + TEMPLATE_INDEX_HTML, visitorContext);
copyResources(redocConfig, redocDir, TEMPLATES_REDOC, redocConfig.getResources(), visitorContext);
render(redocConfig, redocDir, TEMPLATES + SLASH + TEMPLATES_REDOC + SLASH + TEMPLATE_INDEX_HTML, context);
copyResources(redocConfig, redocDir, TEMPLATES_REDOC, redocConfig.getResources(), context);
if (redocConfig.rapiPDFConfig.enabled) {
copyResources(redocConfig.rapiPDFConfig, redocDir, TEMPLATES_RAPIPDF, redocConfig.rapiPDFConfig.getResources(), visitorContext);
copyResources(redocConfig.rapiPDFConfig, redocDir, TEMPLATES_RAPIPDF, redocConfig.rapiPDFConfig.getResources(), context);
}
}
if (rapidocConfig != null) {
Path rapidocDir = outputDir.resolve(RAPIDOC);
render(rapidocConfig, rapidocDir, TEMPLATES + SLASH + TEMPLATES_RAPIDOC + SLASH + TEMPLATE_INDEX_HTML, visitorContext);
copyResources(rapidocConfig, rapidocDir, TEMPLATES_RAPIDOC, rapidocConfig.getResources(), visitorContext);
render(rapidocConfig, rapidocDir, TEMPLATES + SLASH + TEMPLATES_RAPIDOC + SLASH + TEMPLATE_INDEX_HTML, context);
copyResources(rapidocConfig, rapidocDir, TEMPLATES_RAPIDOC, rapidocConfig.getResources(), context);
if (rapidocConfig.rapiPDFConfig.enabled) {
copyResources(rapidocConfig.rapiPDFConfig, rapidocDir, TEMPLATES_RAPIPDF, rapidocConfig.rapiPDFConfig.getResources(), visitorContext);
copyResources(rapidocConfig.rapiPDFConfig, rapidocDir, TEMPLATES_RAPIPDF, rapidocConfig.rapiPDFConfig.getResources(), context);
}
}
if (swaggerUIConfig != null) {
Path swaggerUiDir = outputDir.resolve(SWAGGER_UI);
render(swaggerUIConfig, swaggerUiDir, TEMPLATES + SLASH + TEMPLATES_SWAGGER_UI + SLASH + TEMPLATE_INDEX_HTML, visitorContext);
render(swaggerUIConfig, swaggerUiDir, TEMPLATES + SLASH + TEMPLATES_SWAGGER_UI + SLASH + TEMPLATE_INDEX_HTML, context);
if (SwaggerUIConfig.hasOauth2Option(swaggerUIConfig.options)) {
render(swaggerUIConfig, swaggerUiDir, TEMPLATES + SLASH + TEMPLATES_SWAGGER_UI + SLASH + TEMPLATE_OAUTH_2_REDIRECT_HTML, visitorContext);
render(swaggerUIConfig, swaggerUiDir, TEMPLATES + SLASH + TEMPLATES_SWAGGER_UI + SLASH + TEMPLATE_OAUTH_2_REDIRECT_HTML, context);
}
copyResources(swaggerUIConfig, swaggerUiDir, TEMPLATES_SWAGGER_UI, swaggerUIConfig.getResources(), visitorContext);
copyResources(swaggerUIConfig, swaggerUiDir, TEMPLATES_SWAGGER_UI, swaggerUIConfig.getResources(), context);
if (swaggerUIConfig.rapiPDFConfig.enabled) {
copyResources(swaggerUIConfig.rapiPDFConfig, swaggerUiDir, TEMPLATES_RAPIPDF, swaggerUIConfig.rapiPDFConfig.getResources(), visitorContext);
copyResources(swaggerUIConfig.rapiPDFConfig, swaggerUiDir, TEMPLATES_RAPIPDF, swaggerUIConfig.rapiPDFConfig.getResources(), context);
}
copySwaggerUiTheme(swaggerUIConfig, swaggerUiDir, TEMPLATES_SWAGGER_UI, visitorContext);
copySwaggerUiTheme(swaggerUIConfig, swaggerUiDir, TEMPLATES_SWAGGER_UI, context);
}
}

Expand Down Expand Up @@ -285,7 +286,7 @@ private String readTemplateFromClasspath(String templateName) throws IOException

private String readTemplateFromCustomPath(String customPathStr, VisitorContext context) throws IOException {
String projectDir = StringUtils.EMPTY_STRING;
Path projectPath = Utils.getProjectPath(context);
Path projectPath = getProjectPath(context);
if (projectPath != null) {
projectDir = projectPath.toString().replaceAll("\\\\", "/");
}
Expand Down Expand Up @@ -316,7 +317,7 @@ private String readTemplateFromCustomPath(String customPathStr, VisitorContext c
}
}

Path templatePath = OpenApiApplicationVisitor.resolve(context, Paths.get(customPathStr));
Path templatePath = resolve(context, Paths.get(customPathStr));
if (!Files.isReadable(templatePath)) {
throw new IOException("Can't read file " + customPathStr);
}
Expand Down Expand Up @@ -410,7 +411,7 @@ public String getSpecURL(AbstractViewConfig cfg, VisitorContext context) {

String specUrl = StringUtils.prependUri(serverContextPath, StringUtils.prependUri(mappingPath, specFile));
if (StringUtils.isEmpty(serverContextPath)) {
String contextPath = OpenApiApplicationVisitor.getConfigurationProperty(MICRONAUT_SERVER_CONTEXT_PATH, context);
String contextPath = getConfigProperty(MICRONAUT_SERVER_CONTEXT_PATH, context);
if (contextPath == null) {
contextPath = StringUtils.EMPTY_STRING;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -118,12 +118,13 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;

import static io.micronaut.openapi.visitor.ConfigUtils.getGroupsPropertiesMap;
import static io.micronaut.openapi.visitor.ConfigUtils.getRouterVersioningProperties;
import static io.micronaut.openapi.visitor.ConfigUtils.getSecurityProperties;
import static io.micronaut.openapi.visitor.ConfigUtils.isJsonViewEnabled;
import static io.micronaut.openapi.visitor.ConfigUtils.isOpenApiEnabled;
import static io.micronaut.openapi.visitor.ElementUtils.isFileUpload;
import static io.micronaut.openapi.visitor.ElementUtils.isNullable;
import static io.micronaut.openapi.visitor.OpenApiApplicationVisitor.getGroupsPropertiesMap;
import static io.micronaut.openapi.visitor.OpenApiApplicationVisitor.getSecurityProperties;
import static io.micronaut.openapi.visitor.OpenApiApplicationVisitor.isJsonViewEnabled;
import static io.micronaut.openapi.visitor.OpenApiApplicationVisitor.isOpenApiEnabled;
import static io.micronaut.openapi.visitor.SchemaUtils.COMPONENTS_CALLBACKS_PREFIX;
import static io.micronaut.openapi.visitor.SchemaUtils.COMPONENTS_SCHEMAS_PREFIX;
import static io.micronaut.openapi.visitor.SchemaUtils.TYPE_OBJECT;
Expand Down Expand Up @@ -1854,7 +1855,7 @@ private void processMicronautVersionAndGroup(io.swagger.v3.oas.models.Operation
}
}

RouterVersioningProperties versioningProperties = OpenApiApplicationVisitor.getRouterVersioningProperties(context);
RouterVersioningProperties versioningProperties = getRouterVersioningProperties(context);
boolean isVersioningEnabled = versioningProperties.isEnabled() && versioningProperties.isRouterVersiningEnabled()
&& (versioningProperties.isHeaderEnabled() || versioningProperties.isParameterEnabled());

Expand Down
Loading