Closed
Description
We appreciate issues as very valuable contributions, but just to make sure here are things that are important to do before filing an issue:
- Only report issues, and ask Usage Questions on Jackson-users list -- ypu are more likely to get help that way
- Check to see if this issue has already been reported (quick glance at existing issues): no deep search necessary, just quick sanity check
- Include version information for Jackson version you use
- (optional but highly recommended) Verify that the problem occurs with the latest patch of same minor version; and even better, if possible, try using the latest stable patch version
TL;
-
While investigating Removes use of JAXB API via jackson-dataformat-xml apache/shardingsphere#29384 , I noticed that without setting
@JsonCreator
for the POJO, therequired
attribute in@JsonProperty
is not checked. -
I created a minimal unit test at https://github.com/linghengqian/jackson-xml-required-check-test . The core code looks like this.
public class RequiredTest {
@Test
void assertRequiredValue() {
ObjectMapper XML_MAPPER = XmlMapper.builder().build();
assertThrows(MismatchedInputException.class, () -> XML_MAPPER.readValue("<Record></Record>", TestRecord.class));
assertThrows(MismatchedInputException.class, () -> XML_MAPPER.readValue("<TR></TR>", TestRecordWithoutCreator.class));
}
@Getter
@NoArgsConstructor
@JacksonXmlRootElement(localName = "Record")
static class TestRecord {
private String type;
private String driverClassName;
@JsonCreator
public TestRecord(
@JsonProperty(required = true) @JacksonXmlProperty(localName = "type", isAttribute = true)
String type,
@JsonProperty(required = true) @JacksonXmlProperty(localName = "driverClassName")
String driverClassName) {
this.type = type;
this.driverClassName = driverClassName;
}
}
@Getter
@JacksonXmlRootElement(localName = "TR")
static class TestRecordWithoutCreator {
@JsonProperty(required = true)
@JacksonXmlProperty(localName = "type", isAttribute = true)
private String type;
@JsonProperty(required = true)
@JacksonXmlProperty(localName = "driverClassName")
private String driverClassName;
}
}
assertThrows(MismatchedInputException.class, () -> XML_MAPPER.readValue("<TR></TR>", TestRecordWithoutCreator.class));
will not throw any exception.
[ERROR] com.lingh.RequiredTest.assertRequiredValue -- Time elapsed: 0.276 s <<< FAILURE!
org.opentest4j.AssertionFailedError: Expected com.fasterxml.jackson.databind.exc.MismatchedInputException to be thrown, but nothing was thrown.
at org.junit.jupiter.api.AssertionFailureBuilder.build(AssertionFailureBuilder.java:152)
at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:73)
at org.junit.jupiter.api.AssertThrows.assertThrows(AssertThrows.java:35)
at org.junit.jupiter.api.Assertions.assertThrows(Assertions.java:3115)
at com.lingh.RequiredTest.assertRequiredValue(RequiredTest.java:21)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1511)
- Did I misunderstand something while reading the documentation?
Metadata
Metadata
Assignees
Labels
No labels