|
| 1 | +package com.fasterxml.jackson.databind.mixins; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import com.fasterxml.jackson.databind.BaseMapTest; |
| 5 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 6 | + |
| 7 | +public class MixinsCircularTest extends BaseMapTest { |
| 8 | + |
| 9 | + static class First { |
| 10 | + @JsonProperty("first-mixin") |
| 11 | + public String value; |
| 12 | + } |
| 13 | + |
| 14 | + static class Second { |
| 15 | + @JsonProperty("second-mixin") |
| 16 | + public String value; |
| 17 | + |
| 18 | + } |
| 19 | + |
| 20 | + static class Third { |
| 21 | + @JsonProperty("third-mixin") |
| 22 | + public String value; |
| 23 | + } |
| 24 | + |
| 25 | + /* |
| 26 | + /********************************************************************** |
| 27 | + /* Test methods |
| 28 | + /********************************************************************** |
| 29 | + */ |
| 30 | + |
| 31 | + protected final ObjectMapper MAPPER = jsonMapperBuilder().build(); |
| 32 | + |
| 33 | + public void testPojoMixinDeserialization() throws Exception { |
| 34 | + ObjectMapper mxMapper = MAPPER |
| 35 | + .addMixIn(First.class, Second.class) |
| 36 | + .addMixIn(Second.class, Third.class) |
| 37 | + .addMixIn(Third.class, First.class); |
| 38 | + |
| 39 | + // first deserialized from second |
| 40 | + First first = mxMapper.readValue(a2q("{'second-mixin':'second-mixin'}"), First.class); |
| 41 | + assertEquals("second-mixin", first.value); |
| 42 | + |
| 43 | + // second deserialized from third |
| 44 | + Second second = mxMapper.readValue(a2q("{'third-mixin':'third-mixin'}"), Second.class); |
| 45 | + assertEquals("third-mixin", second.value); |
| 46 | + |
| 47 | + // third deserialized from first |
| 48 | + Third third = mxMapper.readValue(a2q("{'first-mixin':'first-mixin'}"), Third.class); |
| 49 | + assertEquals("first-mixin", third.value); |
| 50 | + } |
| 51 | + |
| 52 | + public void testPojoMixinSerialization() throws Exception { |
| 53 | + ObjectMapper mxMapper = MAPPER |
| 54 | + .addMixIn(First.class, Second.class) |
| 55 | + .addMixIn(Second.class, Third.class) |
| 56 | + .addMixIn(Third.class, First.class); |
| 57 | + |
| 58 | + // first serialized as second |
| 59 | + First firstBean = new First(); |
| 60 | + firstBean.value = "value"; |
| 61 | + assertEquals( |
| 62 | + a2q("{'second-mixin':'value'}"), |
| 63 | + mxMapper.writeValueAsString(firstBean)); |
| 64 | + |
| 65 | + // second serialized as third |
| 66 | + Second secondBean = new Second(); |
| 67 | + secondBean.value = "value"; |
| 68 | + assertEquals( |
| 69 | + a2q("{'third-mixin':'value'}"), |
| 70 | + mxMapper.writeValueAsString(secondBean)); |
| 71 | + |
| 72 | + // third serialized as first |
| 73 | + Third thirdBean = new Third(); |
| 74 | + thirdBean.value = "value"; |
| 75 | + assertEquals( |
| 76 | + a2q("{'first-mixin':'value'}"), |
| 77 | + mxMapper.writeValueAsString(thirdBean)); |
| 78 | + } |
| 79 | +} |
0 commit comments