eclipse-ee4j / eclipse-ee4j/jersey

Jersey's ValidationErrorMessageBodyWriter incorrectly handles charset

Open
#3,541 3 comments 0 reactions 0 assignees View on GitHub
bean-validation beanvalidation Component: extensions Component: media Type: Bug
Dominant language
Java
Stars
730
Forks
382
PR merge metrics
No merged PRs in 30d

Description

I am using bean validation extension to validate my post entities and controller parameters. By default if I set up everything correctly, Jersey should convert constraint violation errors into a new response with the proper status. As stated in the documentation, currently only the following MediaTypes supported:

> text/plain
> text/html
> application/xml
> application/json

But, if I have a filter that sets the charset to for instance UTF-8, then a check fails in **ValidationErrorMessageBodyWriter** that causes an exception of not able to fine a message writer being thrown.

Here is the code piece that needs to be changed:

```
**ValidationErrorMessageBodyWriter.java** @Override
public boolean isWriteable(final Class type,
final Type genericType,
final Annotation[] annotations,
final MediaType mediaType) {
return isSupportedMediaType(mediaType) && isSupportedType(type, genericType);
}
private boolean isSupportedMediaType(final MediaType mediaType) {
return MediaType.TEXT_HTML_TYPE.equals(mediaType) || MediaType.TEXT_PLAIN_TYPE.equals(mediaType);
}
```

MediaType.TEXT_PLAIN_TYPE.equals(mediaType) fails because my mediaType has charset. _isSupportedMediaType_ should not take into account mediaType's parameters. Moreover, in the writeTo method of that class you have the following line:

> entityStream.write(builder.toString().getBytes(MessageUtils.getCharset(mediaType)));

which will never actually pull any charset, because the first check only lets in responses without any parameters in the media type being returned.
#### Environment
MacOS Sierra 10.12.4,
Java(TM) SE Runtime Environment (build 1.8.0_92-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.92-b14, mixed mode)
#### Affected Versions
[2.22.1, 2.25.1]

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.