From 87607cccff82f818d75a3e0b27b7eadcfc7997b0 Mon Sep 17 00:00:00 2001 From: rstoyanchev Date: Mon, 20 Oct 2025 13:25:38 +0100 Subject: [PATCH] Polishing in Protobuf support See gh-35403 --- .../codec/protobuf/ProtobufCodecSupport.java | 4 ++-- .../http/codec/protobuf/ProtobufDecoder.java | 3 +-- .../codec/protobuf/ProtobufJsonDecoder.java | 21 ++++++++++++++----- .../ProtobufHttpMessageConverter.java | 1 + 4 files changed, 20 insertions(+), 9 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufCodecSupport.java b/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufCodecSupport.java index f49c07fa918..3ed35d0f9fe 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufCodecSupport.java +++ b/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufCodecSupport.java @@ -47,8 +47,8 @@ public abstract class ProtobufCodecSupport { if (mimeType == null) { return true; } - for (MimeType m : MIME_TYPES) { - if (m.isCompatibleWith(mimeType)) { + for (MimeType supportedMimeType : MIME_TYPES) { + if (supportedMimeType.isCompatibleWith(mimeType)) { return true; } } diff --git a/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java b/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java index 14d850419bc..cb35f272dbe 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufDecoder.java @@ -129,8 +129,7 @@ public class ProtobufDecoder extends ProtobufCodecSupport implements Decoder decode(Publisher inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map hints) { - MessageDecoderFunction decoderFunction = - new MessageDecoderFunction(elementType, this.maxMessageSize); + MessageDecoderFunction decoderFunction = new MessageDecoderFunction(elementType, this.maxMessageSize); return Flux.from(inputStream) .flatMapIterable(decoderFunction) diff --git a/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufJsonDecoder.java b/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufJsonDecoder.java index 4b020c18ee9..0bb13897dea 100644 --- a/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufJsonDecoder.java +++ b/spring-web/src/main/java/org/springframework/http/codec/protobuf/ProtobufJsonDecoder.java @@ -131,12 +131,19 @@ public class ProtobufJsonDecoder implements Decoder { } @Override - public Flux decode(Publisher inputStream, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map hints) { - return Flux.error(new UnsupportedOperationException("Protobuf decoder does not support Flux, use Mono> instead.")); + public Flux decode( + Publisher inputStream, ResolvableType targetType, @Nullable MimeType mimeType, + @Nullable Map hints) { + + return Flux.error(new UnsupportedOperationException( + "Protobuf decoder does not support Flux, use Mono> instead.")); } @Override - public Message decode(DataBuffer dataBuffer, ResolvableType targetType, @Nullable MimeType mimeType, @Nullable Map hints) throws DecodingException { + public Message decode( + DataBuffer dataBuffer, ResolvableType targetType, @Nullable MimeType mimeType, + @Nullable Map hints) throws DecodingException { + try { Message.Builder builder = getMessageBuilder(targetType.toClass()); this.parser.merge(new InputStreamReader(dataBuffer.asInputStream()), builder); @@ -164,10 +171,14 @@ public class ProtobufJsonDecoder implements Decoder { } @Override - public Mono decodeToMono(Publisher inputStream, ResolvableType elementType, @Nullable MimeType mimeType, @Nullable Map hints) { + public Mono decodeToMono( + Publisher inputStream, ResolvableType elementType, @Nullable MimeType mimeType, + @Nullable Map hints) { + return DataBufferUtils.join(inputStream, this.maxMessageSize) .map(dataBuffer -> decode(dataBuffer, elementType, mimeType, hints)) - .onErrorMap(DataBufferLimitException.class, exc -> new DecodingException("Could not decode JSON as Protobuf message", exc)); + .onErrorMap(DataBufferLimitException.class, + exc -> new DecodingException("Could not decode JSON as Protobuf message", exc)); } } diff --git a/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java b/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java index ce88dc3cf97..866a9127091 100644 --- a/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java +++ b/spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java @@ -257,6 +257,7 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter