Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add @ConsumseJson to contents API. (#999)
### Motivation: - When a client sends a request with a JSON body but does not configure the `content-type` in header, Central Dogma returns the response`: {"exception":"java.lang.IllegalArgumentException","message":"No suitable request converter found for a @RequestObject 'CommitMessageDto'"}`. This response is quite ambiguous, making it difficult for the client to understand the problem. ### Modifications: - Add `@ConsumesJson` to each method that uses `ChangesRequestConverter.class`. ### FYI 1. If `@RequestConverter` is declared on method signatures, Armeria will add an object Resolver. ([link](https://github.com/line/armeria/blob/e2b298dd2f54243e8801540f13e90166f75578b5/core/src/main/java/com/linecorp/armeria/internal/server/annotation/AnnotatedValueResolver.java#L501-L508)) 2. Then, when Armeria receives a request from client, Armeria will try to resolve the request. ([link](https://github.com/line/armeria/blob/e2b298dd2f54243e8801540f13e90166f75578b5/core/src/main/java/com/linecorp/armeria/internal/server/annotation/AnnotatedValueResolver.java#L829-L862)) 3. `ChangesRequestConverter` tries to parse body from request. Actually, `ChangeRequestConverter` delegates to `JacksonRequestConvertFunction`. 4. `JacksonRequestConverter` validates contents type. ([here](https://github.com/line/armeria/blob/e2b298dd2f54243e8801540f13e90166f75578b5/core/src/main/java/com/linecorp/armeria/server/annotation/JacksonRequestConverterFunction.java#L98-L106)) 5. If there is no `content-type` or `content-type` is not `application/json`, `JacksonRequestConverter` will call `RequestConverterFunction.fallthrough()`. With this flow, client will receive response `: {"exception":"java.lang.IllegalArgumentException","message":"No suitable request converter found for a @RequestObject 'CommitMessageDto'"}`. If `@ConsumsJson` is delcared to each method having `ChangesRequestConverter.class` on their method signature, Armeria will not try to resolve request without header `Content-Type: application/json`. ### Result: - Client will receive 415 response. ```sh $ curl -X POST localhost:8080/my-post \ -d '{"name": "John Doe", "email": "[email protected]"}' >> Status: 415 Description: Unsupported Media Type ``` - Closes #987.
- Loading branch information