Skip to content

[Java][webclient] Filter out problem+json mime type from JSON mime types #9680

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 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,15 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
return mediaType != null && (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType) || mediaType.getSubtype().matches("^.*\\+json[;]?\\s*$"));
}

/**
* Check if the given {@code String} is a Problem JSON MIME (RFC-7807).
* @param mediaType the input MediaType
* @return boolean true if the MediaType represents Problem JSON, false otherwise
*/
public boolean isProblemJsonMime(String mediaType) {
return "application/problem+json".equalsIgnoreCase(mediaType);
}

/**
* Select the Accept header's value from the given accepts array:
* if JSON exists in the given array, use it;
Expand All @@ -526,7 +535,7 @@ public class ApiClient{{#jsr310}} extends JavaTimeFormatter{{/jsr310}} {
}
for (String accept : accepts) {
MediaType mediaType = MediaType.parseMediaType(accept);
if (isJsonMime(mediaType)) {
if (isJsonMime(mediaType) && !isProblemJsonMime(accept)) {
return Collections.singletonList(mediaType);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -497,6 +497,15 @@ public boolean isJsonMime(MediaType mediaType) {
return mediaType != null && (MediaType.APPLICATION_JSON.isCompatibleWith(mediaType) || mediaType.getSubtype().matches("^.*\\+json[;]?\\s*$"));
}

/**
* Check if the given {@code String} is a Problem JSON MIME (RFC-7807).
* @param mediaType the input MediaType
* @return boolean true if the MediaType represents Problem JSON, false otherwise
*/
public boolean isProblemJsonMime(String mediaType) {
return "application/problem+json".equalsIgnoreCase(mediaType);
}

/**
* Select the Accept header's value from the given accepts array:
* if JSON exists in the given array, use it;
Expand All @@ -511,7 +520,7 @@ public List<MediaType> selectHeaderAccept(String[] accepts) {
}
for (String accept : accepts) {
MediaType mediaType = MediaType.parseMediaType(accept);
if (isJsonMime(mediaType)) {
if (isJsonMime(mediaType) && !isProblemJsonMime(accept)) {
return Collections.singletonList(mediaType);
}
}
Expand Down