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

HttpRequest#getBody returns JSON String under 3.0.x but returns JsonObject under 3.1.0 #6295

Closed
cbeams opened this issue Oct 7, 2021 · 3 comments
Assignees
Labels
priority: high High priority type: bug Something isn't working
Milestone

Comments

@cbeams
Copy link

cbeams commented Oct 7, 2021

Issue description

Attn: @yawkat as this deals with JsonObject, which it appears you authored in 3.1.

I have a code path in my application where I get the current thread-local Micronaut HttpRequest (from ServerRequestContext#currentRequest) and I then "readdress" and "forward" that request on to another server.

I do this by copying the (json) body from the Micronaut HttpRequest into a new java.net.http.HttpRequest and sending it to the destination URI. Here's the relevant code:

    public static void forward(io.micronaut.http.HttpRequest<?> originalRequest, String newAddress) {
        send(readdress(originalRequest, newAddress));
    }

    private static HttpRequest.Builder readdress(io.micronaut.http.HttpRequest<?> originalRequest, String newAddress) {
        var request = request(newAddress + originalRequest.getUri());
        var body = originalRequest.getBody(String.class).orElse("");
        return request.method(originalRequest.getMethodName(), HttpRequest.BodyPublishers.ofString(body));
    }

    public static HttpRequest.Builder request(String uri) {
        return request().uri(UriBuilder.of(uri).build());
    }

    private static HttpRequest.Builder request() {
        return HttpRequest.newBuilder().header("Content-Type", "application/json");
    }

Note: this is all a bit of a hack, but it appears to be the best I can do. I might have preferred to create a mutable copy of the Micronaut HttpRequest, update its URI and then send it off with a Micronaut HttpClient, but the thread-local HttpRequest does not support the .mutate() call. That left me needing to create an all-new request.

In any case, under 3.0.3, the call above to originalRequest.getBody(String.class) returned with the JSON body of the original request, such that I could simply copy it over to the new request and send it.

On upgrading from 3.0.3 to 3.1.0-SNAPSHOT, however, this same call now returns io.micronaut.json.tree.JsonObject, which cannot as easily be passed on to the new request. How can I best get from this JsonObject back to a stringified JSON representation? Thanks.

@yawkat yawkat self-assigned this Oct 7, 2021
@yawkat yawkat added priority: high High priority type: bug Something isn't working labels Oct 7, 2021
@yawkat yawkat added this to the 3.1.0 milestone Oct 7, 2021
@yawkat
Copy link
Member

yawkat commented Oct 7, 2021

Thanks for catching this. #6296 is the fix.

Unfortunately, the new JsonNode class can't have a json toString, because that would need a dependency on jackson-core. You can implement the toString yourself using JsonNodeTreeCodec, or after the fix go through ConversionService or JsonMapper. And of course getBody will now work as it did before.

@cbeams
Copy link
Author

cbeams commented Oct 7, 2021

Excellent, thanks! I'll try it out once #6296 is merged.

@cbeams
Copy link
Author

cbeams commented Oct 7, 2021

I've tried out the fix in #6296 and it works as expected now. I'm able to run on the latest 3.1.0-SNAPSHOT with no issues. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
priority: high High priority type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants