Skip to content

Commit 3709b7e

Browse files
authored
On route mismatch, only consider alternate media types if method matches (#11684)
If the method doesn't match, that should be the error, not the fact that the content type is also wrong. Fixes #11468
1 parent fc56fb1 commit 3709b7e

File tree

2 files changed

+20
-6
lines changed

2 files changed

+20
-6
lines changed

http-server-netty/src/test/groovy/io/micronaut/http/server/netty/errors/ErrorSpec.groovy

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,19 @@ class ErrorSpec extends AbstractMicronautSpec {
186186
json._links.self.href == '/errors/server-error'
187187
}
188188

189+
@Issue("https://github.com/micronaut-projects/micronaut-core/issues/11468")
190+
void "test 405 error for wrong content type"() {
191+
when:
192+
httpClient.toBlocking().retrieve(
193+
HttpRequest.POST('/errors/server-error', 'blah')
194+
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
195+
)
196+
197+
then:
198+
def e = thrown HttpClientResponseException
199+
e.code() == HttpStatus.METHOD_NOT_ALLOWED.code
200+
}
201+
189202
void "test content type for error handler"() {
190203
given:
191204
HttpResponse response = Flux.from(httpClient.exchange(

http-server/src/main/java/io/micronaut/http/server/RequestLifecycle.java

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -484,12 +484,13 @@ final ExecutionFlow<HttpResponse<?>> onRouteMiss(HttpRequest<?> httpRequest, Pro
484484
final String routeMethod = anyRoute.getRouteInfo().getHttpMethodName();
485485
if (!requestMethodName.equals(routeMethod)) {
486486
allowedMethods.add(routeMethod);
487-
}
488-
if (contentType != null && !anyRoute.getRouteInfo().doesConsume(contentType)) {
489-
acceptableContentTypes.addAll(anyRoute.getRouteInfo().getConsumes());
490-
}
491-
if (hasAcceptHeader && !anyRoute.getRouteInfo().doesProduce(acceptedTypes)) {
492-
produceableContentTypes.addAll(anyRoute.getRouteInfo().getProduces());
487+
} else {
488+
if (contentType != null && !anyRoute.getRouteInfo().doesConsume(contentType)) {
489+
acceptableContentTypes.addAll(anyRoute.getRouteInfo().getConsumes());
490+
}
491+
if (hasAcceptHeader && !anyRoute.getRouteInfo().doesProduce(acceptedTypes)) {
492+
produceableContentTypes.addAll(anyRoute.getRouteInfo().getProduces());
493+
}
493494
}
494495
declaringType = anyRoute.getDeclaringType();
495496
}

0 commit comments

Comments
 (0)