|
2 | 2 |
|
3 | 3 | import org.junit.jupiter.api.Test; |
4 | 4 |
|
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | + |
5 | 8 | import com.fasterxml.jackson.annotation.JsonRootName; |
6 | 9 |
|
7 | 10 | import tools.jackson.databind.ObjectWriter; |
8 | 11 | import tools.jackson.dataformat.xml.XmlMapper; |
9 | 12 | import tools.jackson.dataformat.xml.XmlTestUtil; |
10 | 13 | import tools.jackson.dataformat.xml.XmlWriteFeature; |
| 14 | +import tools.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
11 | 15 | import tools.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
12 | 16 |
|
13 | 17 | import static org.junit.jupiter.api.Assertions.assertEquals; |
@@ -44,6 +48,14 @@ static class RootNsWithAttrBean { |
44 | 48 | public String value = "v"; |
45 | 49 | } |
46 | 50 |
|
| 51 | + // Collection field in a namespace, with wrapper and item elements both namespaced |
| 52 | + @JsonRootName("Box") |
| 53 | + static class FruitBox { |
| 54 | + @JacksonXmlElementWrapper(namespace = "urn:produce:fruit", localName = "fruits") |
| 55 | + @JacksonXmlProperty(namespace = "urn:produce:fruit", localName = "fruit") |
| 56 | + public List<String> fruits = Arrays.asList("apple", "banana", "cherry"); |
| 57 | + } |
| 58 | + |
47 | 59 | private final XmlMapper MAPPER = newMapper(); |
48 | 60 |
|
49 | 61 | // // [dataformat-xml#150]: DTD writing -- ok cases |
@@ -467,6 +479,46 @@ public void testRootElementAndAttributeBindingsCombined() throws Exception |
467 | 479 | w.writeValueAsString(new RootNsWithAttrBean())); |
468 | 480 | } |
469 | 481 |
|
| 482 | + // Collection of namespaced items under a namespaced wrapper: binding should be |
| 483 | + // declared once on the wrapper element and inherited by every item |
| 484 | + @Test |
| 485 | + public void testNamespaceBindingOnCollection() throws Exception |
| 486 | + { |
| 487 | + // Without binding: auto-generated prefix (sanity baseline) |
| 488 | + assertEquals(a2q("<Box>" |
| 489 | + +"<wstxns1:fruits xmlns:wstxns1='urn:produce:fruit'>" |
| 490 | + +"<wstxns1:fruit>apple</wstxns1:fruit>" |
| 491 | + +"<wstxns1:fruit>banana</wstxns1:fruit>" |
| 492 | + +"<wstxns1:fruit>cherry</wstxns1:fruit>" |
| 493 | + +"</wstxns1:fruits>" |
| 494 | + +"</Box>"), |
| 495 | + MAPPER.writeValueAsString(new FruitBox())); |
| 496 | + |
| 497 | + // With prefix binding: declared once on the wrapper, inherited by items |
| 498 | + ObjectWriter w = _writer(new XmlGeneratorInitializer() |
| 499 | + .addNamespace("f", "urn:produce:fruit")); |
| 500 | + assertEquals(a2q("<Box>" |
| 501 | + +"<f:fruits xmlns:f='urn:produce:fruit'>" |
| 502 | + +"<f:fruit>apple</f:fruit>" |
| 503 | + +"<f:fruit>banana</f:fruit>" |
| 504 | + +"<f:fruit>cherry</f:fruit>" |
| 505 | + +"</f:fruits>" |
| 506 | + +"</Box>"), |
| 507 | + w.writeValueAsString(new FruitBox())); |
| 508 | + |
| 509 | + // With default namespace binding: wrapper uses unprefixed xmlns, items inherit |
| 510 | + w = _writer(new XmlGeneratorInitializer() |
| 511 | + .addDefaultNamespace("urn:produce:fruit")); |
| 512 | + assertEquals(a2q("<Box>" |
| 513 | + +"<fruits xmlns='urn:produce:fruit'>" |
| 514 | + +"<fruit>apple</fruit>" |
| 515 | + +"<fruit>banana</fruit>" |
| 516 | + +"<fruit>cherry</fruit>" |
| 517 | + +"</fruits>" |
| 518 | + +"</Box>"), |
| 519 | + w.writeValueAsString(new FruitBox())); |
| 520 | + } |
| 521 | + |
470 | 522 | // // [dataformat-xml#207]: namespace binding -- failing cases |
471 | 523 |
|
472 | 524 | @Test |
|
0 commit comments