micronaut-projects / micronaut-core

Micronaut Application Framework
http://micronaut.io
Apache License 2.0
6.07k stars 1.07k forks source link

HttpRequest in ShoppingCartRequestArgumentBinder#bind method can not gain the http body #5341

Closed CoderYellow closed 3 years ago

CoderYellow commented 3 years ago

Thanks for reporting an issue, please review the task list below before submitting the issue. Your issue report will be closed if the issue is incomplete and the below tasks not completed.

NOTE: If you are unsure about something and the issue is more of a question a better place to ask questions is on Stack Overflow (https://stackoverflow.com/tags/micronaut) or Gitter (https://gitter.im/micronautfw/). DO NOT use the issue tracker to ask questions.

Task List

Steps to Reproduce

  1. Add the test endpoint to ShoppingCartController.groovy
    @Post("/customBody")
    @Consumes(MediaType.TEXT_PLAIN)
    HttpResponse<String> checkCustomPost( @ShoppingCart String body) { //<1>
        HttpResponse.ok("Body:" + body)
    }
  2. Add the test method to ShoppingCartControllerSpec.groovy
    void testBindingCustomBody() {
        when:
        String response = client.toBlocking().retrieve(HttpRequest.POST("/customBinding/customBody","BODY").contentType(MediaType.TEXT_PLAIN_TYPE))
        then:
        response == "Body:BODY"
    }
  3. Debug the test method and set the breakpoint here

Expected Behaviour

Http body should not be empty like DefaultBodyAnnotationBinder: image

Actual Behaviour

Empty in ShoppingCartRequestArgumentBinder#bind image

Environment Information

- **JDK Version:** 

openjdk version "1.8.0_292" OpenJDK Runtime Environment (build 1.8.0_292-b10) Eclipse OpenJ9 VM (build openj9-0.26.0, JRE 1.8.0 Linux amd64-64-Bit Compressed References 20210421_1000 (JIT enabled, AOT enabled) OpenJ9 - b4cc246d9 OMR - 162e6f729 JCL - 2a5e268814 based on jdk8u292-b10)


### Example Application

- TODO: link to github repository with example that reproduces the issue
CoderYellow commented 3 years ago

Are there any ways to bind the http body to the paremeter?

jameskleeh commented 3 years ago

Please upload an example application

CoderYellow commented 3 years ago

Please upload an example application

here

jameskleeh commented 3 years ago

Argument binders can be called multiple times, including before the body is available. You can check if the body is present, and if not, return UNSATISFIED. That will trigger the binder to be called again after the body is available. This is documented here https://docs.micronaut.io/latest/guide/#customArgumentBinding