Skip to content

Commit

Permalink
Fixed #3001
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 2, 2021
1 parent a886176 commit 64ba6ce
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
3 changes: 3 additions & 0 deletions release-notes/VERSION-2.x
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ Project: jackson-databind
2.13.0 (not yet released)

#2828: Add `DatabindException` as intermediate subtype of `JsonMappingException`
#3001: Add mechanism for setting default `ContextAttributes` for `ObjectMapper`
#3035: Add `removeMixIn()` method in `MapperBuilder`
#3036: Backport `MapperBuilder` lambda-taking methods: `withConfigOverride()`,
`withCoercionConfig()`, `withCoercionConfigDefaults()`

2.12.2 (not yet released)

Expand Down
17 changes: 15 additions & 2 deletions src/main/java/com/fasterxml/jackson/databind/ObjectMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -2426,6 +2426,20 @@ public ObjectMapper setTimeZone(TimeZone tz) {
return this;
}

/**
*<p>
* NOTE: preferred way to set the defaults is to use {@code Builder} style
* construction, see {@link com.fasterxml.jackson.databind.json.JsonMapper#builder}
* (and {@link MapperBuilder#defaultAttributes}).
*
* @since 2.13
*/
public ObjectMapper setDefaultAttributes(ContextAttributes attrs) {
_deserializationConfig = _deserializationConfig.with(attrs);
_serializationConfig = _serializationConfig.with(attrs);
return this;
}

/*
/**********************************************************
/* Configuration, simple features: MapperFeature
Expand Down Expand Up @@ -2760,8 +2774,7 @@ public boolean isEnabled(StreamWriteFeature f) {
/*
/**********************************************************
/* Public API (from ObjectCodec): deserialization
/* (mapping from JSON to Java types);
/* main methods
/* (mapping from JSON to Java types); main methods
/**********************************************************
*/

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,23 @@ public B annotationIntrospector(AnnotationIntrospector intr) {
return _this();
}

/**
* Method for replacing default {@link ContextAttributes} that the mapper
* uses: usually one initialized with a set of default shared attributes, but
* potentially also with a custom implementation.
*<p>
* NOTE: instance specified will need to be thread-safe for usage, similar to the
* default ({@link ContextAttributes.Impl}).
*
* @param attrs Default instance to use: may not be {@code null}.
*
* @return This Builder instance to allow call chaining
*/
public B defaultAttributes(ContextAttributes attrs) {
_mapper.setDefaultAttributes(attrs);
return _this();
}

/*
/**********************************************************************
/* Changing introspection helpers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;

public class TestContextAttributeWithDeser extends BaseMapTest
Expand Down Expand Up @@ -92,4 +93,29 @@ public void testHierarchic() throws Exception
assertEquals("x/2", pojos2[0].value);
assertEquals("y/3", pojos2[1].value);
}

// [databind#3001]
public void testDefaultsViaMapper() throws Exception
{
final String INPUT = aposToQuotes("{'value':'x'}");
ContextAttributes attrs = ContextAttributes.getEmpty()
.withSharedAttribute(KEY, Integer.valueOf(72));
ObjectMapper mapper = jsonMapperBuilder()
.defaultAttributes(attrs)
.build();
TestPOJO pojo = mapper.readerFor(TestPOJO.class)
.readValue(INPUT);
assertEquals("x/72", pojo.value);

// as above, should not carry on state
TestPOJO pojo2 = mapper.readerFor(TestPOJO.class)
.readValue(INPUT);
assertEquals("x/72", pojo2.value);

// And should be overridable too
TestPOJO pojo3 = mapper.readerFor(TestPOJO.class)
.withAttribute(KEY, Integer.valueOf(19))
.readValue(INPUT);
assertEquals("x/19", pojo3.value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.cfg.ContextAttributes;
import com.fasterxml.jackson.databind.ser.std.StdScalarSerializer;

public class TestContextAttributeWithSer extends BaseMapTest
Expand Down Expand Up @@ -84,4 +85,26 @@ public void testHierarchic() throws Exception
// and verify state clearing:
assertEquals(EXP, w.writeValueAsString(INPUT));
}

// [databind#3001]
public void testDefaultsViaMapper() throws Exception
{
final TestPOJO[] INPUT = new TestPOJO[] { new TestPOJO("a"), new TestPOJO("b") };
ContextAttributes attrs = ContextAttributes.getEmpty()
.withSharedAttribute(KEY, Integer.valueOf(72));
ObjectMapper mapper = jsonMapperBuilder()
.defaultAttributes(attrs)
.build();
final String EXP1 = a2q("[{'value':'72:a'},{'value':'73:b'}]");
assertEquals(EXP1, mapper.writeValueAsString(INPUT));

// value should be "reset" as well
assertEquals(EXP1, mapper.writeValueAsString(INPUT));

// and should be overridable on per-call basis too
assertEquals(a2q("[{'value':'13:a'},{'value':'14:b'}]"),
mapper.writer()
.withAttribute(KEY, Integer.valueOf(13))
.writeValueAsString(INPUT));
}
}

0 comments on commit 64ba6ce

Please sign in to comment.