Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Types for ctx.query in custom controller appear to be broken #20230

Open
derrickmehaffy opened this issue Apr 29, 2024 · 1 comment
Open

Types for ctx.query in custom controller appear to be broken #20230

derrickmehaffy opened this issue Apr 29, 2024 · 1 comment
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around source: typescript Source is related to TypeScript (typings, tooling, ...) status: confirmed Confirmed by a Strapi Team member or multiple community members version: 5

Comments

@derrickmehaffy
Copy link
Member

Bug report

Required System information

  • Node.js version: v20
  • Yarn version: v1
  • Strapi version: 5.0.0-beta.5
  • Database: SQLite
  • Operating system: Linux Mint 21.3
  • Is your project Javascript or Typescript: Javascript

Describe the bug

Attempting to play around with custom controllers in Strapi 5 I was attempting to set a custom ctx.query and noticed that type errors are being thrown around the structure of ctx.query

Steps to reproduce the behavior

  1. Create a custom route
  2. Create a custom controller
  3. Attempt to manually set the value of ctx.query
  4. Request the custom controller
  5. See error

Expected behavior

No breaking changes were recorded for the object based populate syntax thus this was valid in Strapi 4 and should also be valid in Strapi 5

Screenshots

image

image

Code snippets

Route:

module.exports = {
  routes: [
    {
      method: "GET",
      path: "/tests/custom/example",
      handler: "api::test.test.exampleAction",
    },
  ],
};

Controller:

"use strict";

/**
 * test controller
 */

const { createCoreController } = require("@strapi/strapi").factories;

module.exports = createCoreController("api::test.test", ({ strapi }) => ({
  async exampleAction(ctx) {
    await this.validateQuery(ctx);
    const defaultQuery = {
      populate: {
        createdBy: {
          fields: ["id", "username"],
        },
        updatedBy: {
          fields: ["id", "username"],
        },
      },
    };

    if (!ctx.query) {
      ctx.query = defaultQuery;
    }

    const sanitizedQuery = await this.sanitizeQuery(ctx);
    const entities = await strapi
      .documents("api::test.test")
      .findMany(sanitizedQuery);

    const sanitizedData = await this.sanitizeOutput(entities, ctx);

    try {
      return this.transformResponse(sanitizedData);
    } catch (err) {
      return err;
    }
  },
}));

Schema:

{
  "kind": "collectionType",
  "collectionName": "tests",
  "info": {
    "singularName": "test",
    "pluralName": "tests",
    "displayName": "test"
  },
  "options": {
    "comment": "",
    "populateCreatorFields": true
  },
  "attributes": {
    "test": {
      "type": "string"
    }
  }
}

Additional context

N/A

@derrickmehaffy derrickmehaffy added issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around status: confirmed Confirmed by a Strapi Team member or multiple community members source: typescript Source is related to TypeScript (typings, tooling, ...) version: 5 labels Apr 29, 2024
@derrickmehaffy
Copy link
Member Author

Related to: #18526

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
issue: bug Issue reporting a bug severity: medium If it breaks the basic use of the product but can be worked around source: typescript Source is related to TypeScript (typings, tooling, ...) status: confirmed Confirmed by a Strapi Team member or multiple community members version: 5
Projects
Status: To be reviewed (Open)
Status: Backlog
Status: To review
Development

No branches or pull requests

1 participant