strapi-community / strapi-plugin-slugify

A plugin for Strapi Headless CMS that provides the ability to auto slugify a field for any content type.
https://market.strapi.io/plugins/strapi-plugin-slugify
MIT License
45 stars 21 forks source link

When calling the `slugify/slug` endpoint with populate query values the data structure does not match the `findOne` endpoint. #40

Closed selected-pixel-jameson closed 2 years ago

selected-pixel-jameson commented 2 years ago

If I pass 'populate' values into the query for relational values or component lists the values that come back do not have the same structure as the 'findOne' endpoint.

For example if I call `/api/:model/:id?populate[0]=owner I get the following structure

{
    "data": {
        "id": 45,
        "attributes": {
            "title": "Test Jam 1",
            "createdAt": "2022-02-28T21:26:16.504Z",
            "updatedAt": "2022-03-14T15:59:39.206Z",
            "publishedAt": "2022-02-28T21:26:16.478Z",
            "streamId": null,
            "status": null,
            "isPublic": false,
            "slug": "test-jam-1",
            "deletedOn": null,
            "deleted": false,
            "owner": {
                "data": {
                    "id": 1,
                    "attributes": {
                        "username": "selectedpixel",
                        "email": "xxxxxxxx@test.com",
                        "provider": "local",
                        "confirmed": true,
                        "blocked": false,
                        "createdAt": "2022-02-03T11:12:57.855Z",
                        "updatedAt": "2022-02-08T13:53:28.715Z",
                        "country": null
                    }
                }
            }
        }
    },
    "meta": {}
}

If I make the same request using the slugify/slug endpoint I get the following:

{
    "data": {
        "id": 45,
        "attributes": {
            "title": "Test Jam 1",
            "createdAt": "2022-02-28T21:26:16.504Z",
            "updatedAt": "2022-03-14T15:59:39.206Z",
            "publishedAt": "2022-02-28T21:26:16.478Z",
            "streamId": null,
            "status": null,
            "isPublic": false,
            "slug": "test-jam-1",
            "deletedOn": null,
            "deleted": false,
            "owner": {
                "id": 1,
                "username": "selectedpixel",
                "email": "xxxxxxxx@test.com",
                "provider": "local",
                "confirmed": true,
                "blocked": false,
                "createdAt": "2022-02-03T11:12:57.855Z",
                "updatedAt": "2022-02-08T13:53:28.715Z",
                "country": null
            }
        }
    },
    "meta": {}
}

Notice how the data and attributes fields are missing on the owner when using the slugify/slug endpoint? This will cause the need to create a completely separate response models on the client side for this endpoint vs. the standard findOne endpoint.

selected-pixel-jameson commented 2 years ago

Looks like this is being done when calling transformResponse. I'm not sure if this is intentional and or why Strapi API's are not doing this.

https://github.com/strapi/strapi/blob/master/packages/core/strapi/lib/core-api/controller/transform.js

selected-pixel-jameson commented 2 years ago

Ok. I think I see what is happening here. It looks like you have another project https://github.com/ComfortablyCoding/strapi-plugin-transformer

Which is allowing you to set the responses to be transformed, which I like. However I think that the same option should be included in this repo.

selected-pixel-jameson commented 2 years ago

So I implemented the plugin-transformer. However, that doesn't really solve the problem because that still returned the data wrapper in nested relationships. Where as this response removes the data wrapper as well. So there are still differences between API responses.

ComfortablyCoding commented 2 years ago

Hello @selected-pixel-jameson , thanks for the report. The transform function I am using is from strapi core not from my transformer plugin.

You are indeed correct in that the response format is incorrect at the moment. I have found what I believe to be the issue and will push a fix.

ComfortablyCoding commented 2 years ago

This will be fixed in the next release.