Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Without setting @JsonCreator for the POJO, the required attribute in @JsonProperty is not checked #625

Closed
linghengqian opened this issue Dec 28, 2023 · 1 comment

Comments

@linghengqian
Copy link

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;

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?
@cowtowncoder
Copy link
Member

Correct: @JsonProperty(required=true) is only enforced if and when properties are passed via Creator method (constructor or static factory method). It would be nice to support if for Setter/Field injected properties too; it is one of "most-wanted" issues:

FasterXML/jackson-databind#230

Closing this as duplicate (also note: this is not XML-specific issue but same for all backends).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants