From 6ff8b94d4dd283151fa1936b039bf26f210d1119 Mon Sep 17 00:00:00 2001 From: Andriy Dmytruk Date: Tue, 20 Aug 2024 10:30:03 -0400 Subject: [PATCH] Fixes after Micronaut update --- .../io/micronaut/http/poja/SimpleServerSpec.groovy | 10 +++++----- .../io/micronaut/servlet/http/ServletHttpHandler.java | 7 ++++++- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/http-poja-apache/src/test/groovy/io/micronaut/http/poja/SimpleServerSpec.groovy b/http-poja-apache/src/test/groovy/io/micronaut/http/poja/SimpleServerSpec.groovy index 34e82bc8..b2da6492 100644 --- a/http-poja-apache/src/test/groovy/io/micronaut/http/poja/SimpleServerSpec.groovy +++ b/http-poja-apache/src/test/groovy/io/micronaut/http/poja/SimpleServerSpec.groovy @@ -21,8 +21,8 @@ class SimpleServerSpec extends BaseServerlessApplicationSpec { then: app.read() == """\ HTTP/1.1 200 Ok - Content-Type: text/plain Content-Length: 32 + Content-Type: text/plain Hello, Micronaut Without Netty! """.stripIndent() @@ -39,8 +39,8 @@ class SimpleServerSpec extends BaseServerlessApplicationSpec { then: app.read() == """\ HTTP/1.1 404 Not Found - Content-Type: application/json Content-Length: 140 + Content-Type: application/json {"_links":{"self":[{"href":"/invalid-test","templated":false}]},"_embedded":{"errors":[{"message":"Page Not Found"}]},"message":"Not Found"}""".stripIndent() } @@ -56,8 +56,8 @@ class SimpleServerSpec extends BaseServerlessApplicationSpec { then: app.read() == """\ HTTP/1.1 400 Bad Request - Content-Type: text/plain Content-Length: 32 + Content-Type: text/plain HTTP request could not be parsed""".stripIndent() } @@ -89,8 +89,8 @@ class SimpleServerSpec extends BaseServerlessApplicationSpec { then: app.read() == """\ HTTP/1.1 201 Created - Content-Type: text/plain Content-Length: 13 + Content-Type: text/plain Hello, Dream """.stripIndent() @@ -107,8 +107,8 @@ class SimpleServerSpec extends BaseServerlessApplicationSpec { then: app.read() == """\ HTTP/1.1 200 Ok - Content-Type: text/plain Content-Length: 15 + Content-Type: text/plain Hello, Dream1! """.stripIndent() diff --git a/servlet-core/src/main/java/io/micronaut/servlet/http/ServletHttpHandler.java b/servlet-core/src/main/java/io/micronaut/servlet/http/ServletHttpHandler.java index e9c2d714..cd0cac0b 100644 --- a/servlet-core/src/main/java/io/micronaut/servlet/http/ServletHttpHandler.java +++ b/servlet-core/src/main/java/io/micronaut/servlet/http/ServletHttpHandler.java @@ -46,6 +46,7 @@ import io.micronaut.http.context.event.HttpRequestReceivedEvent; import io.micronaut.http.context.event.HttpRequestTerminatedEvent; import io.micronaut.http.exceptions.HttpStatusException; +import io.micronaut.http.hateoas.JsonError; import io.micronaut.http.server.RequestLifecycle; import io.micronaut.http.server.RouteExecutor; import io.micronaut.http.server.types.files.FileCustomizableResponseType; @@ -420,7 +421,11 @@ private void encodeResponse(ServletExchange exchange, MessageBodyWriter messageBodyWriter = null; if (!(body instanceof HttpStatus)) { messageBodyWriter = routeInfoAttribute.map(RouteInfo::getMessageBodyWriter).orElse(null); - if (messageBodyWriter == null) { + if (messageBodyWriter == null + // A special POJA case because the TCK bring in the netty client dependency + || ("io.micronaut.http.netty.body.NettyCharSequenceBodyWriter".equals(messageBodyWriter.getClass().getName()) + && (JsonError.class.isAssignableFrom(bodyType))) + ) { MediaType finalMediaType = mediaType; Argument finalBodyArgument = bodyArgument; Optional> writer = messageBodyHandlerRegistry.findWriter(bodyArgument, List.of(mediaType));