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

@JsonSetter(nulls=...) not working with XML #285

Closed
cowtowncoder opened this issue May 4, 2018 · 6 comments
Closed

@JsonSetter(nulls=...) not working with XML #285

cowtowncoder opened this issue May 4, 2018 · 6 comments
Milestone

Comments

@cowtowncoder
Copy link
Member

cowtowncoder commented May 4, 2018

(note: follow-up from #209)

Ok it looks like feature added in 2.9 for configurable null handling:

FasterXML/jackson-databind#1402

does not quite work with XML. Need to figure out why, and fix it.

@markaren
Copy link

markaren commented May 4, 2018

The following seems to work as expected

    class Real

    class ScalarVariable {

        @JsonSetter(nulls = Nulls.AS_EMPTY)
        @JacksonXmlProperty(localName = "Real")
        var real: Real? = null

    }

    @Test
    fun test() {

        val xml = """
            <ScalarVariable >
                <Real />
             </ScalarVariable >
            """

        val mapper = XmlMapper().apply {
            registerModule(KotlinModule())
        }
        val variable = mapper.readValue<ScalarVariable>(xml)

        Assert.assertNotNull(variable.real)

    }

Removing

@JsonSetter(nulls = Nulls.AS_EMPTY)

And the test will fail

Version 2.9.5

@cowtowncoder
Copy link
Member Author

@markaren thank you for testing it.

I actually noticed the same: I have a more elaborate case that uses generic type, and that is problematic for some reason... but simple POJO dependency does work.

@cowtowncoder
Copy link
Member Author

Actually, looks like it IS working, but... As per #246, only empty tag (like <value/>) gets coerced to null, but not (yet) <value></value>. But @JsonSetter itself does work.

There may be 2 follow up things here:

  1. Make start/end tag also coerce to null with same feature (seems reasonable)
  2. Ensure that on databind side, coercing empty String to null will appropriately honor @JsonSetter setting.

cowtowncoder added a commit that referenced this issue May 4, 2018
@cowtowncoder cowtowncoder removed the 2.9 label May 5, 2018
@cowtowncoder cowtowncoder added this to the 2.9.6 milestone May 5, 2018
@cowtowncoder
Copy link
Member Author

Will work better in 2.9.6 as well.

@PaulBenn
Copy link

  • Make start/end tag also coerce to null with same feature (seems reasonable)

Hi, is this planned for a future milestone?

Nulls.AS_EMPTY does not support the following form of empty collection yet (as of version 2.11):

<MyList></MyList>

@cowtowncoder
Copy link
Member Author

@PaulBenn You may try this against 2.12.0-rc1 (released couple of days ago). I think handling should result in empty List with 2.12.0, regardless of this configuration (that is, should not even need to set it).

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

3 participants