提交 36bbbab0 编写于 作者: S Sebastien Deleuze

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
上级 dd4468a7
......@@ -25,10 +25,13 @@ import com.google.protobuf.ExtensionRegistry;
* <p>This interface provides a facility to populate the {@code ExtensionRegistry}.
*
* @author Alex Antonov
* @author Sebastien Deleuze
* @since 4.1
* @see <a href="https://developers.google.com/protocol-buffers/docs/reference/java/com/google/protobuf/ExtensionRegistry">
* com.google.protobuf.ExtensionRegistry</a>
* @deprecated as of Spring Framework 5.1, use {@link ExtensionRegistry} based contructors instead
*/
@Deprecated
public interface ExtensionRegistryInitializer {
/**
......
......@@ -77,6 +77,7 @@ import static org.springframework.http.MediaType.TEXT_PLAIN;
* @author Alex Antonov
* @author Brian Clozel
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @since 4.1
* @see FormatFactory
* @see JsonFormat
......@@ -107,7 +108,7 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
private static final Map<Class<?>, Method> methodCache = new ConcurrentReferenceHashMap<>();
private final ExtensionRegistry extensionRegistry = ExtensionRegistry.newInstance();
final ExtensionRegistry extensionRegistry;
@Nullable
private final ProtobufFormatSupport protobufFormatSupport;
......@@ -117,20 +118,34 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
* Construct a new {@code ProtobufHttpMessageConverter}.
*/
public ProtobufHttpMessageConverter() {
this(null);
this(null, null);
}
/**
* Construct a new {@code ProtobufHttpMessageConverter} with an
* initializer that allows the registration of message extensions.
* @param registryInitializer an initializer for message extensions
* @deprecated as of Spring Framework 5.1, use {@link #ProtobufHttpMessageConverter(ExtensionRegistry)} instead
*/
@Deprecated
public ProtobufHttpMessageConverter(@Nullable ExtensionRegistryInitializer registryInitializer) {
this(null, registryInitializer);
this(null, null);
if (registryInitializer != null) {
registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
}
}
/**
* Construct a new {@code ProtobufHttpMessageConverter} with a registry that specifies
* protocol message extensions.
* @param extensionRegistry the registry to populate
*/
public ProtobufHttpMessageConverter(ExtensionRegistry extensionRegistry) {
this(null, extensionRegistry);
}
ProtobufHttpMessageConverter(@Nullable ProtobufFormatSupport formatSupport,
@Nullable ExtensionRegistryInitializer registryInitializer) {
@Nullable ExtensionRegistry extensionRegistry) {
if (formatSupport != null) {
this.protobufFormatSupport = formatSupport;
......@@ -148,9 +163,7 @@ public class ProtobufHttpMessageConverter extends AbstractHttpMessageConverter<M
setSupportedMediaTypes(Arrays.asList(this.protobufFormatSupport != null ?
this.protobufFormatSupport.supportedMediaTypes() : new MediaType[] {PROTOBUF, TEXT_PLAIN}));
if (registryInitializer != null) {
registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
}
this.extensionRegistry = (extensionRegistry == null ? ExtensionRegistry.newInstance() : extensionRegistry);
}
......
......@@ -16,6 +16,7 @@
package org.springframework.http.converter.protobuf;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.util.JsonFormat;
import org.springframework.lang.Nullable;
......@@ -32,6 +33,7 @@ import org.springframework.lang.Nullable;
* with 3.3 or higher recommended.
*
* @author Juergen Hoeller
* @author Sebastien Deleuze
* @since 5.0
* @see JsonFormat#parser()
* @see JsonFormat#printer()
......@@ -44,7 +46,7 @@ public class ProtobufJsonFormatHttpMessageConverter extends ProtobufHttpMessageC
* {@link JsonFormat.Parser} and {@link JsonFormat.Printer} configuration.
*/
public ProtobufJsonFormatHttpMessageConverter() {
this(null, null, null);
this(null, null, (ExtensionRegistry)null);
}
/**
......@@ -56,7 +58,22 @@ public class ProtobufJsonFormatHttpMessageConverter extends ProtobufHttpMessageC
public ProtobufJsonFormatHttpMessageConverter(
@Nullable JsonFormat.Parser parser, @Nullable JsonFormat.Printer printer) {
this(parser, printer, null);
this(parser, printer, (ExtensionRegistry)null);
}
/**
* Construct a new {@code ProtobufJsonFormatHttpMessageConverter} with the given
* {@link JsonFormat.Parser} and {@link JsonFormat.Printer} configuration, also
* accepting a registry that specifies protocol message extensions.
* @param parser the JSON parser configuration
* @param printer the JSON printer configuration
* @param extensionRegistry the registry to populate
* @since 5.1
*/
public ProtobufJsonFormatHttpMessageConverter(@Nullable JsonFormat.Parser parser,
@Nullable JsonFormat.Printer printer, @Nullable ExtensionRegistry extensionRegistry) {
super(new ProtobufJavaUtilSupport(parser, printer), extensionRegistry);
}
/**
......@@ -66,11 +83,17 @@ public class ProtobufJsonFormatHttpMessageConverter extends ProtobufHttpMessageC
* @param parser the JSON parser configuration
* @param printer the JSON printer configuration
* @param registryInitializer an initializer for message extensions
* @deprecated as of Spring Framework 5.1, use
* {@link #ProtobufJsonFormatHttpMessageConverter(JsonFormat.Parser, JsonFormat.Printer, ExtensionRegistry)} instead
*/
@Deprecated
public ProtobufJsonFormatHttpMessageConverter(@Nullable JsonFormat.Parser parser,
@Nullable JsonFormat.Printer printer, @Nullable ExtensionRegistryInitializer registryInitializer) {
super(new ProtobufJavaUtilSupport(parser, printer), registryInitializer);
super(new ProtobufJavaUtilSupport(parser, printer), null);
if (registryInitializer != null) {
registryInitializer.initializeExtensionRegistry(this.extensionRegistry);
}
}
}
......@@ -19,6 +19,7 @@ package org.springframework.http.converter.protobuf;
import java.io.IOException;
import java.nio.charset.Charset;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;
import org.junit.Before;
......@@ -39,11 +40,15 @@ import static org.mockito.Mockito.*;
* @author Alex Antonov
* @author Juergen Hoeller
* @author Andreas Ahlenstorf
* @author Sebastien Deleuze
*/
@SuppressWarnings("deprecation")
public class ProtobufHttpMessageConverterTests {
private ProtobufHttpMessageConverter converter;
private ExtensionRegistry extensionRegistry;
private ExtensionRegistryInitializer registryInitializer;
private Msg testMsg;
......@@ -52,6 +57,7 @@ public class ProtobufHttpMessageConverterTests {
@Before
public void setup() {
this.registryInitializer = mock(ExtensionRegistryInitializer.class);
this.extensionRegistry = mock(ExtensionRegistry.class);
this.converter = new ProtobufHttpMessageConverter(this.registryInitializer);
this.testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
}
......@@ -62,9 +68,16 @@ public class ProtobufHttpMessageConverterTests {
verify(this.registryInitializer, times(1)).initializeExtensionRegistry(any());
}
@Test
public void extensionRegistryInitializerNull() {
ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter((ExtensionRegistryInitializer)null);
assertNotNull(converter.extensionRegistry);
}
@Test
public void extensionRegistryNull() {
new ProtobufHttpMessageConverter(null);
ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter((ExtensionRegistry)null);
assertNotNull(converter.extensionRegistry);
}
@Test
......@@ -128,7 +141,7 @@ public class ProtobufHttpMessageConverterTests {
public void writeJsonWithGoogleProtobuf() throws IOException {
this.converter = new ProtobufHttpMessageConverter(
new ProtobufHttpMessageConverter.ProtobufJavaUtilSupport(null, null),
this.registryInitializer);
this.extensionRegistry);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
this.converter.write(this.testMsg, contentType, outputMessage);
......@@ -152,7 +165,7 @@ public class ProtobufHttpMessageConverterTests {
public void writeJsonWithJavaFormat() throws IOException {
this.converter = new ProtobufHttpMessageConverter(
new ProtobufHttpMessageConverter.ProtobufJavaFormatSupport(),
this.registryInitializer);
this.extensionRegistry);
MockHttpOutputMessage outputMessage = new MockHttpOutputMessage();
MediaType contentType = MediaType.APPLICATION_JSON_UTF8;
this.converter.write(this.testMsg, contentType, outputMessage);
......
......@@ -18,6 +18,7 @@ package org.springframework.http.converter.protobuf;
import java.io.IOException;
import com.google.protobuf.ExtensionRegistry;
import com.google.protobuf.Message;
import com.google.protobuf.util.JsonFormat;
import org.junit.Before;
......@@ -36,11 +37,15 @@ import static org.mockito.Mockito.*;
* Test suite for {@link ProtobufJsonFormatHttpMessageConverter}.
*
* @author Juergen Hoeller
* @author Sebastien Deleuze
*/
@SuppressWarnings("deprecation")
public class ProtobufJsonFormatHttpMessageConverterTests {
private ProtobufHttpMessageConverter converter;
private ExtensionRegistry extensionRegistry;
private ExtensionRegistryInitializer registryInitializer;
private Msg testMsg;
......@@ -49,6 +54,7 @@ public class ProtobufJsonFormatHttpMessageConverterTests {
@Before
public void setup() {
this.registryInitializer = mock(ExtensionRegistryInitializer.class);
this.extensionRegistry = mock(ExtensionRegistry.class);
this.converter = new ProtobufJsonFormatHttpMessageConverter(
JsonFormat.parser(), JsonFormat.printer(), this.registryInitializer);
this.testMsg = Msg.newBuilder().setFoo("Foo").setBlah(SecondMsg.newBuilder().setBlah(123).build()).build();
......@@ -61,8 +67,15 @@ public class ProtobufJsonFormatHttpMessageConverterTests {
}
@Test
public void extensionRegistryNull() {
new ProtobufHttpMessageConverter(null);
public void extensionRegistryInitializerNull() {
ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter((ExtensionRegistryInitializer)null);
assertNotNull(converter);
}
@Test
public void extensionRegistryInitializer() {
ProtobufHttpMessageConverter converter = new ProtobufHttpMessageConverter((ExtensionRegistry)null);
assertNotNull(converter);
}
@Test
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册