package net.apnic.rdapd.rdap.json;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.ser.BeanPropertyWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.PropertyWriter;
import com.fasterxml.jackson.databind.SerializerProvider;

public class RelatedEntityFilter
    extends SimpleBeanPropertyFilter
{
    private static final int MAX_DEPTH = 1;

    private final ThreadLocal<Integer> entityDepthCount;

    public RelatedEntityFilter()
    {
        entityDepthCount = new ThreadLocal<Integer>()
        {
            @Override
            protected Integer initialValue()
            {
                return 0;
            }
        };
    }
    @Override
    protected boolean include(BeanPropertyWriter writer)
    {
        return true;
    }

    @Override
    protected boolean include(PropertyWriter writer)
    {
        return true;
    }

    @Override
    public void serializeAsField(Object pojo, JsonGenerator jgen,
        SerializerProvider provider, PropertyWriter writer)
        throws Exception
    {
        if(writer.getName().equals("entities") && entityDepthCount.get().equals(MAX_DEPTH))
        {
            entityDepthCount.set(0);
            return;
        }
        else if(writer.getName().equals("entities"))
        {
            entityDepthCount.set(entityDepthCount.get().intValue() + 1);
        }
        writer.serializeAsField(pojo, jgen, provider);
    }
}

@@ -78,16 +78,69 @@
 erator;%0A
+import com.fasterxml.jackson.core.JsonStreamContext;%0A
 import c
@@ -464,137 +464,210 @@
    p
-rivate static final int MAX_DEPTH = 1;%0A%0A    private final ThreadLocal%3CInteger%3E entityDepthCount;%0A%0A    public RelatedEntityFil
+ublic RelatedEntityFilter() %7B%7D%0A%0A    @Override%0A    protected boolean include(BeanPropertyWriter writer)%0A    %7B%0A        return true;%0A    %7D%0A%0A    @Override%0A    protected boolean include(PropertyWriter wri
 ter
-(
 )%0A  
@@ -682,62 +682,97 @@
     
-entityDepthCount = new ThreadLocal%3CInteger%3E()%0A  
+return true;%0A    %7D%0A%0A    private String getGrandparentContextName(JsonGenerator jgen)%0A
     
-  
 %7B%0A  
@@ -781,66 +781,143 @@
     
-    @Override%0A            protected Integer initialValue
+JsonStreamContext jsonStreamContext = jgen.getOutputContext();%0A        JsonStreamContext parentContext = jsonStreamContext.getParent
 ()
+;
 %0A   
@@ -917,34 +917,53 @@
 ();%0A        
-    %7B%0A    
+if (parentContext == null) %7B%0A
             
@@ -973,15 +973,14 @@
 urn 
-0;%0A    
+null;%0A
     
@@ -997,94 +997,120 @@
     
-%7D;%0A    %7D%0A    @Override%0A    protected boolean include(BeanPropertyWriter writer)
+JsonStreamContext grandparentContext = parentContext.getParent();%0A        if (grandparentContext == null) %7B
 %0A    
-%7B%0A
     
@@ -1124,113 +1124,74 @@
 urn 
-true;%0A
+null;%0A   
     
+ 
 %7D
-%0A
 %0A    
-@Override%0A    protected boolean include(PropertyWriter writer)%0A    %7B%0A        r
+    return grandparentContext.g
 et
+C
 ur
-n true
+rentName()
 ;%0A  
@@ -1362,24 +1362,83 @@
 ption%0A    %7B%0A
+        /* Do not include nested entities in responses. */%0A
         if(w
@@ -1435,16 +1435,17 @@
       if
+ 
 (writer.
@@ -1476,98 +1476,86 @@
 es%22)
- && entityDepthCount.get().equals(MAX_DEPTH))%0A        %7B%0A            entityDepth
+) %7B%0A            String grandparentContextName = getGrandparent
 Co
-u
 nt
-.set(0
+extName(jgen
 );%0A 
@@ -1569,15 +1569,42 @@
     
-return;
+if (grandparentContextName != null
 %0A   
@@ -1608,26 +1608,24 @@
         
-%7D%0A
         
 else if(
@@ -1620,32 +1620,37 @@
     
-else if(writer.ge
+    && grandparentContex
 tName
-()
 .equ
@@ -1665,16 +1665,18 @@
 ities%22))
+ %7B
 %0A       
@@ -1680,82 +1680,37 @@
     
-%7B%0A
+   
      
-       entityDepthCount.set(entityDepthCount.get().intValue() + 1);
+return;%0A            %7D
 %0A   
@@ -1708,32 +1708,33 @@
     %7D%0A        %7D%0A
+%0A
         writer.s

fix nested entity problem