Skip to content

Commit

Permalink
Allow context-path to be an empty String (#10231)
Browse files Browse the repository at this point in the history
* Allow context-path to be an empty String

* Update ConfigurableUriNamingStrategySpec
  • Loading branch information
jeremyg484 authored Dec 7, 2023
1 parent 34b5231 commit fc01a73
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 5 deletions.
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

0 comments on commit fc01a73

Please sign in to comment.