cloudcreativity / laravel-json-api

JSON API (jsonapi.org) package for Laravel applications.
http://laravel-json-api.readthedocs.io/en/latest/
Apache License 2.0
778 stars 109 forks source link

Self-relation doesn't include model fields #622

Closed IlyaKhalizov closed 3 years ago

IlyaKhalizov commented 3 years ago

Hi!

Schema:


    {
        return [
            ID::make(),
            Number::make('parentId')->sortable(),
            Str::make('name')->sortable(),
            BelongsTo::make('parent')->type('categories'),
            HasMany::make('product-models'),
            SoftDelete::make('deletedAt'),
            DateTime::make('createdAt')->sortable()->readOnly(),
            DateTime::make('updatedAt')->sortable()->readOnly(),
        ];
    }

 Returns via /api/v1/categories?include=parent

{
         "type":"categories",
         "id":"100001",
         "attributes":{
            "parentId":100000,
            "name":"Anesthetic Accessories",
            "deletedAt":null,
            "createdAt":"2021-08-30T07:30:55.000000Z",
            "updatedAt":"2021-08-30T07:30:55.000000Z"
         },
         "relationships":{
            "parent":{
               "links":{
                  "related":"http:\/\/api.local\/api\/v1\/categories\/100001\/parent",
                  "self":"http:\/\/api.local\/api\/v1\/categories\/100001\/relationships\/parent"
               },
               "data":{
                  "type":"categories",
                  "id":"100000"
               }
            },
            "product-models":{
               "links":{
                  "related":"http:\/\/api.local\/api\/v1\/categories\/100001\/product-models",
                  "self":"http:\/\/api.local\/api\/v1\/categories\/100001\/relationships\/product-models"
               }
            }
         },
         "links":{
            "self":"http:\/\/api.local\/api\/v1\/categories\/100001"
         }
      },

But when I use specific id in the request, like /api/v1/categories/100004?include=parent, I get "included" section

 {
   "jsonapi":{
      "version":"1.0"
   },
   "links":{
      "self":"http:\/\/api.local\/api\/v1\/categories\/100004"
   },
   "data":{
      "type":"categories",
      "id":"100004",
      "attributes":{
         "parentId":100000,
         "name":"Syringes",
         "deletedAt":null,
         "createdAt":"2021-08-30T07:30:55.000000Z",
         "updatedAt":"2021-08-30T07:30:55.000000Z"
      },
      "relationships":{
         "parent":{
            "links":{
               "related":"http:\/\/api.local\/api\/v1\/categories\/100004\/parent",
               "self":"http:\/\/api.local\/api\/v1\/categories\/100004\/relationships\/parent"
            },
            "data":{
               "type":"categories",
               "id":"100000"
            }
         },
         "product-models":{
            "links":{
               "related":"http:\/\/api.local\/api\/v1\/categories\/100004\/product-models",
               "self":"http:\/\/api.local\/api\/v1\/categories\/100004\/relationships\/product-models"
            }
         }
      },
      "links":{
         "self":"http:\/\/api.local\/api\/v1\/categories\/100004"
      }
   },
   "included":[
      {
         "type":"categories",
         "id":"100000",
         "attributes":{
            "parentId":null,
            "name":"Anesthetic",
            "deletedAt":null,
            "createdAt":"2021-08-30T07:30:55.000000Z",
            "updatedAt":"2021-08-30T07:30:55.000000Z"
         },
         "relationships":{
            "parent":{
               "links":{
                  "related":"http:\/\/api.local\/api\/v1\/categories\/100000\/parent",
                  "self":"http:\/\/api.local\/api\/v1\/categories\/100000\/relationships\/parent"
               }
            },
            "product-models":{
               "links":{
                  "related":"http:\/\/api.local\/api\/v1\/categories\/100000\/product-models",
                  "self":"http:\/\/api.local\/api\/v1\/categories\/100000\/relationships\/product-models"
               }
            }
         },
         "links":{
            "self":"http:\/\/api.local\/api\/v1\/categories\/100000"
         }
      }
   ]
}

Can you help, please.
lindyhopchris commented 3 years ago

In JSON:API relationships contain relationship identifiers - i.e. only the type/id of the related resource. To get the attributes, you have to use the include query parameter, which will add the full resource to the included top-level member of the JSON document.

I think that answers your question. If not, please re-open the issue and provide more detail about what you're asking!

lindyhopchris commented 3 years ago

PS: This issue is in the wrong repository - if you do need to re-open the issue, please open it in laravel-json-api/laravel.

IlyaKhalizov commented 3 years ago

@lindyhopchris Sorry, maybe I was miss understood, I do not get "included" section when my query is like via /api/v1/categories?include=parent, do we speak about same things?

lindyhopchris commented 3 years ago

Oh right sorry, I was confused by the formatting in the issue description.

What you're trying to do is fully unit tested, so isn't a bug with this package. Is there a scenario where the parent relationship could have a relationship value but the inverse model doesn't exist?

This will be pretty hard for me to debug in your application so you're probably going to need to debug it yourself. If you have more information to give, please can you open an issue in the correct repository?

IlyaKhalizov commented 3 years ago

Thank you, we will try to debug and if find unresolvable situation let you know.