Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.fasterxml.jackson.databind.SerializationFeature;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -47,9 +46,9 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
import static tools.jackson.databind.cfg.DateTimeFeature.WRITE_DATES_AS_TIMESTAMPS;

@SpringBootTest(properties = "spring.jackson.serialization.WRITE_DATES_AS_TIMESTAMPS:true",
webEnvironment = RANDOM_PORT)
@SpringBootTest(properties = "spring.jackson.datetime.write-dates-as-timestamps=true", webEnvironment = RANDOM_PORT)
@DirtiesContext
public class BusJacksonIntegrationTests {

Expand All @@ -68,9 +67,8 @@ public void testCustomEventSerializes() {
assertThat(this.converter.isMapperCreated()).isFalse();

// set by configuration
assertThat(this.converter.getMapper()
.getSerializationConfig()
.isEnabled(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS)).isTrue();
assertThat(this.converter.getMapper().build().serializationConfig().isEnabled(WRITE_DATES_AS_TIMESTAMPS))
.isTrue();

Map map = this.rest.getForObject("http://localhost:" + this.port + "/date", Map.class);
assertThat(map).containsOnlyKeys("date");
Expand Down
2 changes: 1 addition & 1 deletion spring-cloud-bus/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<artifactId>spring-integration-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<groupId>tools.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-cbor</artifactId>
<optional>true</optional>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
import java.util.HashMap;
import java.util.Map;

import org.springframework.boot.EnvironmentPostProcessor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.env.EnvironmentPostProcessor;
import org.springframework.cloud.commons.util.IdUtils;
import org.springframework.cloud.function.context.FunctionProperties;
import org.springframework.core.env.ConfigurableEnvironment;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ public abstract class RemoteApplicationEvent extends ApplicationEvent {

protected static final PathDestinationFactory DEFAULT_DESTINATION_FACTORY = new PathDestinationFactory();

private final String originService;
private String originService;

private final String destinationService;
private String destinationService;

private final String id;
private String id;

protected RemoteApplicationEvent() {
// for serialization libs like jackson
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@
import java.util.List;
import java.util.Set;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.exc.InvalidTypeIdException;
import com.fasterxml.jackson.dataformat.cbor.CBORFactory;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.exc.InvalidTypeIdException;
import tools.jackson.databind.json.JsonMapper;
import tools.jackson.dataformat.cbor.CBORFactory;
import tools.jackson.dataformat.cbor.CBORMapper;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -76,8 +79,7 @@ protected static class CborConfiguration {

@Bean
public AbstractMessageConverter busCborConverter() {
return new BusJacksonMessageConverter(new MimeType("application", "cbor"),
new ObjectMapper(new CBORFactory()));
return new BusJacksonMessageConverter(new MimeType("application", "cbor"), CBORMapper.builder().build());
}

}
Expand All @@ -90,7 +92,7 @@ class BusJacksonMessageConverter extends AbstractMessageConverter implements Ini

private static final String DEFAULT_PACKAGE = ClassUtils.getPackageName(RemoteApplicationEvent.class);

private final ObjectMapper mapper;
private final MapperBuilder mapperBuilder;

private final boolean mapperCreated;

Expand All @@ -110,11 +112,11 @@ private BusJacksonMessageConverter() {
super(mimeType);

if (objectMapper != null) {
this.mapper = objectMapper;
this.mapperBuilder = objectMapper.rebuild();
this.mapperCreated = false;
}
else {
this.mapper = new ObjectMapper();
this.mapperBuilder = JsonMapper.builder();
this.mapperCreated = true;
}
}
Expand All @@ -123,8 +125,8 @@ private BusJacksonMessageConverter() {
return this.mapperCreated;
}

/* for testing */ ObjectMapper getMapper() {
return this.mapper;
/* for testing */ MapperBuilder getMapper() {
return this.mapperBuilder;
}

public void setPackagesToScan(String[] packagesToScan) {
Expand Down Expand Up @@ -168,21 +170,22 @@ protected boolean supports(Class<?> aClass) {

@Override
public Object convertFromInternal(Message<?> message, Class<?> targetClass, Object conversionHint) {
ObjectMapper mapper = this.mapperBuilder.build();
Object result = null;
try {
Object payload = message.getPayload();

if (payload instanceof byte[]) {
try {
result = this.mapper.readValue((byte[]) payload, targetClass);
result = mapper.readValue((byte[]) payload, targetClass);
}
catch (InvalidTypeIdException e) {
return new UnknownRemoteApplicationEvent(new Object(), e.getTypeId(), (byte[]) payload);
}
}
else if (payload instanceof String) {
try {
result = this.mapper.readValue((String) payload, targetClass);
result = mapper.readValue((String) payload, targetClass);
}
catch (InvalidTypeIdException e) {
return new UnknownRemoteApplicationEvent(new Object(), e.getTypeId(),
Expand All @@ -203,8 +206,8 @@ else if (payload instanceof RemoteApplicationEvent) {
}

@Override
public void afterPropertiesSet() throws Exception {
this.mapper.registerModule(new SubtypeModule(findSubTypes()));
public void afterPropertiesSet() {
this.mapperBuilder.subtypeResolver().registerSubtypes(findSubTypes());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

package org.springframework.cloud.bus.jackson;

import com.fasterxml.jackson.databind.module.SimpleModule;
import tools.jackson.databind.module.SimpleModule;

/**
* @author Spencer Gibb
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Environment Post Processor
org.springframework.boot.env.EnvironmentPostProcessor=\
org.springframework.boot.EnvironmentPostProcessor=\
org.springframework.cloud.bus.BusEnvironmentPostProcessor
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
import java.util.LinkedHashSet;
import java.util.List;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import org.junit.Test;
import test.foo.bar.FooBarTestRemoteApplicationEvent;
import tools.jackson.databind.cfg.MapperBuilder;
import tools.jackson.databind.jsontype.NamedType;

import org.springframework.boot.Banner;
import org.springframework.boot.WebApplicationType;
Expand Down Expand Up @@ -96,11 +96,11 @@ private ConfigurableApplicationContext createTestContext(Class<?> configuration)

private void assertConverterBeanAfterPropertiesSet(final String[] expectedPackageToScan,
final Class<?>... expectedRegisterdClasses) {
final ObjectMapper mapper = (ObjectMapper) ReflectionTestUtils.getField(this.converter, "mapper");
final MapperBuilder mapper = this.converter.getMapper();

@SuppressWarnings("unchecked")
final LinkedHashSet<NamedType> registeredSubtypes = (LinkedHashSet<NamedType>) ReflectionTestUtils
.getField(mapper.getSubtypeResolver(), "_registeredSubtypes");
.getField(mapper.subtypeResolver(), "_registeredSubtypes");

final List<Class<?>> expectedRegisterdClassesAsList = new ArrayList<>(Arrays.asList(expectedRegisterdClasses));
addStandardSpringCloudEventBusEvents(expectedRegisterdClassesAsList);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@

import java.util.Collections;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Test;
import tools.jackson.databind.MapperFeature;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import org.springframework.cloud.bus.event.EnvironmentChangeRemoteApplicationEvent;
import org.springframework.cloud.bus.event.RefreshRemoteApplicationEvent;
Expand All @@ -33,12 +35,15 @@
*/
public class SerializationTests {

ObjectMapper mapper = new ObjectMapper();
ObjectMapper mapper;

@Test
public void vanillaDeserialize() throws Exception {
this.mapper.registerModule(
new SubtypeModule(RefreshRemoteApplicationEvent.class, EnvironmentChangeRemoteApplicationEvent.class));
this.mapper = JsonMapper.builder()
.addModule(new SubtypeModule(RefreshRemoteApplicationEvent.class,
EnvironmentChangeRemoteApplicationEvent.class))
.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, true)
.build();
EnvironmentChangeRemoteApplicationEvent source = new EnvironmentChangeRemoteApplicationEvent(this, "foo", "bar",
Collections.<String, String>emptyMap());
String value = this.mapper.writeValueAsString(source);
Expand All @@ -50,8 +55,10 @@ public void vanillaDeserialize() throws Exception {

@Test
public void deserializeOldValueWithNoId() throws Exception {
this.mapper.registerModule(
new SubtypeModule(RefreshRemoteApplicationEvent.class, EnvironmentChangeRemoteApplicationEvent.class));
this.mapper = JsonMapper.builder()
.addModule(new SubtypeModule(RefreshRemoteApplicationEvent.class,
EnvironmentChangeRemoteApplicationEvent.class))
.build();
EnvironmentChangeRemoteApplicationEvent source = new EnvironmentChangeRemoteApplicationEvent(this, "foo", "bar",
Collections.<String, String>emptyMap());
String value = this.mapper.writeValueAsString(source);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package org.springframework.cloud.bus.jackson;

import com.fasterxml.jackson.annotation.JsonTypeName;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategies;
import org.junit.Test;
import tools.jackson.databind.ObjectMapper;
import tools.jackson.databind.json.JsonMapper;

import org.springframework.cloud.bus.event.AckRemoteApplicationEvent;
import org.springframework.cloud.bus.event.RemoteApplicationEvent;
Expand All @@ -38,8 +38,7 @@ public class SubtypeModuleTests {

@Test
public void testDeserializeSubclass() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new SubtypeModule(MyRemoteApplicationEvent.class));
ObjectMapper mapper = JsonMapper.builder().addModule(new SubtypeModule(MyRemoteApplicationEvent.class)).build();

RemoteApplicationEvent event = mapper.readValue(
"{\"type\":\"my\", \"destinationService\":\"myservice\", \"originService\":\"myorigin\"}",
Expand All @@ -60,10 +59,10 @@ public void testDeserializeWhenTypeIsKnown() throws Exception {

@Test
public void testDeserializeCustomizedObjectMapper() throws Exception {
ObjectMapper mapper = new ObjectMapper();
mapper.setPropertyNamingStrategy(new PropertyNamingStrategies.SnakeCaseStrategy());
JsonMapper.Builder mapper = JsonMapper.builder()
.propertyNamingStrategy(tools.jackson.databind.PropertyNamingStrategies.SNAKE_CASE);

BusJacksonMessageConverter converter = new BusJacksonMessageConverter(mapper);
BusJacksonMessageConverter converter = new BusJacksonMessageConverter(mapper.build());
converter.afterPropertiesSet();
Object event = converter.fromMessage(
MessageBuilder.withPayload("{\"type\":\"TestRemoteApplicationEvent\", \"origin_service\":\"myorigin\"}")
Expand Down