Skip to content

Commit ec859af

Browse files
committed
Fix #1607
1 parent 8e98ed6 commit ec859af

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

release-notes/VERSION

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ Project: jackson-databind
44
=== Releases ===
55
------------------------------------------------------------------------
66

7+
2.7.9.2 (not yet released)
8+
9+
#1607: @JsonIdentityReference not used when setup on class only
10+
(reported by vboulaye@github)
11+
712
2.7.9.1 (18-Apr-2017)
813

914
#1599: Jackson Deserializer security vulnerability

src/main/java/com/fasterxml/jackson/databind/introspect/JacksonAnnotationIntrospector.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,13 @@ public ObjectIdInfo findObjectIdInfo(Annotated ann) {
576576
@Override
577577
public ObjectIdInfo findObjectReferenceInfo(Annotated ann, ObjectIdInfo objectIdInfo) {
578578
JsonIdentityReference ref = _findAnnotation(ann, JsonIdentityReference.class);
579-
if (ref != null) {
580-
objectIdInfo = objectIdInfo.withAlwaysAsId(ref.alwaysAsId());
579+
if (ref == null) {
580+
return objectIdInfo;
581581
}
582-
return objectIdInfo;
582+
if (objectIdInfo == null) {
583+
objectIdInfo = ObjectIdInfo.empty();
584+
}
585+
return objectIdInfo.withAlwaysAsId(ref.alwaysAsId());
583586
}
584587

585588
/*

src/main/java/com/fasterxml/jackson/databind/introspect/ObjectIdInfo.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public class ObjectIdInfo
1919
protected final Class<?> _scope;
2020
protected final boolean _alwaysAsId;
2121

22+
/**
23+
* @since 2.8.9
24+
*/
25+
private final static ObjectIdInfo EMPTY = new ObjectIdInfo(PropertyName.NO_NAME, Object.class, null, false, null);
26+
2227
public ObjectIdInfo(PropertyName name, Class<?> scope, Class<? extends ObjectIdGenerator<?>> gen,
2328
Class<? extends ObjectIdResolver> resolver)
2429
{
@@ -56,6 +61,10 @@ protected ObjectIdInfo(PropertyName prop, Class<?> scope, Class<? extends Object
5661
_resolver = resolver;
5762
}
5863

64+
public static ObjectIdInfo empty() {
65+
return EMPTY;
66+
}
67+
5968
public ObjectIdInfo withAlwaysAsId(boolean state) {
6069
if (_alwaysAsId == state) {
6170
return this;

src/main/java/com/fasterxml/jackson/databind/ser/std/BeanSerializerBase.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -429,15 +429,14 @@ public JsonSerializer<?> createContextual(SerializerProvider provider,
429429
if (objectIdInfo == null) {
430430
// no ObjectId override, but maybe ObjectIdRef?
431431
if (oiw != null) {
432-
objectIdInfo = intr.findObjectReferenceInfo(accessor,
433-
new ObjectIdInfo(NAME_FOR_OBJECT_REF, null, null, null));
434-
oiw = _objectIdWriter.withAlwaysAsId(objectIdInfo.getAlwaysAsId());
432+
objectIdInfo = intr.findObjectReferenceInfo(accessor, null);
433+
if (objectIdInfo != null) {
434+
oiw = _objectIdWriter.withAlwaysAsId(objectIdInfo.getAlwaysAsId());
435+
}
435436
}
436437
} else {
437-
/* Ugh: mostly copied from BeanSerializerBase: but can't easily
438-
* change it to be able to move to SerializerProvider (where it
439-
* really belongs)
440-
*/
438+
// Ugh: mostly copied from BeanDeserializerBase: but can't easily change it
439+
// to be able to move to SerializerProvider (where it really belongs)
441440

442441
// 2.1: allow modifications by "id ref" annotations as well:
443442
objectIdInfo = intr.findObjectReferenceInfo(accessor, objectIdInfo);

src/test/java/com/fasterxml/jackson/databind/objectid/AlwaysAsReferenceFirstTest.java

Lines changed: 52 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
public class AlwaysAsReferenceFirstTest extends BaseMapTest
77
{
8+
// [databind#1255]
89
@JsonPropertyOrder({ "bar1", "bar2" })
910
static class Foo {
1011

@@ -20,16 +21,64 @@ static class Bar {
2021
public int value = 3;
2122
}
2223

24+
// [databind#1607]
25+
26+
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id")
27+
static class Value1607
28+
{
29+
public int value;
30+
31+
public Value1607() { this(0); }
32+
public Value1607(int v) {
33+
value = v;
34+
}
35+
}
36+
@JsonIdentityInfo(generator=ObjectIdGenerators.IntSequenceGenerator.class, property="id")
37+
@JsonIdentityReference(alwaysAsId=true)
38+
static class Value1607ViaClass
39+
{
40+
public int value;
41+
42+
public Value1607ViaClass() { this(0); }
43+
public Value1607ViaClass(int v) {
44+
value = v;
45+
}
46+
}
47+
48+
@JsonPropertyOrder(alphabetic=true)
49+
static class ReallyAlwaysContainer
50+
{
51+
public Value1607ViaClass alwaysClass = new Value1607ViaClass(13);
52+
53+
@JsonIdentityReference(alwaysAsId=true)
54+
public Value1607 alwaysProp = new Value1607(13);
55+
}
56+
57+
/*
58+
/**********************************************************
59+
/* Test methods
60+
/**********************************************************
61+
*/
62+
63+
private final ObjectMapper MAPPER = new ObjectMapper();
64+
65+
// [databind#1255]
2366
public void testIssue1255() throws Exception
2467
{
25-
ObjectMapper mapper = new ObjectMapper();
2668
Foo mo = new Foo();
2769
mo.bar1 = new Bar();
2870
mo.bar2 = mo.bar1;
2971

30-
String json = mapper.writeValueAsString(mo);
72+
String json = MAPPER.writeValueAsString(mo);
3173

32-
Foo result = mapper.readValue(json, Foo.class);
74+
Foo result = MAPPER.readValue(json, Foo.class);
3375
assertNotNull(result);
3476
}
77+
78+
// [databind#1607]
79+
public void testIssue1607() throws Exception
80+
{
81+
String json = MAPPER.writeValueAsString(new ReallyAlwaysContainer());
82+
assertEquals(aposToQuotes("{'alwaysClass':1,'alwaysProp':2}"), json);
83+
}
3584
}

0 commit comments

Comments
 (0)