Skip to content

Commit 47a5ebf

Browse files
deblocktsbrannen
authored andcommitted
Support canEncode() for JAXBElement in Jaxb2XmlEncoder
Commit d7970e4 introduced support for JAXBElement in Jaxb2XmlEncoder's encodeValue() method; however, canEncode() still returned false for a JAXBElement element type. This commit revises canEncode() so that it returns true for an element type that is assignable from JAXBElement. See gh-30552 See gh-32972 Closes gh-32977
1 parent 43a113f commit 47a5ebf

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

spring-web/src/main/java/org/springframework/http/codec/xml/Jaxb2XmlEncoder.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -92,7 +92,8 @@ public boolean canEncode(ResolvableType elementType, @Nullable MimeType mimeType
9292
if (super.canEncode(elementType, mimeType)) {
9393
Class<?> outputClass = elementType.toClass();
9494
return (outputClass.isAnnotationPresent(XmlRootElement.class) ||
95-
outputClass.isAnnotationPresent(XmlType.class));
95+
outputClass.isAnnotationPresent(XmlType.class) ||
96+
elementType.isAssignableFrom(JAXBElement.class));
9697
}
9798
else {
9899
return false;

spring-web/src/test/java/org/springframework/http/codec/xml/Jaxb2XmlEncoderTests.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ protected void canEncode() {
6363
assertThat(this.encoder.canEncode(forClass(TypePojo.class), MediaType.APPLICATION_XML)).isTrue();
6464
assertThat(this.encoder.canEncode(forClass(getClass()), MediaType.APPLICATION_XML)).isFalse();
6565

66+
assertThat(this.encoder.canEncode(forClass(JAXBElement.class), MediaType.APPLICATION_XML)).isTrue();
67+
6668
// SPR-15464
6769
assertThat(this.encoder.canEncode(ResolvableType.NONE, null)).isFalse();
6870
}

0 commit comments

Comments
 (0)