Skip to content

Commit

Permalink
Merge pull request micronaut-projects#1854 from altro3/support-spring…
Browse files Browse the repository at this point in the history
…-server-context-path

Add support spring `server.servlet.context-path` and `spring.webflux.base-path` properties
  • Loading branch information
altro3 authored Nov 7, 2024
2 parents c949949 + 442dcea commit 02f4aed
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -138,10 +138,7 @@ protected String getFinalUrlPrefix(OpenApiViewConfig.RendererType rendererType,
}

// process micronaut.server.context-path
String contextPath = ConfigUtils.getConfigProperty(MICRONAUT_SERVER_CONTEXT_PATH, context);
if (contextPath == null) {
contextPath = StringUtils.EMPTY_STRING;
}
String contextPath = ConfigUtils.getServerContextPath(context);
finalUrl += contextPath.startsWith(SLASH) ? contextPath.substring(1) : contextPath;
if (!finalUrl.endsWith(SLASH)) {
finalUrl += SLASH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
import static io.micronaut.openapi.visitor.FileUtils.readFile;
import static io.micronaut.openapi.visitor.FileUtils.resolve;
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.StringUtil.COMMA;
import static io.micronaut.openapi.visitor.StringUtil.DOLLAR;
import static io.micronaut.openapi.visitor.StringUtil.SLASH;
Expand Down Expand Up @@ -436,10 +435,7 @@ public String getSpecURL(AbstractViewConfig cfg, @Nullable VisitorContext contex
}

// process micronaut.server.context-path
String contextPath = ConfigUtils.getConfigProperty(MICRONAUT_SERVER_CONTEXT_PATH, context);
if (contextPath == null) {
contextPath = StringUtils.EMPTY_STRING;
}
String contextPath = ConfigUtils.getServerContextPath(context);
finalUrl += contextPath.startsWith(SLASH) ? contextPath.substring(1) : contextPath;
if (!finalUrl.endsWith(SLASH)) {
finalUrl += SLASH;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_OPENAPI_SECURITY_ENABLED;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_OPENAPI_SWAGGER_FILE_GENERATION_ENABLED;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_OPENAPI_VERSIONING_ENABLED;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_SERVER_CONTEXT_PATH;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.OPENAPI_CONFIG_FILE;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.SPRING_SERVER_CONTEXT_PATH;
import static io.micronaut.openapi.visitor.OpenApiConfigProperty.SPRING_WEBFLUX_BASE_PATH;
import static io.micronaut.openapi.visitor.StringUtil.COMMA;
import static io.micronaut.openapi.visitor.StringUtil.DOT;
import static io.micronaut.openapi.visitor.StringUtil.UNDERSCORE;
Expand Down Expand Up @@ -252,6 +255,22 @@ private static String getClassNameWithGenerics(String className, Map<String, Cla
return key.toString();
}

@NonNull
public static String getServerContextPath(VisitorContext context) {
var contextPath = getConfigProperty(MICRONAUT_SERVER_CONTEXT_PATH, context);
if (contextPath == null) {
contextPath = getConfigProperty(SPRING_SERVER_CONTEXT_PATH, context);
}
if (contextPath == null) {
contextPath = getConfigProperty(SPRING_WEBFLUX_BASE_PATH, context);
}
if (contextPath == null) {
contextPath = StringUtils.EMPTY_STRING;
}

return contextPath;
}

public static DuplicateResolution getSchemaDuplicateResolution(VisitorContext context) {
var value = getConfigProperty(MICRONAUT_OPENAPI_SCHEMA_DUPLICATE_RESOLUTION, context);
if (StringUtils.isNotEmpty(value) && DuplicateResolution.ERROR.name().equalsIgnoreCase(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,14 @@ public interface OpenApiConfigProperty {
* Loaded micronaut-http server context path property.
*/
String MICRONAUT_SERVER_CONTEXT_PATH = "micronaut.server.context-path";
/**
* Context path property for spring boot applications.
*/
String SPRING_SERVER_CONTEXT_PATH = "server.servlet.context-path";
/**
* Context path property for reactive spring boot applications.
*/
String SPRING_WEBFLUX_BASE_PATH = "spring.webflux.base-path";
/**
* Loaded micronaut-http-server-netty property (json-view.enabled).
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.util.ArrayList;
import java.util.List;

import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_SERVER_CONTEXT_PATH;
import static io.micronaut.openapi.visitor.StringUtil.CLOSE_BRACE;
import static io.micronaut.openapi.visitor.StringUtil.DOLLAR;
import static io.micronaut.openapi.visitor.StringUtil.OPEN_BRACE;
Expand Down Expand Up @@ -61,7 +60,7 @@ public static List<String> buildUrls(List<Segment> segments, VisitorContext cont
prevSegment = segment;
}

String contextPath = ConfigUtils.getConfigProperty(MICRONAUT_SERVER_CONTEXT_PATH, context);
String contextPath = ConfigUtils.getServerContextPath(context);
if (StringUtils.isNotEmpty(contextPath)) {
if (!contextPath.startsWith(SLASH) && !contextPath.startsWith(DOLLAR)) {
contextPath = SLASH + contextPath;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ class OpenApiOperationViewRenderSpec extends Specification {
indexHtml.contains('clientSecret: "bar"')
}

@RestoreSystemProperties
void "test generates urlResourcesPrefix context-path and openapi.context.path"() {
given:
System.setProperty(OpenApiConfigProperty.MICRONAUT_SERVER_CONTEXT_PATH, "/local-path")
Expand All @@ -321,10 +322,54 @@ class OpenApiOperationViewRenderSpec extends Specification {

urlPrefix
urlPrefix == '/server-context-path/local-path/swagger-ui/res/'
}

cleanup:
System.clearProperty(OpenApiConfigProperty.MICRONAUT_SERVER_CONTEXT_PATH)
System.clearProperty(OpenApiConfigProperty.MICRONAUT_OPENAPI_CONTEXT_SERVER_PATH)
@RestoreSystemProperties
void "test generates urlResourcesPrefix spring context-path and openapi.context.path"() {
given:
System.setProperty(OpenApiConfigProperty.SPRING_SERVER_CONTEXT_PATH, "/local-path")
System.setProperty(OpenApiConfigProperty.MICRONAUT_OPENAPI_CONTEXT_SERVER_PATH, "/server-context-path")
String spec = "swagger-ui.enabled=true"
OpenApiViewConfig cfg = OpenApiViewConfig.fromSpecification(spec, null, new Properties(), null)
Path outputDir = Paths.get("output")
cfg.title = "OpenAPI documentation"
cfg.specFile = "swagger.yml"
cfg.render(outputDir, null)

expect:
cfg.enabled
cfg.swaggerUIConfig != null
cfg.title == "OpenAPI documentation"
cfg.specFile == "swagger.yml"

String urlPrefix = cfg.swaggerUIConfig.getFinalUrlPrefix(OpenApiViewConfig.RendererType.SWAGGER_UI, null)

urlPrefix
urlPrefix == '/server-context-path/local-path/swagger-ui/res/'
}

@RestoreSystemProperties
void "test generates urlResourcesPrefix spring webflux context-path and openapi.context.path"() {
given:
System.setProperty(OpenApiConfigProperty.SPRING_WEBFLUX_BASE_PATH, "/local-path")
System.setProperty(OpenApiConfigProperty.MICRONAUT_OPENAPI_CONTEXT_SERVER_PATH, "/server-context-path")
String spec = "swagger-ui.enabled=true"
OpenApiViewConfig cfg = OpenApiViewConfig.fromSpecification(spec, null, new Properties(), null)
Path outputDir = Paths.get("output")
cfg.title = "OpenAPI documentation"
cfg.specFile = "swagger.yml"
cfg.render(outputDir, null)

expect:
cfg.enabled
cfg.swaggerUIConfig != null
cfg.title == "OpenAPI documentation"
cfg.specFile == "swagger.yml"

String urlPrefix = cfg.swaggerUIConfig.getFinalUrlPrefix(OpenApiViewConfig.RendererType.SWAGGER_UI, null)

urlPrefix
urlPrefix == '/server-context-path/local-path/swagger-ui/res/'
}

void "test generates urlResourcesPrefix only context-path"() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2483,6 +2483,80 @@ class TestController {
}
}
@Singleton
class MyBean {}
''')
when:
OpenAPI openAPI = Utils.testReference
def paths = openAPI.paths

then:
paths
paths."/server-context-path/local-path/test/save"
paths."/server-context-path/local-path/test/save".get
paths."/server-context-path/local-path/test/save/{id}"
paths."/server-context-path/local-path/test/save/{id}".get
}

@RestoreSystemProperties
void "test append spring server.servlet.context-path to endpoints"() {
given:
System.setProperty(OpenApiConfigProperty.SPRING_SERVER_CONTEXT_PATH, "/local-path")
System.setProperty(OpenApiConfigProperty.MICRONAUT_OPENAPI_CONTEXT_SERVER_PATH, "/server-context-path")

buildBeanDefinition('test.MyBean', '''
package test;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import jakarta.inject.Singleton;
@Controller("/test")
class TestController {
@Get("/save{/id}")
String save() {
return null;
}
}
@Singleton
class MyBean {}
''')
when:
OpenAPI openAPI = Utils.testReference
def paths = openAPI.paths

then:
paths
paths."/server-context-path/local-path/test/save"
paths."/server-context-path/local-path/test/save".get
paths."/server-context-path/local-path/test/save/{id}"
paths."/server-context-path/local-path/test/save/{id}".get
}

@RestoreSystemProperties
void "test append spring spring.webflux.base-path to endpoints"() {
given:
System.setProperty(OpenApiConfigProperty.SPRING_WEBFLUX_BASE_PATH, "/local-path")
System.setProperty(OpenApiConfigProperty.MICRONAUT_OPENAPI_CONTEXT_SERVER_PATH, "/server-context-path")

buildBeanDefinition('test.MyBean', '''
package test;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import jakarta.inject.Singleton;
@Controller("/test")
class TestController {
@Get("/save{/id}")
String save() {
return null;
}
}
@Singleton
class MyBean {}
''')
Expand Down

0 comments on commit 02f4aed

Please sign in to comment.