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

Different Behavior At Test/Runtime Regarding HTTP Controller #8240

Closed
ElderMael opened this issue Oct 26, 2022 · 3 comments
Closed

Different Behavior At Test/Runtime Regarding HTTP Controller #8240

ElderMael opened this issue Oct 26, 2022 · 3 comments
Labels
closed: notabug The issue is not a bug

Comments

@ElderMael
Copy link

Expected Behavior

A response is sent back to the server with the serialized JSON sent.

This application does pass the E2E tests written with RestAssured, at test time, the Controller seems to have the right behavior, but once the server is started without the test environment profile, the Controller stops working.

If one runs gradle test, the test environment and HTTP server start and process the requests without issues. But running the server with the java -jar command shows a different behavior.

Actual Behaviour

The /bug endpoint returns a HTTP 400 response. Fails with the following error:

Client side:

io.micronaut.http.client.exceptions.HttpClientResponseException: Bad Request

Server side HTTP exchange:

POST /bug HTTP/1.1
Accept: application/json, */*;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Content-Length: 10
Content-Type: application/json
Host: localhost:8080
User-Agent: HTTPie/3.2.1

{
    "a": "b"
}


HTTP/1.1 400 Bad Request
Content-Type: application/json
connection: keep-alive
content-length: 166
date: Wed, 26 Oct 2022 22:08:41 GMT

{
    "_embedded": {
        "errors": [
            {
                "message": "Required Body [body] not specified",
                "path": "/body"
            }
        ]
    },
    "_links": {
        "self": {
            "href": "/bug",
            "templated": false
        }
    },
    "message": "Bad Request"
}

Steps To Reproduce

Send an HTTP request to the /bug endpoint with any JSON payload. In this case the example application can send an http request using the following command:

# Send Request
java -jar build/libs/bug-demo-0.1-all.jar test

Environment Information

Pop OS/Mac OS
micronaut cli 3.7.2
java oracle-17

Example Application

https://github.com/ElderMael/micronaut-bug-demo

Version

3.6.2

@ElderMael
Copy link
Author

ElderMael commented Oct 27, 2022 via email

@yawkat
Copy link
Member

yawkat commented Nov 7, 2022

@dstepanov this is another bug caused by #7635 – this time it's JsonConverterRegistrar that is missing. Unfortunately that registrar needs a bean from the context (JsonMapper) so we can't use SPI this time.

@ElderMael The issue appears because you have two concurrent ApplicationContext instances, one from micronaut picocli, one you create yourself. It's a bug on our end that this doesn't work, but you can work around it by using one context like this:

Index: src/main/java/io/eldermael/micronaut/bug/demo/client/BugDemoCommand.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/src/main/java/io/eldermael/micronaut/bug/demo/client/BugDemoCommand.java b/src/main/java/io/eldermael/micronaut/bug/demo/client/BugDemoCommand.java
--- a/src/main/java/io/eldermael/micronaut/bug/demo/client/BugDemoCommand.java	(revision e301423a50cca8767a68dc69bd806f74e5c60a9e)
+++ b/src/main/java/io/eldermael/micronaut/bug/demo/client/BugDemoCommand.java	(date 1667831044023)
@@ -1,8 +1,7 @@
 package io.eldermael.micronaut.bug.demo.client;
 
-import io.eldermael.micronaut.bug.demo.Application;
-import io.eldermael.micronaut.bug.demo.server.Server;
-import io.micronaut.runtime.Micronaut;
+import io.micronaut.runtime.server.EmbeddedServer;
+import jakarta.inject.Inject;
 import picocli.CommandLine.Command;
 import picocli.CommandLine.Option;
 
@@ -17,6 +16,9 @@
   @Option(names = {"--server"}, description = "...")
   boolean server;
 
+  @Inject
+  EmbeddedServer embeddedServer;
+
   public void run() {
     // business logic here
     if (verbose) {
@@ -24,7 +26,7 @@
     }
 
     if(this.server) {
-      Micronaut.run(Server.class, Application.getArgs());
+      embeddedServer.start();
     }
   }
 }

You also need some code to keep the application running after this, otherwise it will just shut down immediately.

@graemerocher graemerocher added the closed: notabug The issue is not a bug label Nov 7, 2022
@graemerocher
Copy link
Contributor

Don't think this is a bug on our end or something we can do about.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
closed: notabug The issue is not a bug
Projects
None yet
Development

No branches or pull requests

3 participants