|
| 1 | +package tools.jackson.dataformat.xml.deser; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | + |
| 5 | +import tools.jackson.dataformat.xml.*; |
| 6 | + |
| 7 | +import static org.junit.jupiter.api.Assertions.*; |
| 8 | + |
| 9 | +// [dataformat-xml#358]: XSI namespace attributes like schemaLocation |
| 10 | +// should be silently ignored when SKIP_UNKNOWN_XSI_ATTRIBUTES is enabled |
| 11 | +public class XsiSchemaLocationTest extends XmlTestUtil |
| 12 | +{ |
| 13 | + static class Dto { |
| 14 | + public String value; |
| 15 | + } |
| 16 | + |
| 17 | + static class DtoWithAttr { |
| 18 | + public String value; |
| 19 | + public int count; |
| 20 | + } |
| 21 | + |
| 22 | + static class DtoWithSchemaLocation { |
| 23 | + public String value; |
| 24 | + public String schemaLocation; |
| 25 | + } |
| 26 | + |
| 27 | + private final XmlMapper MAPPER_SKIP = mapperBuilder() |
| 28 | + .enable(XmlReadFeature.SKIP_UNKNOWN_XSI_ATTRIBUTES) |
| 29 | + .build(); |
| 30 | + |
| 31 | + private final XmlMapper MAPPER_DEFAULT = mapperBuilder().build(); |
| 32 | + |
| 33 | + // Basic case from issue #358: xsi:schemaLocation should be ignored when enabled |
| 34 | + @Test |
| 35 | + public void testXsiSchemaLocationIgnored() throws Exception |
| 36 | + { |
| 37 | + Dto result = MAPPER_SKIP.readValue( |
| 38 | + "<dto xmlns=\"http://SomeNamespace\"" |
| 39 | + + " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" |
| 40 | + + " xsi:schemaLocation=\"http://SomeNamespace" |
| 41 | + + " http://SomeNamespace/schema.xsd\">" |
| 42 | + + "<value>hello</value>" |
| 43 | + + "</dto>", |
| 44 | + Dto.class); |
| 45 | + assertEquals("hello", result.value); |
| 46 | + } |
| 47 | + |
| 48 | + // Also test xsi:noNamespaceSchemaLocation |
| 49 | + @Test |
| 50 | + public void testXsiNoNamespaceSchemaLocationIgnored() throws Exception |
| 51 | + { |
| 52 | + Dto result = MAPPER_SKIP.readValue( |
| 53 | + "<dto xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" |
| 54 | + + " xsi:noNamespaceSchemaLocation=\"schema.xsd\">" |
| 55 | + + "<value>world</value>" |
| 56 | + + "</dto>", |
| 57 | + Dto.class); |
| 58 | + assertEquals("world", result.value); |
| 59 | + } |
| 60 | + |
| 61 | + // Ensure regular attributes still work alongside XSI attributes |
| 62 | + @Test |
| 63 | + public void testXsiSchemaLocationWithRegularAttributes() throws Exception |
| 64 | + { |
| 65 | + DtoWithAttr result = MAPPER_SKIP.readValue( |
| 66 | + "<DtoWithAttr xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" |
| 67 | + + " xsi:schemaLocation=\"http://SomeNamespace schema.xsd\"" |
| 68 | + + " count=\"42\">" |
| 69 | + + "<value>test</value>" |
| 70 | + + "</DtoWithAttr>", |
| 71 | + DtoWithAttr.class); |
| 72 | + assertEquals("test", result.value); |
| 73 | + assertEquals(42, result.count); |
| 74 | + } |
| 75 | + |
| 76 | + // When feature is disabled (default), XSI attributes should be exposed |
| 77 | + // and bindable to POJO properties |
| 78 | + @Test |
| 79 | + public void testXsiSchemaLocationExposedByDefault() throws Exception |
| 80 | + { |
| 81 | + DtoWithSchemaLocation result = MAPPER_DEFAULT.readValue( |
| 82 | + "<DtoWithSchemaLocation xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"" |
| 83 | + + " xsi:schemaLocation=\"http://SomeNamespace schema.xsd\">" |
| 84 | + + "<value>bound</value>" |
| 85 | + + "</DtoWithSchemaLocation>", |
| 86 | + DtoWithSchemaLocation.class); |
| 87 | + assertEquals("bound", result.value); |
| 88 | + assertEquals("http://SomeNamespace schema.xsd", result.schemaLocation); |
| 89 | + } |
| 90 | +} |
0 commit comments