Skip to content

Commit 36bbbab

Browse files
committed
Deprecate ExtensionRegistryInitializer in protobuf support
In order to be consistent with SPR-15776, and since it does not provide clear added value, this commit deprecates ExtensionRegistryInitializer and uses ExtensionRegistry parameter instead in ProtobufHttpMessageConverter and ProtobufJsonFormatHttpMessageConverter constructors. Issue: SPR-17081
1 parent dd4468a commit 36bbbab

File tree

5 files changed

+80
-15
lines changed

5 files changed

+80
-15
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,13 @@
2525
* <p>This interface provides a facility to populate the {@code ExtensionRegistry}.
2626
*
2727
* @author Alex Antonov
28+
* @author Sebastien Deleuze
2829
* @since 4.1
2930
* @see <a href="https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/ExtensionRegistry">
3031
* com.google.protobuf.ExtensionRegistry</a>
32+
* @deprecated as of Spring Framework 5.1, use {@link ExtensionRegistry} based contructors instead
3133
*/
34+
@Deprecated
3235
public interface ExtensionRegistryInitializer {
3336

3437
/**

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

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@
7777
* @author Alex Antonov
7878
* @author Brian Clozel
7979
* @author Juergen Hoeller
80+
* @author Sebastien Deleuze
8081
* @since 4.1
8182
* @see FormatFactory
8283
* @see JsonFormat
@@ -107,7 +108,7 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
107108

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

110-
private final ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
111+
final ExtensionRegistry extensionRegistry;
111112

112113
@Nullable
113114
private final ProtobufFormatSupport protobufFormatSupport;
@@ -117,20 +118,34 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
117118
* Construct a new {@code ProtobufHttpMessageConverter}.
118119
*/
119120
public ProtobufHttpMessageConverter() {
120-
this(null);
121+
this(null, null);
121122
}
122123

123124
/**
124125
* Construct a new {@code ProtobufHttpMessageConverter} with an
125126
* initializer that allows the registration of message extensions.
126127
* @param registryInitializer an initializer for message extensions
128+
* @deprecated as of Spring Framework 5.1, use {@link #ProtobufHttpMessageConverter(ExtensionRegistry)} instead
127129
*/
130+
@Deprecated
128131
public ProtobufHttpMessageConverter(@Nullable ExtensionRegistryInitializer registryInitializer) {
129-
this(null, registryInitializer);
132+
this(null, null);
133+
if (registryInitializer != null) {
134+
registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
135+
}
136+
}
137+
138+
/**
139+
* Construct a new {@code ProtobufHttpMessageConverter} with a registry that specifies
140+
* protocol message extensions.
141+
* @param extensionRegistry the registry to populate
142+
*/
143+
public ProtobufHttpMessageConverter(ExtensionRegistry extensionRegistry) {
144+
this(null, extensionRegistry);
130145
}
131146

132147
ProtobufHttpMessageConverter(@Nullable ProtobufFormatSupport formatSupport,
133-
@Nullable ExtensionRegistryInitializer registryInitializer) {
148+
@Nullable ExtensionRegistry extensionRegistry) {
134149

135150
if (formatSupport != null) {
136151
this.protobufFormatSupport = formatSupport;
@@ -148,9 +163,7 @@ else if (ClassUtils.isPresent("com.google.protobuf.util.JsonFormat", getClass().
148163
setSupportedMediaTypes(Arrays.asList(this.protobufFormatSupport != null ?
149164
this.protobufFormatSupport.supportedMediaTypes() : new MediaType[] {PROTOBUF, TEXT_PLAIN}));
150165

151-
if (registryInitializer != null) {
152-
registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
153-
}
166+
this.extensionRegistry = (extensionRegistry == null ? ExtensionRegistry.newInstance() : extensionRegistry);
154167
}
155168

156169

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

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.http.converter.protobuf;
1818

19+
import com.google.protobuf.ExtensionRegistry;
1920
import com.google.protobuf.util.JsonFormat;
2021

2122
import org.springframework.lang.Nullable;
@@ -32,6 +33,7 @@
3233
* with 3.3 or higher recommended.
3334
*
3435
* @author Juergen Hoeller
36+
* @author Sebastien Deleuze
3537
* @since 5.0
3638
* @see JsonFormat#parser()
3739
* @see JsonFormat#printer()
@@ -44,7 +46,7 @@ public class ProtobufJsonFormatHttpMessageConverter extends ProtobufHttpMessageC
4446
* {@link JsonFormat.Parser} and {@link JsonFormat.Printer} configuration.
4547
*/
4648
public ProtobufJsonFormatHttpMessageConverter() {
47-
this(null, null, null);
49+
this(null, null, (ExtensionRegistry)null);
4850
}
4951

5052
/**
@@ -56,7 +58,22 @@ public ProtobufJsonFormatHttpMessageConverter() {
5658
public ProtobufJsonFormatHttpMessageConverter(
5759
@Nullable JsonFormat.Parser parser, @Nullable JsonFormat.Printer printer) {
5860

59-
this(parser, printer, null);
61+
this(parser, printer, (ExtensionRegistry)null);
62+
}
63+
64+
/**
65+
* Construct a new {@code ProtobufJsonFormatHttpMessageConverter} with the given
66+
* {@link JsonFormat.Parser} and {@link JsonFormat.Printer} configuration, also
67+
* accepting a registry that specifies protocol message extensions.
68+
* @param parser the JSON parser configuration
69+
* @param printer the JSON printer configuration
70+
* @param extensionRegistry the registry to populate
71+
* @since 5.1
72+
*/
73+
public ProtobufJsonFormatHttpMessageConverter(@Nullable JsonFormat.Parser parser,
74+
@Nullable JsonFormat.Printer printer, @Nullable ExtensionRegistry extensionRegistry) {
75+
76+
super(new ProtobufJavaUtilSupport(parser, printer), extensionRegistry);
6077
}
6178

6279
/**
@@ -66,11 +83,17 @@ public ProtobufJsonFormatHttpMessageConverter(
6683
* @param parser the JSON parser configuration
6784
* @param printer the JSON printer configuration
6885
* @param registryInitializer an initializer for message extensions
86+
* @deprecated as of Spring Framework 5.1, use
87+
* {@link #ProtobufJsonFormatHttpMessageConverter(JsonFormat.Parser, JsonFormat.Printer, ExtensionRegistry)} instead
6988
*/
89+
@Deprecated
7090
public ProtobufJsonFormatHttpMessageConverter(@Nullable JsonFormat.Parser parser,
7191
@Nullable JsonFormat.Printer printer, @Nullable ExtensionRegistryInitializer registryInitializer) {
7292

73-
super(new ProtobufJavaUtilSupport(parser, printer), registryInitializer);
93+
super(new ProtobufJavaUtilSupport(parser, printer), null);
94+
if (registryInitializer != null) {
95+
registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
96+
}
7497
}
7598

7699
}

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

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.IOException;
2020
import java.nio.charset.Charset;
2121

22+
import com.google.protobuf.ExtensionRegistry;
2223
import com.google.protobuf.Message;
2324
import com.google.protobuf.util.JsonFormat;
2425
import org.junit.Before;
@@ -39,11 +40,15 @@
3940
* @author Alex Antonov
4041
* @author Juergen Hoeller
4142
* @author Andreas Ahlenstorf
43+
* @author Sebastien Deleuze
4244
*/
45+
@SuppressWarnings("deprecation")
4346
public class ProtobufHttpMessageConverterTests {
4447

4548
private ProtobufHttpMessageConverter converter;
4649

50+
private ExtensionRegistry extensionRegistry;
51+
4752
private ExtensionRegistryInitializer registryInitializer;
4853

4954
private Msg testMsg;
@@ -52,6 +57,7 @@ public class ProtobufHttpMessageConverterTests {
5257
@Before
5358
public void setup() {
5459
this.registryInitializer = mock(ExtensionRegistryInitializer.class);
60+
this.extensionRegistry = mock(ExtensionRegistry.class);
5561
this.converter = new ProtobufHttpMessageConverter(this.registryInitializer);
5662
this.testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
5763
}
@@ -62,9 +68,16 @@ public void extensionRegistryInitialized() {
6268
verify(this.registryInitializer, times(1)).initializeExtensionRegistry(any());
6369
}
6470

71+
@Test
72+
public void extensionRegistryInitializerNull() {
73+
ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter((ExtensionRegistryInitializer)null);
74+
assertNotNull(converter.extensionRegistry);
75+
}
76+
6577
@Test
6678
public void extensionRegistryNull() {
67-
new ProtobufHttpMessageConverter(null);
79+
ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter((ExtensionRegistry)null);
80+
assertNotNull(converter.extensionRegistry);
6881
}
6982

7083
@Test
@@ -128,7 +141,7 @@ public void writeProtobuf() throws IOException {
128141
public void writeJsonWithGoogleProtobuf() throws IOException {
129142
this.converter = new ProtobufHttpMessageConverter(
130143
new ProtobufHttpMessageConverter.ProtobufJavaUtilSupport(null, null),
131-
this.registryInitializer);
144+
this.extensionRegistry);
132145
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
133146
MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
134147
this.converter.write(this.testMsg, contentType, outputMessage);
@@ -152,7 +165,7 @@ public void writeJsonWithGoogleProtobuf() throws IOException {
152165
public void writeJsonWithJavaFormat() throws IOException {
153166
this.converter = new ProtobufHttpMessageConverter(
154167
new ProtobufHttpMessageConverter.ProtobufJavaFormatSupport(),
155-
this.registryInitializer);
168+
this.extensionRegistry);
156169
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
157170
MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
158171
this.converter.write(this.testMsg, contentType, outputMessage);

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.io.IOException;
2020

21+
import com.google.protobuf.ExtensionRegistry;
2122
import com.google.protobuf.Message;
2223
import com.google.protobuf.util.JsonFormat;
2324
import org.junit.Before;
@@ -36,11 +37,15 @@
3637
* Test suite for {@link ProtobufJsonFormatHttpMessageConverter}.
3738
*
3839
* @author Juergen Hoeller
40+
* @author Sebastien Deleuze
3941
*/
42+
@SuppressWarnings("deprecation")
4043
public class ProtobufJsonFormatHttpMessageConverterTests {
4144

4245
private ProtobufHttpMessageConverter converter;
4346

47+
private ExtensionRegistry extensionRegistry;
48+
4449
private ExtensionRegistryInitializer registryInitializer;
4550

4651
private Msg testMsg;
@@ -49,6 +54,7 @@ public class ProtobufJsonFormatHttpMessageConverterTests {
4954
@Before
5055
public void setup() {
5156
this.registryInitializer = mock(ExtensionRegistryInitializer.class);
57+
this.extensionRegistry = mock(ExtensionRegistry.class);
5258
this.converter = new ProtobufJsonFormatHttpMessageConverter(
5359
JsonFormat.parser(), JsonFormat.printer(), this.registryInitializer);
5460
this.testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
@@ -61,8 +67,15 @@ public void extensionRegistryInitialized() {
6167
}
6268

6369
@Test
64-
public void extensionRegistryNull() {
65-
new ProtobufHttpMessageConverter(null);
70+
public void extensionRegistryInitializerNull() {
71+
ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter((ExtensionRegistryInitializer)null);
72+
assertNotNull(converter);
73+
}
74+
75+
@Test
76+
public void extensionRegistryInitializer() {
77+
ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter((ExtensionRegistry)null);
78+
assertNotNull(converter);
6679
}
6780

6881
@Test

0 commit comments

Comments
 (0)