1 package com.fasterxml.jackson.failing; 2 3 import java.util.concurrent.ConcurrentHashMap; 4 5 import com.fasterxml.jackson.annotation.*; 6 7 import com.fasterxml.jackson.databind.*; 8 import com.fasterxml.jackson.databind.annotation.JsonDeserialize; 9 import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; 10 11 public class ObjectIdWithBuilder1496Test extends BaseMapTest 12 { 13 @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") 14 @JsonDeserialize(builder=POJOBuilder.class) 15 static class POJO 16 { 17 private long id; getId()18 public long getId() { return id; } 19 private int var; getVar()20 public int getVar() { return var; } POJO(long id, int var)21 private POJO (long id, int var) { this.id = id; this.var = var; } 22 23 @Override toString()24 public String toString() { return "id: " + id + ", var: " + var; } 25 } 26 27 @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id") 28 @JsonPOJOBuilder(withPrefix = "", buildMethodName="readFromCacheOrBuild") 29 static final class POJOBuilder { 30 // Standard builder stuff 31 private long id; 32 private int var; 33 id(long _id)34 public POJOBuilder id(long _id) { id = _id; return this; } var(int _var)35 public POJOBuilder var(int _var) { var = _var; return this; } 36 build()37 public POJO build() { return new POJO(id, var); } 38 39 // Special build method for jackson deserializer that caches objects already deserialized 40 private final static ConcurrentHashMap<Long, POJO> cache = new ConcurrentHashMap<>(); readFromCacheOrBuild()41 public POJO readFromCacheOrBuild() { 42 POJO pojo = cache.get(id); 43 if (pojo == null) { 44 POJO newPojo = build(); 45 pojo = cache.putIfAbsent(id, newPojo); 46 if (pojo == null) { 47 pojo = newPojo; 48 } 49 } 50 return pojo; 51 } 52 } 53 54 /* 55 /********************************************************** 56 /* Test methods 57 /********************************************************** 58 */ 59 60 private final ObjectMapper MAPPER = newJsonMapper(); 61 testBuilderId1496()62 public void testBuilderId1496() throws Exception 63 { 64 POJO input = new POJOBuilder().id(123L).var(456).build(); 65 String json = MAPPER.writeValueAsString(input); 66 POJO result = MAPPER.readValue(json, POJO.class); 67 assertNotNull(result); 68 } 69 } 70