|
| 1 | +package tools.jackson.dataformat.xml.ser; |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +import org.junit.jupiter.api.Test; |
| 6 | + |
| 7 | +import com.fasterxml.jackson.annotation.JsonApplyView; |
| 8 | +import com.fasterxml.jackson.annotation.JsonView; |
| 9 | + |
| 10 | +import tools.jackson.databind.JsonNode; |
| 11 | +import tools.jackson.dataformat.xml.XmlMapper; |
| 12 | +import tools.jackson.dataformat.xml.XmlTestUtil; |
| 13 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
| 14 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 15 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlRootElement; |
| 16 | + |
| 17 | +import static org.junit.jupiter.api.Assertions.*; |
| 18 | + |
| 19 | +/** |
| 20 | + * Tests for [dataformat-xml#873]: {@code @JsonApplyView} (i.e. the {@code _applyView} |
| 21 | + * mechanism) must be honored for Collection/array properties, both wrapped and unwrapped, |
| 22 | + * so that the forced View propagates to the collection's element beans -- same as |
| 23 | + * {@code BeanPropertyWriter.serializeAsProperty()} does for scalar properties. |
| 24 | + */ |
| 25 | +public class ListApplyView873Test extends XmlTestUtil |
| 26 | +{ |
| 27 | + static class Views { |
| 28 | + static class Public {} |
| 29 | + static class Internal extends Public {} |
| 30 | + } |
| 31 | + |
| 32 | + static class Item { |
| 33 | + @JsonView(Views.Public.class) |
| 34 | + public String pub = "A"; |
| 35 | + @JsonView(Views.Internal.class) |
| 36 | + public String internalOnly = "SECRET"; |
| 37 | + } |
| 38 | + |
| 39 | + // Wrapped list with a forced View (Public) |
| 40 | + @JacksonXmlRootElement(localName = "Bean") |
| 41 | + static class WrappedForcedBean { |
| 42 | + @JsonApplyView(Views.Public.class) |
| 43 | + @JacksonXmlElementWrapper(localName = "items") |
| 44 | + @JacksonXmlProperty(localName = "item") |
| 45 | + public List<Item> items = new ArrayList<>(Arrays.asList(new Item())); |
| 46 | + } |
| 47 | + |
| 48 | + // Wrapped list with View processing explicitly disabled (NONE) |
| 49 | + @JacksonXmlRootElement(localName = "Bean") |
| 50 | + static class WrappedNoneBean { |
| 51 | + // `@JsonView` so the property itself survives an active Public view; |
| 52 | + // `@JsonApplyView(NONE)` then disables View filtering for the elements. |
| 53 | + @JsonView(Views.Public.class) |
| 54 | + @JsonApplyView(JsonApplyView.NONE.class) |
| 55 | + @JacksonXmlElementWrapper(localName = "items") |
| 56 | + @JacksonXmlProperty(localName = "item") |
| 57 | + public List<Item> items = new ArrayList<>(Arrays.asList(new Item())); |
| 58 | + } |
| 59 | + |
| 60 | + // Unwrapped list with a forced View (Public) |
| 61 | + @JacksonXmlRootElement(localName = "Bean") |
| 62 | + static class UnwrappedForcedBean { |
| 63 | + @JsonApplyView(Views.Public.class) |
| 64 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 65 | + @JacksonXmlProperty(localName = "item") |
| 66 | + public List<Item> items = new ArrayList<>(Arrays.asList(new Item())); |
| 67 | + } |
| 68 | + |
| 69 | + private final XmlMapper MAPPER = new XmlMapper(); |
| 70 | + |
| 71 | + // Wrapped: @JsonApplyView(Public) forces Public view onto elements even with no |
| 72 | + // active view set -> 'pub' kept, Internal-only 'internalOnly' excluded. |
| 73 | + @Test |
| 74 | + public void testForcedViewWrapped() throws Exception |
| 75 | + { |
| 76 | + JsonNode item = itemNode(MAPPER.writeValueAsString(new WrappedForcedBean())); |
| 77 | + assertEquals("A", item.path("pub").asString(null)); |
| 78 | + assertTrue(item.path("internalOnly").isMissingNode(), |
| 79 | + "Internal-only property must be excluded by forced Public view: " + item); |
| 80 | + } |
| 81 | + |
| 82 | + // Wrapped: @JsonApplyView(NONE) disables view filtering for elements, so even with |
| 83 | + // an active Public view BOTH properties are written. |
| 84 | + @Test |
| 85 | + public void testNoneViewWrapped() throws Exception |
| 86 | + { |
| 87 | + String xml = MAPPER.writerWithView(Views.Public.class) |
| 88 | + .writeValueAsString(new WrappedNoneBean()); |
| 89 | + JsonNode item = itemNode(xml); |
| 90 | + assertEquals("A", item.path("pub").asString(null)); |
| 91 | + assertEquals("SECRET", item.path("internalOnly").asString(null), |
| 92 | + "@JsonApplyView(NONE) must disable view filtering for elements: " + item); |
| 93 | + } |
| 94 | + |
| 95 | + // Unwrapped list goes through a different writer path; verify it honors the forced |
| 96 | + // view too (regression guard, since the forced view is the whole point of #873). |
| 97 | + @Test |
| 98 | + public void testForcedViewUnwrapped() throws Exception |
| 99 | + { |
| 100 | + JsonNode item = MAPPER.readTree(MAPPER.writeValueAsString(new UnwrappedForcedBean())) |
| 101 | + .path("item"); |
| 102 | + assertEquals("A", item.path("pub").asString(null)); |
| 103 | + assertTrue(item.path("internalOnly").isMissingNode(), |
| 104 | + "Internal-only property must be excluded by forced Public view (unwrapped): " + item); |
| 105 | + } |
| 106 | + |
| 107 | + // For wrapped output: <Bean><items><item>...</item></items></Bean> |
| 108 | + private JsonNode itemNode(String xml) { |
| 109 | + return MAPPER.readTree(xml).path("items").path("item"); |
| 110 | + } |
| 111 | +} |
0 commit comments