strapi / strapi

🚀 Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable, and developer-first.
https://strapi.io
Other
63.79k stars 8.13k forks source link

REST API Error on populating component that has a relation back to the content type #21149

Open nurielmeni opened 2 months ago

nurielmeni commented 2 months ago

Bug report

Required System information

Describe the bug

I created a component that is related to User (users-permissions) the name is Children, the purpose is to show relation between users: Schema:

{
  "kind": "collectionType",
  "collectionName": "up_users",
  "info": {
    "name": "user",
    "description": "",
    "singularName": "user",
    "pluralName": "users",
    "displayName": "User"
  },
  "options": {
    "draftAndPublish": false
  },
  "attributes": {
    "username": {
      "type": "string",
    },

    ...

    "children": {
      "type": "component",
      "repeatable": true,
      "component": "account.children"
    }
  }
}

The Children is a repeatable component that has one field "child" that is related to the User (users-permissions). Schema:

{
  "collectionName": "components_account_children",
  "info": {
    "displayName": "Children",
    "icon": "user",
    "description": ""
  },
  "options": {},
  "attributes": {
    "child": {
      "type": "relation",
      "relation": "oneToOne",
      "target": "plugin::users-permissions.user"
    },
    "class": {
      "type": "string",
      "required": true
    },
    ....
  }
}

Steps to reproduce the behavior

I am using the REST API, and fetching the url:

(1)
GET http://localhost:1337/api/users/me
    ?populate=children
    &populate[children][populate][]=child

I am getting error 500: `error: Invalid populate parameter. Expected a string, an array of strings, a populate object

However when fetching the url with the addition of the fields part:

(2)
GET http://localhost:1337/api/users/me
    ?populate=children
    &populate[children][fields][]=*
    &populate[children][populate][]=child

I am getting the expected result, it seem as a bug, that only with the fields mentioned the results return.

Expected behavior

To use the (1) url and get back the user with the related child and the child data all in one fetch as actually happening with the(2) url example.

{
  "id": 1,
  "username": "parent1",
  "email": "parent1@test.com",
  "provider": "local",
  "confirmed": true,
  "blocked": false,
  "createdAt": "2024-09-04T21:34:48.161Z",
  "updatedAt": "2024-09-04T22:01:14.070Z",
  "children": [
    {
      "id": 5,
      "class": "Class1",
      "child": {
        "id": 20,
        "username": "child1",
        "email": "child1@test.com",
        "provider": "local",
        "confirmed": true,
        "blocked": false,
        "createdAt": "2024-09-04T21:33:55.462Z",
        "updatedAt": "2024-09-04T21:33:55.462Z"
      }
    }
  ]
}
laurenskling commented 2 months ago

I don't think it has actually to so with the relation back. all my contentTypes with draftAndPublish: false also fail to populate their relation..