DATAREST-872 - PersistentEntityJackson2Module now serializes JsonTypeInfo correctly.
NestedEntitySerializer now overrides serializeWithType(…) so that @JsonTypeInfo is rendered properly. Related pull request: #221.
This commit is contained in:
committed by
Oliver Gierke
parent
216e267fce
commit
c36267a447
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.webmvc.jpa;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
/**
|
||||
* @author Alex Leigh
|
||||
* @see DATAREST-872
|
||||
*/
|
||||
@Entity
|
||||
@DiscriminatorValue("D")
|
||||
public class Dinner extends Meal {
|
||||
|
||||
public static final String TYPE = "dinner";
|
||||
|
||||
private String dinnerCode;
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
public String getDinnerCode() {
|
||||
return dinnerCode;
|
||||
}
|
||||
|
||||
public void setDinnerCode(String dinnerCode) {
|
||||
this.dinnerCode = dinnerCode;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.webmvc.jpa;
|
||||
|
||||
import javax.persistence.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Alex Leigh
|
||||
* @see DATAREST-872
|
||||
*/
|
||||
@Entity
|
||||
public class Guest {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
@OneToOne(cascade = CascadeType.ALL)
|
||||
private Room room;
|
||||
|
||||
@OneToMany(cascade = CascadeType.ALL, orphanRemoval = true)
|
||||
private List<Meal> meals = new ArrayList<Meal>();
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public Room getRoom() {
|
||||
return room;
|
||||
}
|
||||
|
||||
public void setRoom(Room room) {
|
||||
this.room = room;
|
||||
}
|
||||
|
||||
public List<Meal> getMeals() {
|
||||
return meals;
|
||||
}
|
||||
|
||||
public void setMeals(List<Meal> meals) {
|
||||
this.meals = meals;
|
||||
}
|
||||
|
||||
public void addMeal(Meal meal) {
|
||||
meals.add(meal);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* Copyright 2015-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.webmvc.jpa;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
|
||||
/**
|
||||
* @author Alex Leigh
|
||||
* @see DATAREST-872
|
||||
*/
|
||||
public interface GuestRepository extends JpaRepository<Guest, Long> {}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.webmvc.jpa;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
|
||||
/**
|
||||
* @author Alex Leigh
|
||||
* @see DATAREST-872
|
||||
*/
|
||||
@Entity
|
||||
@Inheritance
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = Dinner.class, name = Dinner.TYPE)
|
||||
})
|
||||
public abstract class Meal {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public abstract String getType();
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.webmvc.jpa;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonSubTypes;
|
||||
import com.fasterxml.jackson.annotation.JsonTypeInfo;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Inheritance;
|
||||
|
||||
/**
|
||||
* @author Alex Leigh
|
||||
* @see DATAREST-872
|
||||
*/
|
||||
@Entity
|
||||
@Inheritance
|
||||
@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.EXISTING_PROPERTY, property = "type")
|
||||
@JsonSubTypes({
|
||||
@JsonSubTypes.Type(value = Suite.class, name = Suite.TYPE)
|
||||
})
|
||||
public abstract class Room {
|
||||
|
||||
@Id
|
||||
@GeneratedValue
|
||||
private Long id;
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public abstract String getType();
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.springframework.data.rest.webmvc.jpa;
|
||||
|
||||
import javax.persistence.DiscriminatorValue;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
/**
|
||||
* @author Alex Leigh
|
||||
* @see DATAREST-872
|
||||
*/
|
||||
@Entity
|
||||
@DiscriminatorValue("S")
|
||||
public class Suite extends Room {
|
||||
|
||||
public static final String TYPE = "suite";
|
||||
|
||||
private String suiteCode;
|
||||
|
||||
@Override
|
||||
public String getType() {
|
||||
return TYPE;
|
||||
}
|
||||
|
||||
public String getSuiteCode() {
|
||||
return suiteCode;
|
||||
}
|
||||
|
||||
public void setSuiteCode(String suiteCode) {
|
||||
this.suiteCode = suiteCode;
|
||||
}
|
||||
}
|
||||
@@ -36,15 +36,7 @@ import org.springframework.data.projection.ProjectionFactory;
|
||||
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
|
||||
import org.springframework.data.repository.support.Repositories;
|
||||
import org.springframework.data.rest.webmvc.PersistentEntityResource;
|
||||
import org.springframework.data.rest.webmvc.jpa.CreditCard;
|
||||
import org.springframework.data.rest.webmvc.jpa.JpaRepositoryConfig;
|
||||
import org.springframework.data.rest.webmvc.jpa.LineItem;
|
||||
import org.springframework.data.rest.webmvc.jpa.Order;
|
||||
import org.springframework.data.rest.webmvc.jpa.OrderRepository;
|
||||
import org.springframework.data.rest.webmvc.jpa.Person;
|
||||
import org.springframework.data.rest.webmvc.jpa.PersonRepository;
|
||||
import org.springframework.data.rest.webmvc.jpa.PersonSummary;
|
||||
import org.springframework.data.rest.webmvc.jpa.UserExcerpt;
|
||||
import org.springframework.data.rest.webmvc.jpa.*;
|
||||
import org.springframework.data.rest.webmvc.util.TestUtils;
|
||||
import org.springframework.hateoas.Link;
|
||||
import org.springframework.hateoas.LinkDiscoverer;
|
||||
@@ -301,4 +293,31 @@ public class PersistentEntitySerializationTests {
|
||||
|
||||
assertThat(JsonPath.read(mapper.writeValueAsString(creditCard), "$.ccn"), is("1234123412341234"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see DATAREST-872
|
||||
*/
|
||||
@Test
|
||||
public void serializesInheritance() throws Exception {
|
||||
|
||||
Suite suite = new Suite();
|
||||
suite.setSuiteCode("Suite 42");
|
||||
|
||||
Dinner dinner = new Dinner();
|
||||
dinner.setDinnerCode("STD_DINNER");
|
||||
|
||||
Guest guest = new Guest();
|
||||
guest.setRoom(suite);
|
||||
guest.addMeal(dinner);
|
||||
|
||||
PersistentEntityResource resource = PersistentEntityResource.
|
||||
build(guest, repositories.getPersistentEntity(Guest.class)).
|
||||
withLink(new Link("/guests/1")).
|
||||
build();
|
||||
|
||||
String result = mapper.writeValueAsString(resource);
|
||||
|
||||
assertThat(JsonPath.read(result, "$.room.type"), equalTo("suite"));
|
||||
assertThat(JsonPath.read(result, "$.meals[0].type"), equalTo("dinner"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,6 +79,7 @@ import com.fasterxml.jackson.databind.deser.std.StdDeserializer;
|
||||
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;
|
||||
import com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition;
|
||||
import com.fasterxml.jackson.databind.jsontype.TypeDeserializer;
|
||||
import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
|
||||
import com.fasterxml.jackson.databind.module.SimpleModule;
|
||||
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
|
||||
import com.fasterxml.jackson.databind.ser.BeanSerializerBuilder;
|
||||
@@ -369,6 +370,13 @@ public class PersistentEntityJackson2Module extends SimpleModule {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void serializeWithType(Object value, JsonGenerator gen, SerializerProvider provider,
|
||||
TypeSerializer typeSerializer) throws IOException {
|
||||
|
||||
serialize(value, gen, provider);
|
||||
}
|
||||
|
||||
private Resource<Object> toResource(Object value) {
|
||||
|
||||
PersistentEntity<?, ?> entity = entities.getPersistentEntity(value.getClass());
|
||||
|
||||
Reference in New Issue
Block a user