Skip to content

Commit

Permalink
Merge branch '2.16'
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Oct 1, 2023
2 parents 6512a07 + 8328baf commit 907b09e
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 43 deletions.
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ Project: jackson-databind
(contributed by Joo-Hyuk K)
#2787: Mix-ins do not work for `Enum`s
(fix contributed by Joo-Hyuk K)
#3647: `@JsonIgnoreProperties` not working with `@JsonValue`
(reported by @ThatSneakyRaccoon)
(fix contributed by Joo-Hyuk K)
#3780: Deprecated JsonNode.with(String) suggests using JsonNode.withObject(String)
but it is not the same thing
(reported by @bmatasar)
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/tools/jackson/databind/ValueSerializer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tools.jackson.databind;

import java.util.Iterator;
import java.util.Set;

import com.fasterxml.jackson.annotation.JsonFormat;

Expand Down Expand Up @@ -153,6 +154,17 @@ public ValueSerializer<?> withFilterId(Object filterId) {
return this;
}

/**
* Mutant factory method called to create a new instance after excluding specified set of
* properties by name, if there is any.
*
* @param ignoredProperties Set of property names to ignore for serialization;
* @return Serializer instance that without specified set of properties to ignore (if any)
*/
public ValueSerializer<?> withIgnoredProperties(Set<String> ignoredProperties) {
return this;
}

/**
* Mutant factory called if there is need to create a serializer with specified
* format overrides (typically from property on which this serializer would be used,
Expand Down
6 changes: 5 additions & 1 deletion src/main/java/tools/jackson/databind/ser/BeanSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ protected BeanSerializer(BeanSerializerBase src, Set<String> toIgnore, Set<Strin
super(src, toIgnore, toInclude);
}

// @since 2.11.1
protected BeanSerializer(BeanSerializerBase src,
BeanPropertyWriter[] properties, BeanPropertyWriter[] filteredProperties) {
super(src, properties, filteredProperties);
Expand Down Expand Up @@ -115,6 +114,11 @@ protected BeanSerializerBase withProperties(BeanPropertyWriter[] properties,
return new BeanSerializer(this, properties, filteredProperties);
}

@Override
public ValueSerializer<?> withIgnoredProperties(Set<String> toIgnore) {
return new BeanSerializer(this, toIgnore, null);
}

/**
* Implementation has to check whether as-array serialization
* is possible reliably; if (and only if) so, will construct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public BeanSerializerBase withFilterId(Object filterId) {
return new BeanSerializer(this, _objectIdWriter, filterId);
}

@Override
public ValueSerializer<?> withIgnoredProperties(Set<String> toIgnore) {
// Revert to Vanilla variant here as well
return new BeanSerializer(this, toIgnore, null);
}

@Override
protected BeanSerializerBase withByNameInclusion(Set<String> toIgnore, Set<String> toInclude) {
return new UnrolledBeanSerializer(this, toIgnore, toInclude);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tools.jackson.databind.ser.impl;

import java.util.Arrays;
import java.util.function.UnaryOperator;

import tools.jackson.databind.BeanProperty;
import tools.jackson.databind.JavaType;
Expand Down Expand Up @@ -77,6 +78,24 @@ public final SerializerAndMapResult findAndAddSecondarySerializer(JavaType type,
return new SerializerAndMapResult(serializer, newWith(type.getRawClass(), serializer));
}

public final SerializerAndMapResult findAndAddSecondarySerializer(Class<?> type,
SerializerProvider provider, BeanProperty property,
UnaryOperator<ValueSerializer<Object>> serTransformer)
{
ValueSerializer<Object> serializer = provider.findContentValueSerializer(type, property);
serializer = serTransformer.apply(serializer);
return new SerializerAndMapResult(serializer, newWith(type, serializer));
}

public final SerializerAndMapResult findAndAddSecondarySerializer(JavaType type,
SerializerProvider provider, BeanProperty property,
UnaryOperator<ValueSerializer<Object>> serTransformer)
{
ValueSerializer<Object> serializer = provider.findContentValueSerializer(type, property);
serializer = serTransformer.apply(serializer);
return new SerializerAndMapResult(serializer, newWith(type.getRawClass(), serializer));
}

/**
* Method called if initial lookup fails, when looking for a root value
* serializer: one that is not directly attached to a property, but needs to
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
import java.lang.reflect.InvocationTargetException;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.function.UnaryOperator;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonTypeInfo.As;
import tools.jackson.core.*;
import tools.jackson.core.type.WritableTypeId;
Expand Down Expand Up @@ -179,7 +181,8 @@ public ValueSerializer<?> createContextual(SerializerProvider ctxt,
*/

@Override
public void serialize(Object bean, JsonGenerator gen, SerializerProvider ctxt)
public void serialize(final Object bean, final JsonGenerator gen,
final SerializerProvider ctxt)
throws JacksonException
{
Object value;
Expand All @@ -195,12 +198,15 @@ public void serialize(Object bean, JsonGenerator gen, SerializerProvider ctxt)
}
ValueSerializer<Object> ser = _valueSerializer;
if (ser == null) {
final UnaryOperator<ValueSerializer<Object>> serTransformer =
valueSer -> _withIgnoreProperties(ctxt, _accessor, valueSer);
Class<?> cc = value.getClass();
if (_valueType.hasGenericTypes()) {
ser = _findAndAddDynamic(ctxt,
ctxt.constructSpecializedType(_valueType, cc));
ctxt.constructSpecializedType(_valueType, cc),
serTransformer);
} else {
ser = _findAndAddDynamic(ctxt, cc);
ser = _findAndAddDynamic(ctxt, cc, serTransformer);
}
}
if (_valueTypeSerializer != null) {
Expand All @@ -210,6 +216,28 @@ public void serialize(Object bean, JsonGenerator gen, SerializerProvider ctxt)
}
}

/**
* Internal helper that configures the provided {@code ser} to ignore properties specified by {@link JsonIgnoreProperties}.
*
* @param ctxt For introspection.
* @param ser Serializer to be configured
* @return Configured serializer with specified properties ignored
*/
@SuppressWarnings("unchecked")
protected ValueSerializer<Object> _withIgnoreProperties(SerializerProvider ctxt,
AnnotatedMember accessor,
ValueSerializer<?> ser) {
final AnnotationIntrospector ai = ctxt.getAnnotationIntrospector();
final SerializationConfig config = ctxt.getConfig();

JsonIgnoreProperties.Value ignorals = ai.findPropertyIgnoralByName(config, accessor);
Set<String> ignored = ignorals.findIgnoredForSerialization();
if (!ignored.isEmpty()) {
ser = ser.withIgnoredProperties(ignored);
}
return (ValueSerializer<Object>) ser;
}

@Override
public void serializeWithType(Object bean, JsonGenerator gen, SerializerProvider ctxt,
TypeSerializer typeSer0) throws JacksonException
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package tools.jackson.databind.ser.std;

import java.util.function.UnaryOperator;

import tools.jackson.databind.*;
import tools.jackson.databind.jsontype.TypeSerializer;
import tools.jackson.databind.ser.impl.PropertySerializerMap;
Expand Down Expand Up @@ -91,6 +93,18 @@ protected final ValueSerializer<Object> _findAndAddDynamic(SerializerProvider ct
return result.serializer;
}

protected final ValueSerializer<Object> _findAndAddDynamic(SerializerProvider ctxt, Class<?> type,
UnaryOperator<ValueSerializer<Object>> serTransformer)
{
PropertySerializerMap map = _dynamicValueSerializers;
PropertySerializerMap.SerializerAndMapResult result = map.findAndAddSecondarySerializer(type,
ctxt, _property, serTransformer);
if (map != result.map) {
_dynamicValueSerializers = result.map;
}
return result.serializer;
}

protected final ValueSerializer<Object> _findAndAddDynamic(SerializerProvider ctxt, JavaType type)
{
PropertySerializerMap map = _dynamicValueSerializers;
Expand All @@ -101,4 +115,16 @@ protected final ValueSerializer<Object> _findAndAddDynamic(SerializerProvider ct
}
return result.serializer;
}

protected final ValueSerializer<Object> _findAndAddDynamic(SerializerProvider ctxt, JavaType type,
UnaryOperator<ValueSerializer<Object>> serTransformer)
{
PropertySerializerMap map = _dynamicValueSerializers;
PropertySerializerMap.SerializerAndMapResult result = map.findAndAddSecondarySerializer(type,
ctxt, _property, serTransformer);
if (map != result.map) {
_dynamicValueSerializers = result.map;
}
return result.serializer;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package tools.jackson.databind.ser.filter;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonValue;

import tools.jackson.databind.BaseMapTest;
import tools.jackson.databind.ObjectMapper;

// [databind#3647] : Support @JsonIgnoreProperties to work with @JsonValue
public class JsonValueIgnore3647Test extends BaseMapTest
{
static class Foo3647 {
public String p1 = "hello";
public String p2 = "world";
}

static class Bar3647 {
@JsonValue
@JsonIgnoreProperties("p1")
public Foo3647 getFoo() {
return new Foo3647();
}
}

@JsonIgnoreProperties({"a"})
static class Bean3647 {
public String a = "hello";
public String b = "world";
}

static class Container3647 {
@JsonValue
@JsonIgnoreProperties("b")
public Bean3647 getBean() {
return new Bean3647();
}
}

static class Base3647 {
public String a = "hello";
public String b = "world";
}

static class BaseContainer3647 {
@JsonValue
public Base3647 getBean() {
return new Base3647();
}
}

static class MixinContainer3647 {
@JsonIgnoreProperties("b")
public Base3647 getBean() {
return new Base3647();
}
}

@JsonIgnoreProperties({"a", "b"})
static class Mixin3647 {
public String a = "hello";
public String b = "world";
}

/*
/**********************************************************************
/* Test methods
/**********************************************************************
*/

private final ObjectMapper MAPPER = newJsonMapper();

public void testIgnorePropsAndJsonValueAtSameLevel() throws Exception
{
assertEquals("{\"p2\":\"world\"}",
MAPPER.writeValueAsString(new Bar3647()));
}

public void testUnionOfIgnorals() throws Exception
{
assertEquals("{}",
MAPPER.writeValueAsString(new Container3647()));
}

public void testMixinContainerAndJsonValue() throws Exception
{
ObjectMapper mapper = jsonMapperBuilder()
.addMixIn(BaseContainer3647.class, MixinContainer3647.class)
.build();

assertEquals("{\"a\":\"hello\"}",
mapper.writeValueAsString(new BaseContainer3647()));
}

public void testMixinAndJsonValue() throws Exception
{
ObjectMapper mapper = jsonMapperBuilder()
.addMixIn(Base3647.class, Mixin3647.class)
.build();

assertEquals("{}",
mapper.writeValueAsString(new Base3647()));
}
}
39 changes: 0 additions & 39 deletions src/test/java/tools/jackson/failing/JsonValueIgnore3647Test.java

This file was deleted.

0 comments on commit 907b09e

Please sign in to comment.