Skip to content

Commit 631a5d1

Browse files
committed
Remove support for Protobuf 2.x and protobuf-java-format
Closes gh-31465
1 parent 7271dfe commit 631a5d1

File tree

4 files changed

+7
-131
lines changed

4 files changed

+7
-131
lines changed

framework-platform/framework-platform.gradle

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ dependencies {
3030
api("com.google.code.findbugs:findbugs:3.0.1")
3131
api("com.google.code.findbugs:jsr305:3.0.2")
3232
api("com.google.code.gson:gson:2.10.1")
33-
api("com.google.protobuf:protobuf-java-util:3.23.2")
34-
api("com.googlecode.protobuf-java-format:protobuf-java-format:1.4")
33+
api("com.google.protobuf:protobuf-java-util:3.24.4")
3534
api("com.h2database:h2:2.2.220")
3635
api("com.jayway.jsonpath:json-path:2.8.0")
3736
api("com.rometools:rome:1.19.0")

spring-web/spring-web.gradle

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ dependencies {
1818
optional("com.fasterxml.woodstox:woodstox-core")
1919
optional("com.google.code.gson:gson")
2020
optional("com.google.protobuf:protobuf-java-util")
21-
optional("com.googlecode.protobuf-java-format:protobuf-java-format")
2221
optional("com.rometools:rome")
2322
optional("com.squareup.okhttp3:okhttp")
2423
optional("io.reactivex.rxjava3:rxjava")

spring-web/src/main/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverter.java

Lines changed: 6 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 the original author or authors.
2+
* Copyright 2002-2023 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,8 +32,6 @@
3232
import com.google.protobuf.Message;
3333
import com.google.protobuf.TextFormat;
3434
import com.google.protobuf.util.JsonFormat;
35-
import com.googlecode.protobuf.format.FormatFactory;
36-
import com.googlecode.protobuf.format.ProtobufFormatter;
3735

3836
import org.springframework.http.HttpInputMessage;
3937
import org.springframework.http.HttpOutputMessage;
@@ -48,8 +46,6 @@
4846
import org.springframework.util.ConcurrentReferenceHashMap;
4947

5048
import static org.springframework.http.MediaType.APPLICATION_JSON;
51-
import static org.springframework.http.MediaType.APPLICATION_XML;
52-
import static org.springframework.http.MediaType.TEXT_HTML;
5349
import static org.springframework.http.MediaType.TEXT_PLAIN;
5450

5551
/**
@@ -60,26 +56,17 @@
6056
* <p>To generate {@code Message} Java classes, you need to install the {@code protoc} binary.
6157
*
6258
* <p>This converter supports by default {@code "application/x-protobuf"} and {@code "text/plain"}
63-
* with the official {@code "com.google.protobuf:protobuf-java"} library. Other formats can be
64-
* supported with one of the following additional libraries on the classpath:
65-
* <ul>
66-
* <li>{@code "application/json"}, {@code "application/xml"}, and {@code "text/html"} (write-only)
67-
* with the {@code "com.googlecode.protobuf-java-format:protobuf-java-format"} third-party library
68-
* <li>{@code "application/json"} with the official {@code "com.google.protobuf:protobuf-java-util"}
69-
* for Protobuf 3 (see {@link ProtobufJsonFormatHttpMessageConverter} for a configurable variant)
70-
* </ul>
59+
* with the official {@code "com.google.protobuf:protobuf-java"} library.
60+
* The {@code "application/json"} format is also supported with the {@code "com.google.protobuf:protobuf-java-util"}
61+
* dependency. See {@link ProtobufJsonFormatHttpMessageConverter} for a configurable variant.
7162
*
72-
* <p>Requires Protobuf 2.6 or higher (and Protobuf Java Format 1.4 or higher for formatting).
73-
* This converter will auto-adapt to Protobuf 3 and its default {@code protobuf-java-util} JSON
74-
* format if the Protobuf 2 based {@code protobuf-java-format} isn't present; however, for more
75-
* explicit JSON setup on Protobuf 3, consider {@link ProtobufJsonFormatHttpMessageConverter}.
63+
* <p>This converter requires Protobuf 3 or higher as of Spring Framework 6.1.
7664
*
7765
* @author Alex Antonov
7866
* @author Brian Clozel
7967
* @author Juergen Hoeller
8068
* @author Sebastien Deleuze
8169
* @since 4.1
82-
* @see FormatFactory
8370
* @see JsonFormat
8471
* @see ProtobufJsonFormatHttpMessageConverter
8572
*/
@@ -105,18 +92,10 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
10592
*/
10693
public static final String X_PROTOBUF_MESSAGE_HEADER = "X-Protobuf-Message";
10794

108-
private static final boolean protobufFormatFactoryPresent;
109-
110-
private static final boolean protobufJsonFormatPresent;
95+
private static final boolean protobufJsonFormatPresent = ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", ProtobufHttpMessageConverter.class.getClassLoader());
11196

11297
private static final Map<Class<?>, Method> methodCache = new ConcurrentReferenceHashMap<>();
11398

114-
static {
115-
ClassLoader classLoader = ProtobufHttpMessageConverter.class.getClassLoader();
116-
protobufFormatFactoryPresent = ClassUtils.isPresent("com.googlecode.protobuf.format.FormatFactory", classLoader);
117-
protobufJsonFormatPresent = ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", classLoader);
118-
}
119-
12099

121100
final ExtensionRegistry extensionRegistry;
122101

@@ -146,9 +125,6 @@ public ProtobufHttpMessageConverter(ExtensionRegistry extensionRegistry) {
146125
if (formatSupport != null) {
147126
this.protobufFormatSupport = formatSupport;
148127
}
149-
else if (protobufFormatFactoryPresent) {
150-
this.protobufFormatSupport = new ProtobufJavaFormatSupport();
151-
}
152128
else if (protobufJsonFormatPresent) {
153129
this.protobufFormatSupport = new ProtobufJavaUtilSupport(null, null);
154130
}
@@ -290,73 +266,6 @@ void print(Message message, OutputStream output, MediaType contentType, Charset
290266
}
291267

292268

293-
/**
294-
* {@link ProtobufFormatSupport} implementation used when
295-
* {@code com.googlecode.protobuf.format.FormatFactory} is available.
296-
*/
297-
static class ProtobufJavaFormatSupport implements ProtobufFormatSupport {
298-
299-
private final ProtobufFormatter jsonFormatter;
300-
301-
private final ProtobufFormatter xmlFormatter;
302-
303-
private final ProtobufFormatter htmlFormatter;
304-
305-
public ProtobufJavaFormatSupport() {
306-
FormatFactory formatFactory = new FormatFactory();
307-
this.jsonFormatter = formatFactory.createFormatter(FormatFactory.Formatter.JSON);
308-
this.xmlFormatter = formatFactory.createFormatter(FormatFactory.Formatter.XML);
309-
this.htmlFormatter = formatFactory.createFormatter(FormatFactory.Formatter.HTML);
310-
}
311-
312-
@Override
313-
public MediaType[] supportedMediaTypes() {
314-
return new MediaType[] {PROTOBUF, TEXT_PLAIN, APPLICATION_XML, APPLICATION_JSON};
315-
}
316-
317-
@Override
318-
public boolean supportsWriteOnly(@Nullable MediaType mediaType) {
319-
return TEXT_HTML.isCompatibleWith(mediaType);
320-
}
321-
322-
@Override
323-
public void merge(InputStream input, Charset charset, MediaType contentType,
324-
ExtensionRegistry extensionRegistry, Message.Builder builder)
325-
throws IOException, HttpMessageConversionException {
326-
327-
if (contentType.isCompatibleWith(APPLICATION_JSON)) {
328-
this.jsonFormatter.merge(input, charset, extensionRegistry, builder);
329-
}
330-
else if (contentType.isCompatibleWith(APPLICATION_XML)) {
331-
this.xmlFormatter.merge(input, charset, extensionRegistry, builder);
332-
}
333-
else {
334-
throw new HttpMessageConversionException(
335-
"protobuf-java-format does not support parsing " + contentType);
336-
}
337-
}
338-
339-
@Override
340-
public void print(Message message, OutputStream output, MediaType contentType, Charset charset)
341-
throws IOException, HttpMessageConversionException {
342-
343-
if (contentType.isCompatibleWith(APPLICATION_JSON)) {
344-
this.jsonFormatter.print(message, output, charset);
345-
}
346-
else if (contentType.isCompatibleWith(APPLICATION_XML)) {
347-
this.xmlFormatter.print(message, output, charset);
348-
}
349-
else if (contentType.isCompatibleWith(TEXT_HTML)) {
350-
this.htmlFormatter.print(message, output, charset);
351-
}
352-
else {
353-
throw new HttpMessageConversionException(
354-
"protobuf-java-format does not support printing " + contentType);
355-
}
356-
}
357-
}
358-
359-
360269
/**
361270
* {@link ProtobufFormatSupport} implementation used when
362271
* {@code com.google.protobuf.util.JsonFormat} is available.

spring-web/src/test/java/org/springframework/http/converter/protobuf/ProtobufHttpMessageConverterTests.java

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,15 @@ void canRead() {
5555
assertThat(this.converter.canRead(Msg.class, null)).isTrue();
5656
assertThat(this.converter.canRead(Msg.class, ProtobufHttpMessageConverter.PROTOBUF)).isTrue();
5757
assertThat(this.converter.canRead(Msg.class, MediaType.APPLICATION_JSON)).isTrue();
58-
assertThat(this.converter.canRead(Msg.class, MediaType.APPLICATION_XML)).isTrue();
5958
assertThat(this.converter.canRead(Msg.class, MediaType.TEXT_PLAIN)).isTrue();
60-
61-
// only supported as an output format
62-
assertThat(this.converter.canRead(Msg.class, MediaType.TEXT_HTML)).isFalse();
6359
}
6460

6561
@Test
6662
void canWrite() {
6763
assertThat(this.converter.canWrite(Msg.class, null)).isTrue();
6864
assertThat(this.converter.canWrite(Msg.class, ProtobufHttpMessageConverter.PROTOBUF)).isTrue();
6965
assertThat(this.converter.canWrite(Msg.class, MediaType.APPLICATION_JSON)).isTrue();
70-
assertThat(this.converter.canWrite(Msg.class, MediaType.APPLICATION_XML)).isTrue();
7166
assertThat(this.converter.canWrite(Msg.class, MediaType.TEXT_PLAIN)).isTrue();
72-
assertThat(this.converter.canWrite(Msg.class, MediaType.TEXT_HTML)).isTrue();
7367
}
7468

7569
@Test
@@ -131,31 +125,6 @@ void writeJsonWithGoogleProtobuf() throws IOException {
131125
ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER)).isNull();
132126
}
133127

134-
@Test
135-
void writeJsonWithJavaFormat() throws IOException {
136-
this.converter = new ProtobufHttpMessageConverter(
137-
new ProtobufHttpMessageConverter.ProtobufJavaFormatSupport(),
138-
this.extensionRegistry);
139-
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
140-
@SuppressWarnings("deprecation")
141-
MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
142-
this.converter.write(this.testMsg, contentType, outputMessage);
143-
144-
assertThat(outputMessage.getHeaders().getContentType()).isEqualTo(contentType);
145-
146-
String body = outputMessage.getBodyAsString(StandardCharsets.UTF_8);
147-
assertThat(body).as("body is empty").isNotEmpty();
148-
149-
Msg.Builder builder = Msg.newBuilder();
150-
JsonFormat.parser().merge(body, builder);
151-
assertThat(builder.build()).isEqualTo(this.testMsg);
152-
153-
assertThat(outputMessage.getHeaders().getFirst(
154-
ProtobufHttpMessageConverter.X_PROTOBUF_MESSAGE_HEADER)).isNull();
155-
assertThat(outputMessage.getHeaders().getFirst(
156-
ProtobufHttpMessageConverter.X_PROTOBUF_SCHEMA_HEADER)).isNull();
157-
}
158-
159128
@Test
160129
void defaultContentType() throws Exception {
161130
assertThat(this.converter.getDefaultContentType(this.testMsg))

0 commit comments

Comments
 (0)