Skip to content

@JacksonXmlRootElement is ignored with Jackson 3 #757

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

Closed
sdeleuze opened this issue May 13, 2025 · 10 comments
Closed

@JacksonXmlRootElement is ignored with Jackson 3 #757

sdeleuze opened this issue May 13, 2025 · 10 comments

Comments

@sdeleuze
Copy link

With Jackson 3.0.0-rc4:

@JsonInclude(NON_EMPTY)
@JacksonXmlRootElement(localName = "problem", namespace = ProblemDetailJacksonXmlMixin.NAMESPACE)
public interface ProblemDetailJacksonXmlMixin {

	/** RFC 7807 (obsoleted by RFC 9457) namespace. */
	String NAMESPACE = "urn:ietf:rfc:7807";


	@JacksonXmlProperty(namespace = NAMESPACE)
	URI getType();

	@JacksonXmlProperty(namespace = NAMESPACE)
	String getTitle();

	@JacksonXmlProperty(namespace = NAMESPACE)
	int getStatus();

	@JacksonXmlProperty(namespace = NAMESPACE)
	String getDetail();

	@JacksonXmlProperty(namespace = NAMESPACE)
	URI getInstance();

	@JsonAnySetter
	void setProperty(String name, @Nullable Object value);

	@JsonAnyGetter
	@JacksonXmlProperty(namespace = NAMESPACE)
	Map<String, Object> getProperties();

}

@SuppressWarnings("serial")
private static class MyProblemDetail extends ProblemDetail {

	private static final long serialVersionUID = 848059476504348430L;

	public MyProblemDetail(HttpStatus status) {
		super(status.value());
	}
}

@Test
void test() {
	XmlMapper mapper = XmlMapper.builder()
			.addMixIn(ProblemDetail.class, ProblemDetailJacksonXmlMixin.class)
			.build();
	MyProblemDetail problemDetail = new MyProblemDetail(HttpStatus.BAD_REQUEST);
	System.out.println(mapper.writerFor(MyProblemDetail.class).writeValueAsString(problemDetail));
}

Jackson 2.x prints as expected:

<problem xmlns="urn:ietf:rfc:7807"><type>about:blank</type><title>Bad Request</title><status>400</status></problem>

Jackson 3.x ignores the @JacksonXmlRootElement annotation and prints:

<MyProblemDetail><status>400</status><title>Bad Request</title><type>about:blank</type></MyProblemDetail>

This is breaking Spring support for RFC 9457.

@pjfanning
Copy link
Member

try builder().enable(SerializationFeature.WRAP_ROOT_VALUE)

@sdeleuze
Copy link
Author

sdeleuze commented May 13, 2025

Thanks for the proposal, but I don't think I can configure that, since it will break other use cases (I am dealing here with JacksonXmlHttpMessageConverter default XmlMapper).

Also no change of behavior seems documented for that between 2.x and 3.x in the Javadoc, and @JsonRootName works by default (but is not usable for us due to #355), so I tend to think this is a genuine bug/regression.

@cowtowncoder
Copy link
Member

Sounds like potential bug. I assume 2.x refers to 2.19.0?

I think there were some fixes wrt property name merging -- and perhaps namespace-only case was broken. Will need to look into this.

@sdeleuze
Copy link
Author

sdeleuze commented May 13, 2025

I tested with 2.18.3 but that always worked on 2.x as far as I can tell. Seems also broken without mixins involved.

@cowtowncoder
Copy link
Member

Right, but what I am trying to figure out is whether it broke with 2.19 or 3.0. Has not been reported for 2.19 fwtw. And none of XML changes in https://github.com/FasterXML/jackson/wiki/Jackson-Release-2.19 sound suspicious.

I can check this later on when I have time, just thought to confirm version you used.

@sdeleuze
Copy link
Author

Just tested, can't reproduce with Jackson 2.19.0, that's a Jackson 3 regression.

@cowtowncoder
Copy link
Member

Thanks!

@cowtowncoder
Copy link
Member

@sdeleuze Looks like reproduction is incomplete: it is missing ProblemDetail. Could you add that?

Also ideally I think we'd have minimal reproduction (just remove all but one of fields for POJO).
But I can do that.

@sdeleuze
Copy link
Author

Hum, I think I understand what happens now. @JacksonXmlRootElement is not part of jackson-annotations but jackson-dataformat-xml dependency so it has changed of package as part of the Jackson 3 migration, hence the fact com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement is not recognized with Jackson 3.

@sdeleuze sdeleuze closed this as not planned Won't fix, can't repro, duplicate, stale May 15, 2025
@cowtowncoder
Copy link
Member

Ah. Yeah, in-module annotations (jackson-databind has some too) are bit challenging as unlike jackson-annotations, their Java packages change.

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