Skip to content

Commit

Permalink
JAVA-4215 Fix issue in "Jackson Bidirectional Rels" article
Browse files Browse the repository at this point in the history
  • Loading branch information
mikr committed Feb 28, 2021
1 parent e8fe6ce commit 4b1f33b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.baeldung.jackson.bidirection;

import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fasterxml.jackson.annotation.JsonBackReference;

public class ItemWithRef {
public int id;
public String itemName;

@JsonManagedReference
@JsonBackReference
public UserWithRef owner;

public ItemWithRef() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@
import java.util.ArrayList;
import java.util.List;

import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonManagedReference;

public class UserWithRef {
public int id;
public String name;

@JsonBackReference
@JsonManagedReference
public List<ItemWithRef> userItems;

public UserWithRef() {
Expand All @@ -19,7 +19,7 @@ public UserWithRef() {
public UserWithRef(final int id, final String name) {
this.id = id;
this.name = name;
userItems = new ArrayList<ItemWithRef>();
userItems = new ArrayList<>();
}

public void addItem(final ItemWithRef item) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package com.baeldung.jackson.bidirection;

import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThat;

Expand All @@ -16,7 +18,7 @@

public class JacksonBidirectionRelationUnitTest {

@Test(expected = JsonMappingException.class)
@Test (expected = JsonMappingException.class)
public void givenBidirectionRelation_whenSerializing_thenException() throws JsonProcessingException {
final User user = new User(1, "John");
final Item item = new Item(2, "book", user);
Expand All @@ -26,16 +28,39 @@ public void givenBidirectionRelation_whenSerializing_thenException() throws Json
}

@Test
public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotation_thenCorrect() throws JsonProcessingException {
public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotationWithSerialization_thenCorrect() throws JsonProcessingException {
final UserWithRef user = new UserWithRef(1, "John");
final ItemWithRef item = new ItemWithRef(2, "book", user);
user.addItem(item);

final String result = new ObjectMapper().writeValueAsString(item);
final String itemJson = new ObjectMapper().writeValueAsString(item);
final String userJson = new ObjectMapper().writeValueAsString(user);

assertThat(result, containsString("book"));
assertThat(result, containsString("John"));
assertThat(result, not(containsString("userItems")));
assertThat(itemJson, containsString("book"));
assertThat(itemJson, not(containsString("John")));

assertThat(userJson, containsString("John"));
assertThat(userJson, containsString("userItems"));
assertThat(userJson, containsString("book"));
}

@Test
public void givenBidirectionRelation_whenUsingJacksonReferenceAnnotationWithDeserialization_thenCorrect() throws JsonProcessingException {
final UserWithRef user = new UserWithRef(1, "John");
final ItemWithRef item = new ItemWithRef(2, "book", user);
user.addItem(item);

final String itemJson = new ObjectMapper().writeValueAsString(item);
final String userJson = new ObjectMapper().writeValueAsString(user);

final ItemWithRef itemRead = new ObjectMapper().readValue(itemJson, ItemWithRef.class);
final UserWithRef userRead = new ObjectMapper().readValue(userJson, UserWithRef.class);

assertThat(itemRead.itemName, is("book"));
assertThat(itemRead.owner, nullValue());

assertThat(userRead.name, is("John"));
assertThat(userRead.userItems.get(0).itemName, is("book"));
}

@Test
Expand Down

0 comments on commit 4b1f33b

Please sign in to comment.