-
Notifications
You must be signed in to change notification settings - Fork 6k
[JAVA][#5172] Allow vendor json media types #5189
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
8ca9240
[#5172] Allow vendor json media types
02639bd
Revert unnecessary diffs
f336763
Update petstore sample
dfbd661
Didn't run mvn after some edits
f10952a
Merge remote-tracking branch 'upstream/master' into FIX-5172
0e62b83
Merge remote-tracking branch 'upstream/master' into FIX-5172
49e73a2
Rerun ' ./bin/java-petstore-all.sh' and './bin/security/java-petstore…
3e81211
Added more realistic test cases for isJsonMime
c31d597
Merge remote-tracking branch 'upstream/master' into FIX-5172
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -720,12 +720,13 @@ public class ApiClient { | |
| * application/json | ||
| * application/json; charset=UTF8 | ||
| * APPLICATION/JSON | ||
| * | ||
| * application/vnd.company+json | ||
| * @param mime MIME (Multipurpose Internet Mail Extensions) | ||
| * @return True if the given MIME is JSON, false otherwise. | ||
| */ | ||
| public boolean isJsonMime(String mime) { | ||
| return mime != null && mime.matches("(?i)application\\/json(;.*)?"); | ||
| String jsonMime = "(?i)^(application/json|[^;/ \t]+/[^;/ \t]+[+]json)[ \t]*(;.*)?$"; | ||
| return mime != null && mime.matches(jsonMime); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Later I will file a PR to put these 2 lines into one without declaring a temporary variable. |
||
| } | ||
|
|
||
| /** | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
81 changes: 81 additions & 0 deletions
81
...ecurity-test/java/okhttp-gson/src/main/java/io/swagger/client/GzipRequestInterceptor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Swagger Petstore *_/ ' \" =end -- \\r\\n \\n \\r | ||
| * This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\ *_/ ' \" =end -- | ||
| * | ||
| * OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r | ||
| * Contact: apiteam@swagger.io *_/ ' \" =end -- \\r\\n \\n \\r | ||
| * | ||
| * NOTE: This class is auto generated by the swagger code generator program. | ||
| * https://github.com/swagger-api/swagger-codegen.git | ||
| * Do not edit the class manually. | ||
| */ | ||
|
|
||
|
|
||
| package io.swagger.client; | ||
|
|
||
| import com.squareup.okhttp.*; | ||
| import okio.Buffer; | ||
| import okio.BufferedSink; | ||
| import okio.GzipSink; | ||
| import okio.Okio; | ||
|
|
||
| import java.io.IOException; | ||
|
|
||
| /** | ||
| * Encodes request bodies using gzip. | ||
| * | ||
| * Taken from https://github.com/square/okhttp/issues/350 | ||
| */ | ||
| class GzipRequestInterceptor implements Interceptor { | ||
| @Override public Response intercept(Chain chain) throws IOException { | ||
| Request originalRequest = chain.request(); | ||
| if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) { | ||
| return chain.proceed(originalRequest); | ||
| } | ||
|
|
||
| Request compressedRequest = originalRequest.newBuilder() | ||
| .header("Content-Encoding", "gzip") | ||
| .method(originalRequest.method(), forceContentLength(gzip(originalRequest.body()))) | ||
| .build(); | ||
| return chain.proceed(compressedRequest); | ||
| } | ||
|
|
||
| private RequestBody forceContentLength(final RequestBody requestBody) throws IOException { | ||
| final Buffer buffer = new Buffer(); | ||
| requestBody.writeTo(buffer); | ||
| return new RequestBody() { | ||
| @Override | ||
| public MediaType contentType() { | ||
| return requestBody.contentType(); | ||
| } | ||
|
|
||
| @Override | ||
| public long contentLength() { | ||
| return buffer.size(); | ||
| } | ||
|
|
||
| @Override | ||
| public void writeTo(BufferedSink sink) throws IOException { | ||
| sink.write(buffer.snapshot()); | ||
| } | ||
| }; | ||
| } | ||
|
|
||
| private RequestBody gzip(final RequestBody body) { | ||
| return new RequestBody() { | ||
| @Override public MediaType contentType() { | ||
| return body.contentType(); | ||
| } | ||
|
|
||
| @Override public long contentLength() { | ||
| return -1; // We don't know the compressed length in advance! | ||
| } | ||
|
|
||
| @Override public void writeTo(BufferedSink sink) throws IOException { | ||
| BufferedSink gzipSink = Okio.buffer(new GzipSink(sink)); | ||
| body.writeTo(gzipSink); | ||
| gzipSink.close(); | ||
| } | ||
| }; | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are tab characters really allowed in media type names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, according to the grammar described here in RFC-7230. You can see that
HTABcorresponds to horizontal tab here in RFC-5234.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, thanks.