Browse Source

Merge branch '6.1.x'

pull/33097/head
Brian Clozel 2 years ago
parent
commit
7f9e541f58
  1. 9
      spring-messaging/src/main/java/org/springframework/messaging/converter/ProtobufMessageConverter.java
  2. 11
      spring-messaging/src/test/java/org/springframework/messaging/converter/ProtobufMessageConverterTests.java

9
spring-messaging/src/main/java/org/springframework/messaging/converter/ProtobufMessageConverter.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -265,7 +265,12 @@ public class ProtobufMessageConverter extends AbstractMessageConverter { @@ -265,7 +265,12 @@ public class ProtobufMessageConverter extends AbstractMessageConverter {
throws IOException, MessageConversionException {
if (contentType.isCompatibleWith(APPLICATION_JSON)) {
this.parser.merge(message.getPayload().toString(), builder);
if (message.getPayload() instanceof byte[] bytes) {
this.parser.merge(new String(bytes, charset), builder);
}
else {
this.parser.merge(message.getPayload().toString(), builder);
}
}
else {
throw new MessageConversionException(

11
spring-messaging/src/test/java/org/springframework/messaging/converter/ProtobufMessageConverterTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -16,6 +16,7 @@ @@ -16,6 +16,7 @@
package org.springframework.messaging.converter;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import org.junit.jupiter.api.Test;
@ -49,14 +50,14 @@ class ProtobufMessageConverterTests { @@ -49,14 +50,14 @@ class ProtobufMessageConverterTests {
private Message<byte[]> messageWithoutContentType = MessageBuilder.withPayload(this.testMsg.toByteArray()).build();
private final Message<String> messageJson = MessageBuilder.withPayload("""
private final Message<byte[]> messageJson = MessageBuilder.withPayload("""
{
"foo": "Foo",
"blah": {
"blah": 123
}
}
""")
""".getBytes(StandardCharsets.UTF_8))
.setHeader(CONTENT_TYPE, APPLICATION_JSON)
.build();
@ -113,10 +114,10 @@ class ProtobufMessageConverterTests { @@ -113,10 +114,10 @@ class ProtobufMessageConverterTests {
Message<?> message = converter.toMessage(testMsg, new MessageHeaders(Map.of(CONTENT_TYPE, APPLICATION_JSON)));
assertThat(message).isNotNull();
assertThat(message.getHeaders().get(CONTENT_TYPE)).isEqualTo(APPLICATION_JSON);
JSONAssert.assertEquals(messageJson.getPayload(), message.getPayload().toString(), true);
JSONAssert.assertEquals(new String(messageJson.getPayload()), message.getPayload().toString(), true);
//convertFrom
assertThat(converter.fromMessage(message, Msg.class)).isEqualTo(testMsg);
assertThat(converter.fromMessage(messageJson, Msg.class)).isEqualTo(testMsg);
}
}

Loading…
Cancel
Save