Repository: django-json-api/django-rest-framework-json-api

Issue:
Included compound documents are being included recursively
On a model having a relation with itself, including the relation will have the consequence of including the fields in the relation itself.

http://localhost:8000/api/contacts/organizations/534?format=vnd.api%2Bjson&include=resellers,people&fields[organization]=resellers,people&fields[person]=full_name

``` json
{
    "data": {
        "type": "organization",
        "id": "534",
        "attributes": {},
        "relationships": {
            "resellers": {
                "data": [
                    {
                        "type": "organization",
                        "id": "4"
                    }
                ],
                "links": {
                    "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/534/relationships/resellers?format=vnd.api%2Bjson",
                    "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/534/resellers?format=vnd.api%2Bjson"
                },
                "meta": {
                    "count": 1
                }
            },
            "people": {
                "data": [
                    {
                        "type": "person",
                        "id": "628"
                    },
                    {
                        "type": "person",
                        "id": "1022"
                    }
                ],
                "links": {
                    "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/534/relationships/people?format=vnd.api%2Bjson",
                    "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/534/people?format=vnd.api%2Bjson"
                },
                "meta": {
                    "count": 2
                }
            }
        },
        "links": {
            "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/534?format=vnd.api%2Bjson"
        }
    },
    "included": [
        {
            "type": "organization",
            "id": "4",
            "attributes": {},
            "relationships": {
                "resellers": {
                    "data": [
                        {
                            "type": "organization",
                            "id": "504"
                        }
                    ],
                    "links": {
                        "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/4/relationships/resellers?format=vnd.api%2Bjson",
                        "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/4/resellers?format=vnd.api%2Bjson"
                    },
                    "meta": {
                        "count": 1
                    }
                },
                "people": {
                    "data": [
                        {
                            "type": "person",
                            "id": "111"
                        },
                        {
                            "type": "person",
                            "id": "1783"
                        },
                        {
                            "type": "person",
                            "id": "1985"
                        },
                        {
                            "type": "person",
                            "id": "648"
                        },
                        {
                            "type": "person",
                            "id": "421"
                        },
                        {
                            "type": "person",
                            "id": "353"
                        }
                    ],
                    "links": {
                        "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/4/relationships/people?format=vnd.api%2Bjson",
                        "related": "http://api-dev.zerofail.com:8080/api/contacts/organizations/4/people?format=vnd.api%2Bjson"
                    },
                    "meta": {
                        "count": 6
                    }
                }
            },
            "links": {
                "self": "http://api-dev.zerofail.com:8080/api/contacts/organizations/4?format=vnd.api%2Bjson"
            }
        },
        {
            "type": "person",
            "id": "1022",
            "attributes": {
                "full_name": "Neppie Abernathy"
            },
            "links": {
                "self": "http://api-dev.zerofail.com:8080/api/contacts/people/1022?format=vnd.api%2Bjson"
            }
        },
        {
            "type": "person",
            "id": "111",
            "attributes": {
                "full_name": "Mrs. Ernestine Bartoletti"
            },
            "links": {
                "self": "http://api-dev.zerofail.com:8080/api/contacts/people/111?format=vnd.api%2Bjson"
            }
        },
        {
            "type": "person",
            "id": "1783",
            "attributes": {
                "full_name": "Adrain Franecki"
            },
            "links": {
                "self": "http://api-dev.zerofail.com:8080/api/contacts/people/1783?format=vnd.api%2Bjson"
            }
        },
        {
            "type": "person",
            "id": "1985",
            "attributes": {
                "full_name": "Mr. Dandre Haley"
            },
            "links": {
                "self": "http://api-dev.zerofail.com:8080/api/contacts/people/1985?format=vnd.api%2Bjson"
            }
        },
        {
            "type": "person",
            "id": "353",
            "attributes": {
                "full_name": "Mrs. Harlene VonRueden"
            },
            "links": {
                "self": "http://api-dev.zerofail.com:8080/api/contacts/people/353?format=vnd.api%2Bjson"
            }
        },
        {
            "type": "person",
            "id": "421",
            "attributes": {
                "full_name": "Miss Jasmin Kutch"
            },
            "links": {
                "self": "http://api-dev.zerofail.com:8080/api/contacts/people/421?format=vnd.api%2Bjson"
            }
        },
        {
            "type": "person",
            "id": "628",
            "attributes": {
                "full_name": "Dr. Beckham Abernathy"
            },
            "links": {
                "self": "http://api-dev.zerofail.com:8080/api/contacts/people/628?format=vnd.api%2Bjson"
            }
        },
        {
            "type": "person",
            "id": "648",
            "attributes": {
                "full_name": "Creed Kuhn"
            },
            "links": {
                "self": "http://api-dev.zerofail.com:8080/api/contacts/people/648?format=vnd.api%2Bjson"
            }
        }
    ]
}
```



Discussion:

Resolving patch:
diff --git a/rest_framework_json_api/utils.py b/rest_framework_json_api/utils.py
--- a/rest_framework_json_api/utils.py
+++ b/rest_framework_json_api/utils.py
@@ -25,6 +25,7 @@
     HyperlinkedRouterField = type(None)
 
 import django
+
 if django.VERSION < (1, 7):
     from django.utils.module_loading import import_by_path as import_class_from_dotted_path
 else:
@@ -434,7 +435,9 @@ def extract_included(fields, resource, resource_instance, included_resources):
             # For ManyRelatedFields if `related_name` is not set we need to access `foo_set` from `source`
             relation_instance_or_manager = getattr(resource_instance, field.child_relation.source)
 
-        new_included_resources = [key.replace('%s.' % field_name, '', 1) for key in included_resources]
+        new_included_resources = [key.replace('%s.' % field_name, '', 1)
+                                  for key in included_resources
+                                  if field_name == key.split('.')[0]]
         serializer_data = resource.get(field_name)
 
         if isinstance(field, ManyRelatedField):


Test patch: