Skip to content

ProtobufMessageConverter fails to parse JSON payload if byte array is used #27408

@white-sagittarius

Description

@white-sagittarius

Affects: org.springframework:spring-messaging:5.3.6


We are using org.springframework:spring-messaging:5.3.6 with com.google.protobuf:protobuf-java:3.13.0 and com.google.protobuf:protobuf-java-util:3.13.0 to send and receive protobuf messages as application/json.

static class ProtobufJavaUtilSupport implements ProtobufFormatSupport {
...
@Override
public void merge(org.springframework.messaging.Message<?> message, Charset charset,
		MimeType contentType, ExtensionRegistry extensionRegistry, Message.Builder builder)
		throws IOException, MessageConversionException {

	if (contentType.isCompatibleWith(APPLICATION_JSON)) {
		this.parser.merge(message.getPayload().toString(), builder);
	}
	else {
		throw new MessageConversionException(
				"protobuf-java-util does not support parsing " + contentType);
	}
}
...
}

message.getPayload() returns a byte array (GenericMessage [payload=byte[230], headers={...}]). Calling toString on a byte array produces a string similar to "[B@27fe3806".

Attempt to parse resulting string as JSON produces the following error:

com.google.protobuf.InvalidProtocolBufferException: java.io.EOFException: End of input at line 1 column 12 path $[1]
at com.google.protobuf.util.JsonFormat$ParserImpl.merge (JsonFormat.java:1347)
at com.google.protobuf.util.JsonFormat$Parser.merge (JsonFormat.java:477)
at org.springframework.messaging.converter.ProtobufMessageConverter$ProtobufJavaUtilSupport.merge (ProtobufMessageConverter.java:265)
at org.springframework.messaging.converter.ProtobufMessageConverter.convertFromInternal (ProtobufMessageConverter.java:150)

I assume that byte arrays need special handling so that we use String constructor instead of toString() call in order to get actual JSON content (i.e. something akin to new String(message.getPayload(), StandardCharsets.UTF_8)).

Metadata

Metadata

Assignees

Labels

in: messagingIssues in messaging modules (jms, messaging)type: bugA general bug

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions