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

Allow context-path to be an empty String #10231

Merged
merged 2 commits into from
Dec 7, 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 @@ -32,4 +32,18 @@ class ContextURISpec extends Specification {
cleanup:
embeddedServer.close()
}

void "test getContextURI returns the base URI when context path is set to an empty string"() {
when:
EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer, [
'micronaut.server.context-path': '',
'micronaut.server.port': 60006
])

then:
embeddedServer.getContextURI().toString() == 'http://localhost:60006'

cleanup:
embeddedServer.close()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import io.micronaut.context.annotation.Value;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.naming.conventions.PropertyConvention;
import io.micronaut.core.util.StringUtils;
import io.micronaut.inject.BeanDefinition;
import jakarta.inject.Singleton;

Expand Down Expand Up @@ -69,11 +70,13 @@ public String resolveUri(Class type) {
}

private String normalizeContextPath(String contextPath) {
if (contextPath.charAt(0) != '/') {
contextPath = '/' + contextPath;
}
if (contextPath.charAt(contextPath.length() - 1) == '/') {
contextPath = contextPath.substring(0, contextPath.length() - 1);
if (StringUtils.isNotEmpty(contextPath)) {
if (contextPath.charAt(0) != '/') {
contextPath = '/' + contextPath;
}
if (contextPath.charAt(contextPath.length() - 1) == '/') {
contextPath = contextPath.substring(0, contextPath.length() - 1);
}
}
return contextPath;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class ConfigurableUriNamingStrategySpec extends Specification {
applicationContext.close()
}



@Unroll
void "Test 'micronaut.server.context-path' matches with #route"() {
given:
Expand Down Expand Up @@ -163,6 +165,32 @@ class ConfigurableUriNamingStrategySpec extends Specification {
GET | '/test/city/country/Spain' | 'Country Spain'
}

@Unroll
void "Test 'micronaut.server.context-path' set to empty String matches with #route"() {
given:
def applicationContext = ApplicationContext.run(
PropertySource.of(
'test',
['micronaut.server.context-path': '']
)
)
.start()
def router = applicationContext.getBean(Router)

expect:
router."$method"(route).isPresent()
router."$method"(route).get().invoke() == result

cleanup:
applicationContext.close()

where:
method | route | result
GET | '/city' | 'Hello city'
GET | '/city/Madrid' | 'City Madrid'
GET | '/city/country/Spain' | 'Country Spain'
}

@Controller('/city')
static class CityController {

Expand Down
Loading