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

Don't use custom BeanPropertyWriter for xml module #7170

Merged
merged 2 commits into from Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions jackson-databind/build.gradle
Expand Up @@ -31,6 +31,7 @@ dependencies {
testImplementation project(":inject-java")
testImplementation project(":inject-java-test")
testImplementation project(":inject-groovy")
testImplementation "com.fasterxml.jackson.dataformat:jackson-dataformat-xml"
if (!JavaVersion.current().isJava9Compatible()) {
testImplementation files(org.gradle.internal.jvm.Jvm.current().toolsJar)
}
Expand Down
Expand Up @@ -343,7 +343,10 @@ public JsonSerializer<?> build() {
final Optional<BeanProperty<Object, Object>> property = Optional.ofNullable(named.get(existing.getName()));
// ignore properties that are @JsonIgnore, so that we don't replace other properties of the
// same name
if (property.isPresent() && !property.get().isAnnotationPresent(JsonIgnore.class)) {
if (property.isPresent() &&
!property.get().isAnnotationPresent(JsonIgnore.class) &&
// we can't support XmlBeanPropertyWriter easily https://github.com/micronaut-projects/micronaut-core/issues/5907
!existing.getClass().getName().equals("com.fasterxml.jackson.dataformat.xml.ser.XmlBeanPropertyWriter")) { // NOSONAR
final BeanProperty<Object, Object> beanProperty = property.get();
newProperties.set(i, new BeanIntrospectionPropertyWriter(
existing,
Expand Down
Expand Up @@ -21,6 +21,8 @@ import com.fasterxml.jackson.databind.SerializerProvider
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
import com.fasterxml.jackson.databind.annotation.JsonNaming
import com.fasterxml.jackson.databind.annotation.JsonSerialize
import com.fasterxml.jackson.dataformat.xml.XmlMapper
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper
import groovy.transform.EqualsAndHashCode
import groovy.transform.PackageScope
import io.micronaut.context.ApplicationContext
Expand Down Expand Up @@ -965,6 +967,31 @@ class BeanIntrospectionModuleSpec extends Specification {
}
}

@Issue('https://github.com/micronaut-projects/micronaut-core/issues/5907')
void "xml modifier"() {
given:
ApplicationContext ctx = ApplicationContext.run()
XmlMapper objectMapper = new XmlMapper()
objectMapper.registerModule(ctx.getBean(BeanIntrospectionModule))

expect:
objectMapper.writeValueAsString(new UsesXmlElementWrapper()) ==
'<UsesXmlElementWrapper><nestedItems><strings>foo</strings><strings>bar</strings></nestedItems></UsesXmlElementWrapper>'

cleanup:
ctx.close()
}

@Introspected
static class UsesXmlElementWrapper {
private List<String> strings = ["foo", "bar"];

@JacksonXmlElementWrapper(/*useWrapping = false, */localName = "nestedItems")
public List<String> getStrings() {
return strings
}
}

@Unroll("JsonProperty annotation is supported with ignoreReflectiveProperties: #ignoreReflectiveProperties")
void "JsonProperty support"(boolean ignoreReflectiveProperties) {
given:
Expand Down