-
Notifications
You must be signed in to change notification settings - Fork 737
Jackson parsing fails when using an UTF-8 character for link masking #705
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
Comments
The character seems to break the JSON parsing on Windows as reported in Spring REST Docs [0]. We temporarily move off the dedicated character in favor of three dots. [0] spring-projects/spring-restdocs#705.
Some experimentation is suggesting that this isn't a REST Docs problem and that the The following UTF-8-encoded source appears to be sufficient to reproduce the problem:
When run on a JVM using CP1252 as its default encoding, the following output is produced:
Switching the JVM's default encoding to UTF-8 produces the expected output:
@odrotbohm Can you try your app with the JVM running the tests configured to use UTF-8 as its default encoding ( |
Hm, to me it looks like the |
I've spent some time in a Windows VM and I think I have a more complete understanding of the problem.
Yeah, changes along these lines do appear to fix the problem: @@ -16,6 +16,8 @@
package org.springframework.restdocs.operation.preprocess;
+import java.nio.charset.Charset;
+import java.nio.charset.StandardCharsets;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -47,13 +49,8 @@ class PatternReplacingContentModifier implements ContentModifier {
@Override
public byte[] modifyContent(byte[] content, MediaType contentType) {
- String original;
- if (contentType != null && contentType.getCharset() != null) {
- original = new String(content, contentType.getCharset());
- }
- else {
- original = new String(content);
- }
+ Charset charset = (contentType != null && contentType.getCharset() != null) ? contentType.getCharset() : StandardCharsets.UTF_8;
+ String original = new String(content, charset);
Matcher matcher = this.pattern.matcher(original);
StringBuilder builder = new StringBuilder();
int previous = 0;
@@ -73,7 +70,7 @@ class PatternReplacingContentModifier implements ContentModifier {
if (previous < original.length()) {
builder.append(original.substring(previous));
}
- return builder.toString().getBytes();
+ return builder.toString().getBytes(charset);
}
} That said, I'm not sure that this is the right change to make. Switching from using the JVM's default encoding as a fallback to UTF-8 will fix this problem, but it may create others when UTF-8 isn't the right encoding to use. I'm concerned about such a change breaking people's tests in a maintenance release. To reduce the scope of the change but still address the problem, I think it may be better for |
Lovely, thanks, Andy! 🙇 |
Uh oh!
There was an error while loading. Please reload this page.
When using e.g. the UTF-8 ellipsis character (
…
) passed intomaskLinks(…)
, the execution fails on windows.I wasn't able to obtain a more complete stack trace yet but will further investigate.
Update I managed to obtain more stack trace:
Also, tweaking
AbstractJsonLinkExtractor.extractLinks(…)
to rather use….getContentAsString()
over….getContent()
seems to workaround the error as that replaces mangled characters with the replacement character (?
). I.e. it looks like something is platform encoding the content byte array.The text was updated successfully, but these errors were encountered: