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

XmlMapper does not find no-argument record constructor for deserialization of empty XML #542

Closed
MichalStehlikCz opened this issue Aug 24, 2022 · 3 comments
Milestone

Comments

@MichalStehlikCz
Copy link

Replicated with Jackson 2.13.3

Might be related to #508

Unlike XmlMapper is not able to find no-argument record constructor for deserialization of emptzy document even if one is explicitly defined

public class NoArgumentConstructorTest {

  public record TestRecord(@Nullable String a) {

    public TestRecord() {
      this(null);
    }
  }

  @Test
  void testNoArgumentConstructorXml() throws JsonProcessingException {
    var xmlMapper = new XmlMapper();
    assertThat(xmlMapper.readerFor(TestRecord.class).readValue("<TEST/>"))
        .isNotNull();
  }
}
com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of `com.provys.serverapi.restapi.obj.api.NoArgumentConstructorTest$TestRecord` (although at least one Creator exists): no default no-arguments constructor found
 at [Source: (StringReader); line: 1, column: 1]

	at com.fasterxml.jackson.databind.exc.MismatchedInputException.from(MismatchedInputException.java:63)
	at com.fasterxml.jackson.databind.DeserializationContext.reportInputMismatch(DeserializationContext.java:1728)
	at com.fasterxml.jackson.databind.DeserializationContext.handleMissingInstantiator(DeserializationContext.java:1353)
	at com.fasterxml.jackson.databind.deser.ValueInstantiator.createUsingDefault(ValueInstantiator.java:248)
	at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createUsingDefault(StdValueInstantiator.java:275)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.getEmptyValue(BeanDeserializerBase.java:1042)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromEmptyString(StdDeserializer.java:322)
	at com.fasterxml.jackson.databind.deser.std.StdDeserializer._deserializeFromString(StdDeserializer.java:270)
	at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1495)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:196)
	at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:186)
	at com.fasterxml.jackson.dataformat.xml.deser.XmlDeserializationContext.readRootValue(XmlDeserializationContext.java:91)
	at com.fasterxml.jackson.databind.ObjectReader._bindAndClose(ObjectReader.java:2051)
	at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:1492)
	at com.provys.serverapi.restapi.obj.api.NoArgumentConstructorTest.testNoArgumentConstructorXml(NoArgumentConstructorTest.java:30)

@cowtowncoder
Copy link
Member

Jackson does not consider any of alternate Record constructor but only the primary one.
This does not vary by mapper I think? It would be good to test it for JSON since if this is the case, it'd need improvement there.

I think it would be nice to also auto-detect 0-args constructor FWTW, above is just stating that this does not exist yet.

@cowtowncoder
Copy link
Member

Actually fix for #547 (alas, only for 2.15) might help here, essentially resolve. And if not (Record vs POJO) should be relatively easy to solve.

Just need to figure out best way to test, wrt combination of things; XML module only requiring JDK 8 for build but Records available only in 15+

@cowtowncoder cowtowncoder added this to the 2.15.0 milestone Jan 22, 2023
@cowtowncoder
Copy link
Member

I added a test in https://github.com/FasterXML/jackson-jdk17-compat-test to verify that this is indeed fixed; will mark as such.

@cowtowncoder cowtowncoder changed the title XmlMapper does not find no-argument record constructor for deserialization of empty XML XmlMapper does not find no-argument record constructor for deserialization of empty XML Jan 22, 2023
cowtowncoder added a commit that referenced this issue Jan 22, 2023
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