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

[CAMEL-21199] Camel-jackson not properly marshalling 4-byte characters #15515

Closed
wants to merge 1 commit into from
Closed
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 @@ -19,6 +19,7 @@
import java.io.File;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.util.ArrayList;
import java.util.Collection;
Expand Down Expand Up @@ -157,7 +158,11 @@ public void marshal(Exchange exchange, Object graph, OutputStream stream) throws
if (this.schemaResolver != null) {
schema = this.schemaResolver.resolve(exchange);
}
this.objectMapper.writerWithView(jsonView).with(schema).writeValue(stream, graph);
// using OutputStreamWriter because of Jackson not handling 4-byte characters properly with only a stream
// https://github.com/FasterXML/jackson-core/issues/223
try (OutputStreamWriter writer = new OutputStreamWriter(stream)) {
this.objectMapper.writerWithView(jsonView).with(schema).writeValue(writer, graph);
}

if (contentTypeHeader) {
exchange.getMessage().setHeader(Exchange.CONTENT_TYPE, getDefaultContentType());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.camel.component.jackson;

import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.mock.MockEndpoint;
import org.apache.camel.test.junit5.CamelTestSupport;
import org.junit.jupiter.api.Test;

import java.util.HashMap;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;

public class JacksonMarshalMultibyteCharactersTest extends CamelTestSupport {

@Test
public void testMarshal4ByteCharacter() throws Exception {
Map<String, Object> in = new HashMap<>();
in.put("name", "システム\uD867\uDE3D");
Copy link
Contributor

Choose a reason for hiding this comment

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

We use unicode escape to have foregin symbols in the camel source code.

See for example
https://github.com/apache/camel/blob/main/core/camel-core/src/test/java/org/apache/camel/component/file/FileConvertBodyToUTF8Test.java

Can you see if you can replace those to escaped values in this PR unit test.


MockEndpoint mock = getMockEndpoint("mock:reverse");
mock.expectedMessageCount(1);
mock.message(0).body().isInstanceOf(Map.class);
mock.message(0).body().isEqualTo(in);

Object marshalled = template.requestBody("direct:in", in);
String marshalledAsString = context.getTypeConverter().convertTo(String.class, marshalled);

// prior to fixing CAMEL-21199, this was the result returned by Jackson
assertNotEquals("{\"name\":\"システム\\uD867\\uDE3D\"}", marshalledAsString);

assertEquals("{\"name\":\"システム\uD867\uDE3D\"}", marshalledAsString);

template.sendBody("direct:back", marshalled);
mock.assertIsSatisfied();
}

@Override
protected RouteBuilder createRouteBuilder() {
return new RouteBuilder() {

@Override
public void configure() {
JacksonDataFormat format = new JacksonDataFormat();
from("direct:in").marshal(format);
from("direct:back").unmarshal(format).to("mock:reverse");
}
};
}

}