Skip to content

Commit 3266d8d

Browse files
committed
Improve javadocs wrt #1214
1 parent f8ed660 commit 3266d8d

File tree

3 files changed

+54
-7
lines changed

3 files changed

+54
-7
lines changed

src/main/java/com/fasterxml/jackson/databind/annotation/JsonDeserialize.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,17 @@
2525
* )
2626
*</pre>
2727
*<p>
28+
* Something to note on usage:
29+
*<ul>
30+
* <li>All other annotations regarding behavior during building should be on <b>Builder</b>
31+
* class and NOT on target POJO class: for example &#64;JsonIgnoreProperties should be on
32+
* Builder to prevent "unknown property" errors.
33+
* </li>
34+
* <li>Similarly configuration overrides (see {@link com.fasterxml.jackson.databind.ObjectMapper#configOverride})
35+
* should be targeted at Builder class, not target POJO class.
36+
* </li>
37+
* </ul>
38+
*
2839
*/
2940
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD, ElementType.FIELD, ElementType.TYPE, ElementType.PARAMETER})
3041
@Retention(RetentionPolicy.RUNTIME)

src/main/java/com/fasterxml/jackson/databind/annotation/JsonPOJOBuilder.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,16 @@
1414
* Annotation is typically used if the naming convention
1515
* of a Builder class is different from defaults:
1616
*<ul>
17+
* <li>By default, setters are expected to have names like `withName()` (for property "name");
18+
* override by {@link #withPrefix()} property.
19+
* </li>
1720
* </ul>
18-
*
21+
*<p>
22+
* In addition to configuration using this annotation, note that many other configuration
23+
* annotations are also applied to Builders, for example
24+
* {@link com.fasterxml.jackson.annotation.JsonIgnoreProperties} can be used to ignore
25+
* "unknown" properties.
26+
*
1927
* @since 2.0
2028
*/
2129
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})

src/test/java/com/fasterxml/jackson/databind/creators/BuilderSimpleTest.java

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import com.fasterxml.jackson.annotation.JsonAnySetter;
66
import com.fasterxml.jackson.annotation.JsonCreator;
7+
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
78
import com.fasterxml.jackson.annotation.JsonProperty;
89
import com.fasterxml.jackson.annotation.JsonSetter;
910
import com.fasterxml.jackson.core.JsonProcessingException;
@@ -302,19 +303,46 @@ public ValueClass822(int x, Map<String,Object> stuff) {
302303
*/
303304

304305
private final ObjectMapper MAPPER = new ObjectMapper();
305-
306+
306307
public void testSimple() throws Exception
307308
{
308309
String json = "{\"x\":1,\"y\":2}";
309310
Object o = MAPPER.readValue(json, ValueClassXY.class);
310311
assertNotNull(o);
311-
assertSame(ValueClassXY.class, o.getClass());
312-
ValueClassXY value = (ValueClassXY) o;
313-
// note: ctor adds one to both values
314-
assertEquals(value._x, 2);
315-
assertEquals(value._y, 3);
312+
assertSame(ValueClassXY.class, o.getClass());
313+
ValueClassXY value = (ValueClassXY) o;
314+
// note: ctor adds one to both values
315+
assertEquals(value._x, 2);
316+
assertEquals(value._y, 3);
316317
}
317318

319+
// related to [databind#1214]
320+
public void testSimpleWithIgnores() throws Exception
321+
{
322+
// 'z' is unknown, and would fail by default:
323+
String json = "{\"x\":1,\"y\":2,\"z\":3}";
324+
Object o = null;
325+
326+
try {
327+
o = MAPPER.readValue(json, ValueClassXY.class);
328+
fail("Should not pass");
329+
} catch (JsonMappingException e) {
330+
verifyException(e, "Unrecognized field \"z\"");
331+
}
332+
333+
// but with config overrides should pass
334+
ObjectMapper ignorantMapper = new ObjectMapper();
335+
ignorantMapper.configOverride(SimpleBuilderXY.class)
336+
.setIgnorals(JsonIgnoreProperties.Value.forIgnoreUnknown(true));
337+
o = ignorantMapper.readValue(json, ValueClassXY.class);
338+
assertNotNull(o);
339+
assertSame(ValueClassXY.class, o.getClass());
340+
ValueClassXY value = (ValueClassXY) o;
341+
// note: ctor adds one to both values
342+
assertEquals(value._x, 2);
343+
assertEquals(value._y, 3);
344+
}
345+
318346
public void testMultiAccess() throws Exception
319347
{
320348
String json = "{\"c\":3,\"a\":2,\"b\":-9}";

0 commit comments

Comments
 (0)