Skip to content

Commit

Permalink
Merge branch '2.18' of github.com:FasterXML/jackson-dataformat-xml in…
Browse files Browse the repository at this point in the history
…to 2.18
  • Loading branch information
cowtowncoder committed Oct 11, 2024
2 parents a5ab6a7 + 5265704 commit 7375278
Show file tree
Hide file tree
Showing 2 changed files with 161 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.fasterxml.jackson.dataformat.xml.records.failing;

import java.util.List;

import org.junit.Test;

import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

// [databind#517] XML wrapper doesn't work with java records
// Equivalent to on in jdk17/.../deser/XmlWrapperRecord517Test.java
public class XmlWrapperRecord517Test
extends XmlTestBase
{

public record Request(
@JacksonXmlElementWrapper(localName = "messages")
@JacksonXmlProperty(localName = "message")
List<Message> messages
) {
public Request {}

private Request() {this(null);}
}

public record Message(String text) {
public Message {
}

private Message() {
this(null);
}
}

private final String expectedXML =
"<Request>" +
"<messages>" +
"<message>" +
"<text>Hello, World!</text>" +
"</message>" +
"</messages>" +
"</Request>";

@Test
public void testWrapper() throws Exception {
XmlWrapperRecord517Test.Request request = new Request(List.of(new Message("Hello, World!")));

// test serialization
String xml = newMapper().writeValueAsString(request);
assertEquals(expectedXML, xml);

// test deserialization
Request result = newMapper().readValue(xml, Request.class);

assertEquals(request.messages().size(), result.messages().size());
assertEquals(request.messages().get(0).text(), result.messages().get(0).text());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package com.fasterxml.jackson.dataformat.xml.deser;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.junit.Test;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;

// [databind#517] XML wrapper doesn't work with java records
// Equivalent to on in jdk17/.../records/XmlWrapperRecord517Test.java
public class XmlWrapperClass517Test
extends XmlTestBase
{
public static final class Request {
@JacksonXmlElementWrapper(localName = "messages")
@JacksonXmlProperty(localName = "message")
private final List<Message> messages;

private Request() { this.messages = null; }
public Request(List<Message> messages) { this.messages = messages; }

public List<Message> getMessages() { return messages; }

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Request)) return false;
return Objects.equals(messages, ((Request)o).messages);
}

@Override
public int hashCode() { return Objects.hash(messages); }

@Override
public String toString() { return "Request{messages=" + messages + '}'; }
}

public static final class Message {

private final String text;

private Message() { this.text = null; }
public Message(String text) { this.text = text; }

public String getText() { return text; }

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Message)) return false;
Message message = (Message) o;
return Objects.equals(text, message.text);
}

@Override
public int hashCode() { return Objects.hash(text); }

@Override
public String toString() { return "Message{text='" + text + "\'}"; }
}

private final ObjectMapper mapper = newMapper();

private final String expectedXML =
"<Request>" +
"<messages>" +
"<message>" +
"<text>given text</text>" +
"</message>" +
"</messages>" +
"</Request>";

@Test
public void testShouldSerialize() throws Exception {
Request givenRequest = _createRequest("given text");

String actualXml = mapper.writeValueAsString(givenRequest);

assertEquals(expectedXML, actualXml);
}

@Test
public void testShouldDeserialize() throws Exception {
Request expected = _createRequest("given text");

Request actualRequest = mapper.readValue(expectedXML, Request.class);

assertEquals(expected, actualRequest);
}

private Request _createRequest(String givenText) {
List<Message> messages = new ArrayList<>();
messages.add(new Message(givenText));
return new Request(messages);
}

}

0 comments on commit 7375278

Please sign in to comment.