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

Fix logic for micronaut.environment.enabled #891

Merged
merged 1 commit into from
Jan 9, 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
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ projectVersion=4.8.2-SNAPSHOT
projectGroup=io.micronaut.openapi

micronautDocsVersion=2.0.0
micronautVersion=3.7.4
micronautVersion=3.8.0
micronautTestVersion=3.8.0
groovyVersion=3.0.13
groovyVersion=3.0.14
spockVersion=2.3-groovy-3.0

title=OpenAPI/Swagger Support
Expand Down
2 changes: 1 addition & 1 deletion gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ managed-slf4j = "1.7.36"
# Versions beyond 0.62.2 require Java 11
managed-html2md-converter = "0.62.2"

kotlin = "1.7.21"
kotlin = "1.7.22"

[libraries]
# Duplicated to keep catalog compatibility with 3.4.x. Can be removed for 4.0.0
Expand Down
2 changes: 1 addition & 1 deletion openapi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies {
api(libs.managed.swagger.models)
api(libs.managed.swagger.annotations)
api(libs.managed.javadoc.parser)
api(libs.managed.slf4j.nop)
api(mn.slf4j.nop)
api(libs.managed.html2md.converter) {
exclude group: "org.jetbrains", module: "annotations"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@

import javax.xml.datatype.XMLGregorianCalendar;

import io.micronaut.context.env.Environment;
import io.micronaut.core.annotation.AnnotationClassValue;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.annotation.NonNull;
Expand Down Expand Up @@ -301,11 +300,8 @@ List<PathItem> resolvePathItems(VisitorContext context, List<UriMatchTemplate> m
result.append(c);
}

String resultPath = result.toString();
Environment environment = OpenApiApplicationVisitor.getEnv(context);
if (environment != null) {
resultPath = environment.getPlaceholderResolver().resolvePlaceholders(resultPath).orElse(resultPath);
}
String resultPath = OpenApiApplicationVisitor.replacePlaceholders(result.toString(), context);

if (!resultPath.startsWith("/") && !resultPath.startsWith("$")) {
resultPath = "/" + resultPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,10 @@ public static String getConfigurationProperty(String key, VisitorContext context

@Nullable
public static Environment getEnv(VisitorContext context) {
if (!isEnvEnabled(context)) {
return null;
}

Environment existedEnvironment = context != null ? context.get(MICRONAUT_ENVIRONMENT, Environment.class).orElse(null) : null;
Boolean envCreated = context != null ? context.get(MICRONAUT_ENVIRONMENT_CREATED, Boolean.class).orElse(null) : null;
if (envCreated != null && envCreated) {
Expand All @@ -508,8 +512,7 @@ public static Environment getEnv(VisitorContext context) {
return environment;
}

public static List<String> getActiveEnvs(VisitorContext context) {

private static boolean isEnvEnabled(VisitorContext context) {
String isEnabledStr = System.getProperty(MICRONAUT_ENVIRONMENT_ENABLED, readOpenApiConfigFile(context).getProperty(MICRONAUT_ENVIRONMENT_ENABLED));
boolean isEnabled = true;
if (StringUtils.isNotEmpty(isEnabledStr)) {
Expand All @@ -518,7 +521,12 @@ public static List<String> getActiveEnvs(VisitorContext context) {
if (context != null) {
context.put(MICRONAUT_ENVIRONMENT_ENABLED, isEnabled);
}
if (!isEnabled) {
return isEnabled;
}

public static List<String> getActiveEnvs(VisitorContext context) {

if (!isEnvEnabled(context)) {
return Collections.emptyList();
}

Expand Down Expand Up @@ -871,16 +879,43 @@ public static JsonNode resolvePlaceholders(JsonNode node, UnaryOperator<String>
}

public static String expandProperties(String s, List<Map.Entry<String, String>> properties, VisitorContext context) {
if (StringUtils.isEmpty(s)) {
if (StringUtils.isEmpty(s) || !s.contains(Utils.PLACEHOLDER_PREFIX)) {
return s;
}

// form openapi file (expandable properties)
if (CollectionUtils.isNotEmpty(properties)) {
for (Map.Entry<String, String> entry : properties) {
s = s.replace(entry.getKey(), entry.getValue());
}
}
Environment environment = getEnv(context);
return environment != null ? environment.getPlaceholderResolver().resolvePlaceholders(s).orElse(s) : s;

return replacePlaceholders(s, context);
}

public static String replacePlaceholders(String value, VisitorContext context) {
if (StringUtils.isEmpty(value) || !value.contains(Utils.PLACEHOLDER_PREFIX)) {
return value;
}
// system properties
if (CollectionUtils.isNotEmpty(System.getProperties())) {
for (Map.Entry<Object, Object> sysProp : System.getProperties().entrySet()) {
value = value.replace(Utils.PLACEHOLDER_PREFIX + sysProp.getKey().toString() + Utils.PLACEHOLDER_POSTFIX, sysProp.getValue().toString());
}
}

// form openapi file
for (Map.Entry<Object, Object> fileProp : OpenApiApplicationVisitor.readOpenApiConfigFile(context).entrySet()) {
value = value.replace(Utils.PLACEHOLDER_PREFIX + fileProp.getKey().toString() + Utils.PLACEHOLDER_POSTFIX, fileProp.getValue().toString());
}

// from environments
Environment environment = OpenApiApplicationVisitor.getEnv(context);
if (environment != null) {
value = environment.getPlaceholderResolver().resolvePlaceholders(value).orElse(value);
}

return value;
}

public static List<Map.Entry<String, String>> getExpandableProperties(VisitorContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@
import io.micronaut.context.RequiresCondition;
import io.micronaut.context.annotation.Requires;
import io.micronaut.context.env.Environment;
import io.micronaut.context.env.PropertyPlaceholderResolver;
import io.micronaut.core.annotation.AnnotationValue;
import io.micronaut.core.util.CollectionUtils;
import io.micronaut.core.util.StringUtils;
Expand Down Expand Up @@ -187,21 +186,19 @@ protected List<UriMatchTemplate> uriMatchTemplates(MethodElement element, Visito
controllerValue = customUri;
}

Environment environment = OpenApiApplicationVisitor.getEnv(context);
PropertyPlaceholderResolver propertyPlaceholderResolver = environment != null ? environment.getPlaceholderResolver() : Utils.getPropertyPlaceholderResolver();
controllerValue = propertyPlaceholderResolver.resolvePlaceholders(controllerValue).orElse(controllerValue);
controllerValue = OpenApiApplicationVisitor.replacePlaceholders(controllerValue, context);

UriMatchTemplate matchTemplate = UriMatchTemplate.of(controllerValue);
// check if we have multiple uris
String[] uris = element.stringValues(HttpMethodMapping.class, "uris");
if (uris.length == 0) {
String methodValue = element.getValue(HttpMethodMapping.class, String.class).orElse("/");
methodValue = propertyPlaceholderResolver.resolvePlaceholders(methodValue).orElse(methodValue);
methodValue = OpenApiApplicationVisitor.replacePlaceholders(methodValue, context);
return Collections.singletonList(matchTemplate.nest(methodValue));
} else {
List<UriMatchTemplate> matchTemplates = new ArrayList<>(uris.length);
for (String methodValue : uris) {
methodValue = propertyPlaceholderResolver.resolvePlaceholders(methodValue).orElse(methodValue);
methodValue = OpenApiApplicationVisitor.replacePlaceholders(methodValue, context);
matchTemplates.add(matchTemplate.nest(methodValue));
}
return matchTemplates;
Expand Down
3 changes: 3 additions & 0 deletions openapi/src/main/java/io/micronaut/openapi/visitor/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
*/
public final class Utils {

public static final String PLACEHOLDER_PREFIX = "${";
public static final String PLACEHOLDER_POSTFIX = "}";

public static final String ATTR_OPENAPI = "io.micronaut.OPENAPI";
public static final String ATTR_TEST_MODE = "io.micronaut.OPENAPI_TEST";
public static final String ATTR_VISITED_ELEMENTS = "io.micronaut.OPENAPI.visited.elements";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ class MyBean {}
sliceMyDtoSchema.properties.empty
sliceMyDtoSchema.properties.numberOfElements

sortSchema.properties.sorted
sortSchema.properties.orderBy

sortOrderSchema.properties.ignoreCase
Expand Down