"Invalid UTF-8: Illegal surrogate character" when deserializing attribute name with encoded emoji
Since upgrading from Jackson 2.18.2 to 2.21.1, we encounter an exception during deserialization of a UTF-8 encoded JSON string with an attribute name that contains an encoded emojis character, e.g. {"\uD83D\uDE03": "x":}.
com.fasterxml.jackson.core.JsonParseException: Invalid UTF-8: Illegal surrogate character 0xd83d
at [Source: REDACTED (`StreamReadFeature.INCLUDE_SOURCE_IN_LOCATION` disabled); line: 1, column: 16]
at com.fasterxml.jackson.core.JsonParser._constructError(JsonParser.java:2599)
at com.fasterxml.jackson.core.JsonParser._constructReadException(JsonParser.java:2625)
at com.fasterxml.jackson.core.base.ParserMinimalBase._reportInvalidUTF8Surrogate(ParserMinimalBase.java:783)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.addName(UTF8StreamJsonParser.java:2413)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.parseEscapedName(UTF8StreamJsonParser.java:2076)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.parseName(UTF8StreamJsonParser.java:1973)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser._parseName(UTF8StreamJsonParser.java:1809)
at com.fasterxml.jackson.core.json.UTF8StreamJsonParser.nextFieldName(UTF8StreamJsonParser.java:1040)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer._readAndBindStringKeyMap(MapDeserializer.java:596)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:450)
at com.fasterxml.jackson.databind.deser.std.MapDeserializer.deserialize(MapDeserializer.java:31)
at com.fasterxml.jackson.databind.deser.DefaultDeserializationContext.readRootValue(DefaultDeserializationContext.java:342)
at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:5052)
at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:3991)
If the input contains an actual emoji character, it can be deserialized without error, e.g. {"😃": "x"}. The same is true if the encoded emoji appears in the attribute value, e.g. {"x": "\uD83D\uDE03"}. We observed that the issue occurs only when reading from an InputStream, not with in-memory Strings. The encoded character is generated by Jackson serialization, also only when dealing with streams.
A simple JUnit test demonstrating the problem is attached. It succeeds with 2.18.2, but fails with 2.21.1. JacksonEmojiReadWriteTest.java
The changed behavior seems to have been introduced with https://github.com/FasterXML/jackson-core/issues/363. Is this an intended effect? But if encoded characters in attribute names are now disallowed, why does Jackson generate such strings in the first place?
2.21.2 is released and has a fix for #1541 - there is a reasonable chance that that this is a duplicate of that
@pjfanning Thanks for the prompt response. Somehow I missed that issue in my research. You are correct, the new version 2.21.2 seems to resolve the issue.
2.21.2 was just released couple of days ok, so it's understandable.