Skip to content

Commit

Permalink
Fix #1255
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Jun 5, 2016
1 parent d15b19f commit 644487b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ public final class WritableObjectId

public Object id;

/**
* Marker to denote whether Object Id value has been written as part of an Object,
* to be referencible. Remains false when forward-reference is written.
*/
protected boolean idWritten = false;

public WritableObjectId(ObjectIdGenerator<?> generator) {
Expand All @@ -27,7 +31,7 @@ public WritableObjectId(ObjectIdGenerator<?> generator) {

public boolean writeAsId(JsonGenerator gen, SerializerProvider provider, ObjectIdWriter w) throws IOException
{
if (id != null && (idWritten || w.alwaysAsId)) {
if ((id != null) && (idWritten || w.alwaysAsId)) {
// 03-Aug-2013, tatu: Prefer Native Object Ids if available
if (gen.canWriteObjectId()) {
gen.writeObjectRef(String.valueOf(id));
Expand All @@ -40,7 +44,13 @@ public boolean writeAsId(JsonGenerator gen, SerializerProvider provider, ObjectI
}

public Object generateId(Object forPojo) {
return (id = generator.generateId(forPojo));
// 04-Jun-2016, tatu: As per [databind#1255], need to consider possibility of
// id being generated for "alwaysAsId", but not being written as POJO; regardless,
// need to use existing id if there is one:
if (id == null) {
id = generator.generateId(forPojo);
}
return id;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.fasterxml.jackson.databind.objectid;

import com.fasterxml.jackson.annotation.*;
import com.fasterxml.jackson.databind.*;

public class AlwaysAsReferenceFirstTest extends BaseMapTest
{
@JsonPropertyOrder({ "bar1", "bar2" })
static class Foo {

@JsonIdentityReference(alwaysAsId = true)
public Bar bar1;

@JsonIdentityReference
public Bar bar2;
}

@JsonIdentityInfo(generator = ObjectIdGenerators.IntSequenceGenerator.class)
static class Bar {
public int value = 3;
}

public void testIssue1255() throws Exception
{
ObjectMapper mapper = new ObjectMapper();
Foo mo = new Foo();
mo.bar1 = new Bar();
mo.bar2 = mo.bar1;

String json = mapper.writeValueAsString(mo);

Foo result = mapper.readValue(json, Foo.class);
assertNotNull(result);
}
}

0 comments on commit 644487b

Please sign in to comment.