The response of the findOne operation is missing the "attributes" field

System Information
  • Strapi Version: 4.11.1
  • Operating System:
  • Database:
  • Node Version: 18.16
  • NPM Version:
  • Yarn Version:

I created a custom controller and used the findOne API to retrieve an article from the resource. However, the response is not the same as the response from the ‘/articles/:id’ endpoint.

All of the default Strapi controllers return an Object with the ‘attributes’ field, but the controller I created does not have the ‘attributes’ field. How can I make them the same?

// strapi controller
{
  "data": {
    "id": 3,
    "attributes": {
      "title": "Title",
      "content": "Content",
      "description": null,
      "publishTime": null,
      "createdAt": "2023-07-11T03:26:09.261Z",
      "updatedAt": "2023-07-11T03:26:31.010Z",
      "subtitle": null,
      "thumb": {
        "data": null
      },
      "category": {
        "data": {
          "id": 1,
          "attributes": {
            "title": "",
            "description": null,
            "createdAt": "2023-07-09T14:16:44.621Z",
            "updatedAt": "2023-07-09T14:16:44.621Z"
          }
        }
      }
    }
  },
  "meta": {}
}
// custom controller
{
  "id": 3,
  "title": "Title",
  "content": "Content",
  "description": null,
  "publishTime": null,
  "createdAt": "2023-07-11T03:26:09.261Z",
  "updatedAt": "2023-07-11T03:26:31.010Z",
  "subtitle": null,
  "thumb": null,
  "category": {
    "id": 1,
    "title": "",
    "description": null,
    "createdAt": "2023-07-09T14:16:44.621Z",
    "updatedAt": "2023-07-09T14:16:44.621Z"
  }
}

The REST API and the Custom Query API will return different response but the results will be the same. The Query Api won’t include data > attributes object inside response.

Thanks. Is there an official function or method to ensure that the response from a custom controller in Strapi is consistent with the default controllers?

Not that I’m aware of.