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

Add @ConsumseJson to contents API. #999

Merged
merged 4 commits into from
Aug 13, 2024

Conversation

chickenchickenlove
Copy link
Contributor

@chickenchickenlove chickenchickenlove commented Jul 28, 2024

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)
  2. Then, when Armeria receives a request from client, Armeria will try to resolve the request. (link)
  3. ChangesRequestConverter tries to parse body from request. Actually, ChangeRequestConverter delegates to JacksonRequestConvertFunction.
  4. JacksonRequestConverter validates contents type. (here)
  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.
$ curl -X POST localhost:8080/my-post \
-d '{"name": "John Doe", "email": "john.doe@example.com"}'
>>
Status: 415
Description: Unsupported Media Type

@CLAassistant
Copy link

CLAassistant commented Jul 28, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

codecov bot commented Jul 28, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 70.88%. Comparing base (c960313) to head (38bd2b1).
Report is 135 commits behind head on main.

Additional details and impacted files
@@             Coverage Diff              @@
##               main     #999      +/-   ##
============================================
+ Coverage     65.61%   70.88%   +5.27%     
- Complexity     3319     3933     +614     
============================================
  Files           355      388      +33     
  Lines         13865    15661    +1796     
  Branches       1498     1704     +206     
============================================
+ Hits           9098    11102    +2004     
+ Misses         3916     3580     -336     
- Partials        851      979     +128     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@ikhoon
Copy link
Contributor

ikhoon commented Jul 31, 2024

It looks good.

Client will receive 415 response.

Would you add a test that verifies the status? It is a simple test but will be helpful to detect regressions in the future.

@ikhoon ikhoon added this to the 0.68.1 milestone Jul 31, 2024
@ikhoon ikhoon added the defect label Jul 31, 2024
@chickenchickenlove
Copy link
Contributor Author

@ikhoon nim, i make a new commit to apply your comments.

I discovered something in the process.
When the Content-Type is not configured for each client, the default Content-Type may differ.

the values may differ!
dogma.client don't configure Content-Type. thus value is null.
curl configure application/x-form-urlencoded as default. thus value is application/x-form/urlencoded.
aiohttp.ClientSession() in python configure text/plain as default. thus value is text/plain.

Since the Dogma client defaults to a null Content-Type, it always will receive 400 response.
Therefore, to properly test this, I used parameterized tests to vary the Content-Type.

Copy link
Contributor

@ikhoon ikhoon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @chickenchickenlove! 👍👍

Copy link
Contributor

@jrhee17 jrhee17 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 👍

Copy link
Member

@minwoox minwoox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @chickenchickenlove ❤️

@minwoox minwoox merged commit 1ec504c into line:main Aug 13, 2024
8 of 10 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add @Consume(content-type) to all REST services class or methods
5 participants