Skip to content

Commit

Permalink
Append micronaut.server.context-path to endpoints
Browse files Browse the repository at this point in the history
Fixed #1774
  • Loading branch information
altro3 committed Sep 18, 2024
1 parent 067fd50 commit de22caa
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ Map<String, List<PathItem>> resolvePathItems(VisitorContext context, List<UriMat
var resultPathItemsMap = new HashMap<String, List<PathItem>>();

for (UriMatchTemplate matchTemplate : matchTemplates) {
var segms = parsePathSegments(matchTemplate.toPathString());
var finalPaths = buildUrls(segms);
var segments = parsePathSegments(matchTemplate.toPathString());
var finalPaths = buildUrls(segments, context);

for (String finalPath : finalPaths) {
List<PathItem> resultPathItems = resultPathItemsMap.computeIfAbsent(finalPath, k -> new ArrayList<>());
Expand Down
20 changes: 19 additions & 1 deletion openapi/src/main/java/io/micronaut/openapi/visitor/UrlUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
package io.micronaut.openapi.visitor;

import io.micronaut.core.annotation.Internal;
import io.micronaut.core.util.StringUtils;
import io.micronaut.inject.visitor.VisitorContext;

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 @@ -47,7 +50,7 @@ private UrlUtils() {
* @param segments url template segments
* @return all possible URL variants by parsed segments.
*/
public static List<String> buildUrls(List<Segment> segments) {
public static List<String> buildUrls(List<Segment> segments, VisitorContext context) {

var results = new ArrayList<StringBuilder>();

Expand All @@ -57,6 +60,16 @@ public static List<String> buildUrls(List<Segment> segments) {
prevSegment = segment;
}

String contextPath = ConfigUtils.getConfigProperty(MICRONAUT_SERVER_CONTEXT_PATH, context);
if (StringUtils.isNotEmpty(contextPath)) {
if (!contextPath.startsWith(SLASH) && !contextPath.startsWith(DOLLAR)) {
contextPath = SLASH + contextPath;
}
if (contextPath.endsWith(SLASH)) {
contextPath = contextPath.substring(0, contextPath.length() - 1);
}
}

var resultStrings = new ArrayList<String>();
for (var res : results) {
var url = res.toString();
Expand All @@ -67,6 +80,11 @@ public static List<String> buildUrls(List<Segment> segments) {
} else if (url.startsWith(SLASH + DOLLAR)) {
url = url.substring(1);
}

if (StringUtils.isNotEmpty(contextPath)) {
url = contextPath + url;
}

if (!resultStrings.contains(url)) {
resultStrings.add(url);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ import io.swagger.v3.oas.models.Operation
import io.swagger.v3.oas.models.PathItem
import io.swagger.v3.oas.models.Paths
import io.swagger.v3.oas.models.media.Schema
import java.nio.file.Path
import spock.lang.Issue
import spock.util.environment.RestoreSystemProperties

import static io.micronaut.openapi.visitor.OpenApiConfigProperty.MICRONAUT_OPENAPI_SWAGGER_FILE_GENERATION_ENABLED

class OpenApiControllerVisitorSpec extends AbstractOpenApiTypeElementSpec {

Expand Down Expand Up @@ -2459,4 +2463,41 @@ class MyBean {}
operation.requestBody.content."application/json".schema
operation.requestBody.content."application/json".schema.$ref == "#/components/schemas/SimpleBody"
}

@RestoreSystemProperties
void "test append micronaut.server.context-path to endpoints"() {
given:
System.setProperty(OpenApiConfigProperty.MICRONAUT_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
}
}

0 comments on commit de22caa

Please sign in to comment.