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

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

Closed
white-sagittarius opened this issue Sep 15, 2021 · 6 comments
Assignees
Labels
in: messaging Issues in messaging modules (jms, messaging) type: bug A general bug
Milestone

Comments

@white-sagittarius
Copy link

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)).

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Sep 15, 2021
@rstoyanchev rstoyanchev added the in: messaging Issues in messaging modules (jms, messaging) label Nov 10, 2021
@snicoll
Copy link
Member

snicoll commented Sep 26, 2023

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

Yes although it looks a bit odd to me that the MimeType of the body is application/json and then the actual content of the body is not text. To make sure I am not missing anything, can you please share a small sample that we can run ourselves? You can do so by attaching a zip to this issue or pushing the code to a separate GitHub repository.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Sep 26, 2023
@spring-projects-issues
Copy link
Collaborator

If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed.

@spring-projects-issues spring-projects-issues added the status: feedback-reminder We've sent a reminder that we need additional information before we can continue label Oct 3, 2023
@spring-projects-issues
Copy link
Collaborator

Closing due to lack of requested feedback. If you would like us to look at this issue, please provide the requested information and we will re-open the issue.

@spring-projects-issues spring-projects-issues closed this as not planned Won't fix, can't repro, duplicate, stale Oct 10, 2023
@spring-projects-issues spring-projects-issues removed status: waiting-for-feedback We need additional information before we can continue status: feedback-reminder We've sent a reminder that we need additional information before we can continue status: waiting-for-triage An issue we've not yet triaged or decided on labels Oct 10, 2023
@gnagy
Copy link
Contributor

gnagy commented Apr 17, 2024

Hi, I just ran into this exact same issue on a project. My use case is to read Protobuf messages from Kafka (has ByteArray native payload format) and ProtobufMessageConverter is the only converter configured.

Following the answer here https://stackoverflow.com/questions/70121156/how-to-use-org-springframework-messaging-converter-protobufmessageconverter have subclassed ProtobufMessageConverter.

This works OK for binary protobuf encoding, when content-type: application/x-protobuf is set. The same converter supports JSON encoding (by checking protobufFormatSupport field) when content-type: application/json is set, but fails with the exception described above.

Looking at other MessageConverters, they seem to support multiple payload types, therefore I believe in ProtobufMessageConverter it is a bug.

@gnagy
Copy link
Contributor

gnagy commented Apr 17, 2024

Another related shortcoming:

If com.google.protobuf:protobuf-java-util is on the classpath, therefore ProtobufMessageConverter.protobufJsonFormatPresent is true, then ProtobufMessageConverter will try to handle content-type: application/json messages, preventing another JSON MessageConverter to handle those. This will fail for cases when protobuf binary and non-protobuf JSON topics are configured within the same app. There is no apparent way to configure the supported mime types in ProtobufMessageConverter.

@siaavush
Copy link

I have the exact same problem here

@bclozel bclozel self-assigned this Jun 25, 2024
@bclozel bclozel added the type: bug A general bug label Jun 25, 2024
@bclozel bclozel added this to the 6.1.11 milestone Jun 25, 2024
@bclozel bclozel reopened this Jun 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: messaging Issues in messaging modules (jms, messaging) type: bug A general bug
Projects
None yet
Development

No branches or pull requests

7 participants