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

Fix mismatch in setNextIsUnwrapped(boolean) in XmlBeanSerializerBase#serializeFieldsFiltered() #616

Merged
merged 2 commits into from
Dec 2, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ protected void serializeFieldsFiltered(Object bean, JsonGenerator gen0,
filter.serializeAsField(bean, xgen, provider, prop);
}
}
// Reset to avoid next value being written as unwrapped,
// for example when property is suppressed
if (i == textIndex) {
xgen.setNextIsUnwrapped(false);
}
}
if (_anyGetterWriter != null) {
// For [#117]: not a clean fix, but with @JsonTypeInfo, we'll end up
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.fasterxml.jackson.dataformat.xml.ser;

import com.fasterxml.jackson.annotation.JsonFilter;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.ser.FilterProvider;
import com.fasterxml.jackson.databind.ser.PropertyFilter;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.XmlTestBase;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlText;

/**
* Unit test for [PullRequest#616], problems with filtered serialization.
*/
public class TestSerializationWithFilter extends XmlTestBase
{
@JsonFilter("filter")
static class Item
{
@JacksonXmlText
public int a;
public int b;
public int c;
}

public void testPullRequest616() throws Exception
{
Item bean = new Item();
bean.a = 0;
bean.b = 10;
bean.c = 100;

String exp = "<Item><b>10</b><c>100</c></Item>";

XmlMapper xmlMapper = new XmlMapper();
PropertyFilter filter = new SimpleBeanPropertyFilter() {
@Override
public void serializeAsField(Object pojo, JsonGenerator jgen, SerializerProvider provider, PropertyWriter writer) throws Exception
{
if (include(writer) && writer.getName().equals("a")) {
int a = ((Item) pojo).a;
if (a <= 0)
return;
}
super.serializeAsField(pojo, jgen, provider, writer);
}
};
FilterProvider filterProvider = new SimpleFilterProvider().addFilter("filter", filter);
xmlMapper.setFilterProvider(filterProvider);
String act = xmlMapper.writeValueAsString(bean);
assertEquals(exp, act);
}
}