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

MappingJackson2XmlHttpMessageConverter should support non UTF charsets #34090

Closed
mrpiggi opened this issue Dec 13, 2024 · 2 comments
Closed

MappingJackson2XmlHttpMessageConverter should support non UTF charsets #34090

mrpiggi opened this issue Dec 13, 2024 · 2 comments
Assignees
Labels
for: external-project Needs a fix in external project in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@mrpiggi
Copy link

mrpiggi commented Dec 13, 2024

I currently have to deal with a REST API using text/xml;charset=ISO-8859-1 as MediaType. So just using MappingJackson2XmlHttpMessageConverter seemed obvious to me. Unfortunately I found myself with

RestClientException: No HttpMessageConverter for ... and content type "text/xml;charset=ISO-8859-1"

As it turns out, inheriting from AbstractJackson2HttpMessageConverter checks for hard-coded UTF encodings when calling MappingJackson2XmlHttpMessageConverter.canWrite()

private static final Map<String, JsonEncoding> ENCODINGS;
static {
ENCODINGS = CollectionUtils.newHashMap(JsonEncoding.values().length);
for (JsonEncoding encoding : JsonEncoding.values()) {
ENCODINGS.put(encoding.getJavaName(), encoding);
}
ENCODINGS.put("US-ASCII", JsonEncoding.UTF8);
}

public boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType) {
if (!canWrite(mediaType)) {
return false;
}
if (mediaType != null && mediaType.getCharset() != null) {
Charset charset = mediaType.getCharset();
if (!ENCODINGS.containsKey(charset.name())) {
return false;
}
}
ObjectMapper objectMapper = selectObjectMapper(clazz, mediaType);

which is totally reasonable for JSON but not for XML. Overriding this method is not a problem at all but I wanted you to be notified at least.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged or decided on label Dec 13, 2024
@sdeleuze sdeleuze self-assigned this Dec 16, 2024
@sdeleuze sdeleuze added in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged or decided on labels Dec 16, 2024
@sdeleuze sdeleuze added this to the 6.2.2 milestone Dec 18, 2024
@sdeleuze
Copy link
Contributor

sdeleuze commented Dec 27, 2024

After having a deeper look, I am going to decline this issue due to assumptions hardcoded in Jackson JsonEncoding, what was discussed in #25076 and given the fact FasterXML/jackson-dataformat-xml#315 is opened for years. There are workarounds possible but we would need to change totally the Jackson converter and codec hierarchy, so I prefer to wait for first-class support for that in Jackson.

For now, please use Jaxb2RootElementHttpMessageConverter if you have such need.

@sdeleuze sdeleuze closed this as not planned Won't fix, can't repro, duplicate, stale Dec 27, 2024
@sdeleuze sdeleuze removed this from the 6.2.2 milestone Dec 27, 2024
@sdeleuze sdeleuze added status: declined A suggestion or change that we don't feel we should currently apply for: external-project Needs a fix in external project labels Dec 27, 2024
@mrpiggi
Copy link
Author

mrpiggi commented Dec 27, 2024

I should have looked more carefully for old issues. Sorry about that. Thanks for pointing to
Jaxb2RootElementHttpMessageConverter that's exactly the way I went.

Sorry for the noise...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project Needs a fix in external project in: web Issues in web modules (web, webmvc, webflux, websocket) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants