8000 Add config to enforce content types strictly by maths22 · Pull Request #2255 · javalin/javalin · GitHub
[go: up one dir, main page]

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 config to enforce content types strictly #2255

Merged
merged 5 commits into from
Jul 5, 2024

Conversation

maths22
Copy link
Contributor
@maths22 maths22 commented May 25, 2024

This is valuable for being able to unconditionally use formParams
in an application without risking consuming the body stream on
requests where the client did not send form params.

For bodyAsClass/bodyStreamAsClass, this will throw a BadRequestResponse when trying to convert a non-JSON body to a class. For formParams and friends, the formParam list will be empty when trying to read form params from a request with a non-form-param content type.

maths22 added 2 commits May 25, 2024 08:42
This is valuable for being able to unconditionally use formParams
in an application without risking consuming the body stream on
requests where the client did not send form params.
Copy link
codecov bot commented May 25, 2024

Codecov Report

Attention: Patch coverage is 52.94118% with 8 lines in your changes missing coverage. Please review.

Project coverage is 86.32%. Comparing base (01a6d55) to head (8192bcf).
Report is 25 commits behind head on master.

Files Patch % Lines
javalin/src/main/java/io/javalin/http/Context.kt 41.66% 1 Missing and 6 partials ⚠️
...-mock/src/main/java/io/javalin/mock/ContextMock.kt 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##             master    #2255      +/-   ##
============================================
- Coverage     86.42%   86.32%   -0.10%     
- Complexity     1299     1309      +10     
============================================
  Files           148      148              
  Lines          4029     4038       +9     
  Branches        504      508       +4     
============================================
+ Hits           3482     3486       +4     
- Misses          331      333       +2     
- Partials        216      219       +3     

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

@maths22 maths22 changed the title Add config to enforce form content params strictly Add config to enforce content types strictly May 30, 2024
/** Returns true if request is application/x-www-form-urlencoded. */
fun isFormUrlencoded(): Boolean = header(Header.CONTENT_TYPE)?.lowercase(Locale.ROOT)?.contains("application/x-www-form-urlencoded") == true

/** Returns true if request is application/x-www-form-urlencoded. */
Copy link
@norrs norrs May 30, 2024

Choose a reason for hiding this comment

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

Suggested change
/** Returns true if request is application/x-www-form-urlencoded. */
/** Returns true if request is application/json. */

I do however wonder if you should use startsWith for each value in the collection as usually content types can have definitions of character encoding etc in it, but it however should start with application/json. ie application/json; charset=iso-8859-1

@tipsy
Copy link
Member
tipsy commented Jul 5, 2024

Thanks @maths22, sorry about the long wait!

@tipsy tipsy merged commit b5d6bae into javalin:master Jul 5, 2024
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0