micronaut-projects / micronaut-core

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

Body annotation value is ignored #2026

Closed bruno-lopes closed 5 years ago

bruno-lopes commented 5 years ago

Thanks for reporting an issue for Micronaut, 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 (http://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. Create a class Course this way:
    
    import lombok.Data;

import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.Table;

@Entity @Table(name = "course") @Data public class Course {

@Id
Long id;
String level;
String name;

}

2. Create a controller method that consumes JSON, and has a body parameter that is a reference from a nested JSON attribute:
```java
@Post(value = "/propg/alunoRegular", consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
    public HttpResponse<Alumn> createAlumn(@Valid @Body("course") Course curse)
  1. Send the following JSON:
{
    "name": "Test Name",
    "course": {
        "id": 4815
    }
}
  1. Debug and watch the value of the course variable.

Expected Behaviour

Course variable has to be instantiated with id value equal to 4815 and null in name attribute.

Actual Behaviour

Course variable has is instantiated with id value null and name attribute as "Test Name". If a create another parameter, like this:

@Post(value = "/propg/alunoRegular", consumes = MediaType.APPLICATION_JSON, produces = MediaType.APPLICATION_JSON)
    public HttpResponse<Alumn> createAlumn(@Valid @Body("course") Course curse, @Valid @Body DataFromCourse dataFromCourse)

And the following class

@Data
public class DataFromCourse {
    Course course;
}

The variable dataFromCourse is instantiated as expected, with the course attribute ìnstantiated with id 4815. So, the value of the Body Annotation is ignored, different from the class documentation suggests:

    /**
     * @return A Key or qualifier within the body. For example a reference to a nested JSON attribute
     */
    @AliasFor(annotation = Bindable.class, member = "value")
    String value() default ""

Environment Information

Example Application

Very simple example.

jameskleeh commented 5 years ago

Just FYI the desired behavior could be achieved by simply omitting the body annotation

bruno-lopes commented 5 years ago

Just FYI the desired behavior could be achieved by simply omitting the body annotation

Very thanks. I'll try it.