kubb-labs / kubb

The ultimate toolkit for working with APIs.
https://kubb.dev
MIT License
676 stars 56 forks source link

Path parameters generated with faker do not have "randexp" imported into their file #1072

Closed TangoYankee closed 3 months ago

TangoYankee commented 3 months ago

What version of kubb is running?

2.23.0

What platform is your computer?

MacOS

What version of external packages are you using(@tanstack-query, MSW, React, Vue, ...)

"dependencies": { "@kubb/swagger-client": "^2.23.0", "@remix-run/node": "^2.7.2", "@remix-run/react": "^2.7.2", "@remix-run/serve": "^2.7.2", "axios": "^1.6.7", "framer-motion": "^6.5.1", "isbot": "^4.1.0", "react": "^18.2.0", "react-dom": "^18.2.0", "remix-utils": "^7.5.0", "zod": "^3.22.4" }, "devDependencies": { "@faker-js/faker": "^8.4.1", "@kubb/cli": "^2.23.0", "@kubb/core": "^2.23.0", "@kubb/swagger": "^2.23.0", "@kubb/swagger-faker": "^2.23.0", "@kubb/swagger-msw": "^2.23.0", "@kubb/swagger-ts": "^2.23.0", "@kubb/swagger-zod": "^2.23.0", "@playwright/test": "^1.42.1", "@remix-run/dev": "^2.7.2", "@remix-run/testing": "^2.7.2", "@testing-library/jest-dom": "^6.4.2", "@testing-library/react": "^14.2.1", "@testing-library/user-event": "^14.5.2", "@types/node": "^20.11.25", "@types/react": "^18.2.20", "@types/react-dom": "^18.2.7", "husky": "^9.0.11", "jsdom": "^24.0.0", "msw": "^2.2.2", "randexp": "^0.5.3", "typescript": "^5.1.6", "vite": "^5.1.7", "vitest": "^1.3.1" },

What steps can reproduce the bug?

Ticket #1067 added an option to use 'randexp' as the regex generator for swagger-faker. The feature itself is working as intended. However, generated files for path parameters do not have randexp imported.

The example below creates 'managingCode' and 'capitalProjectId' path parameters. The 'managingCode' parameter is generated with 'randexp'. The generation itself is correct. However, the file does not import 'RandExp' and the function fails to run.

import { faker } from "@faker-js/faker";
import type {
  FindCapitalCommitmentsByManagingCodeCapitalProjectIdPathParams,
} from "../types/FindCapitalCommitmentsByManagingCodeCapitalProjectId";

export function createFindCapitalCommitmentsByManagingCodeCapitalProjectIdPathParams(): NonNullable<FindCapitalCommitmentsByManagingCodeCapitalProjectIdPathParams> {
  return {
    managingCode: faker.helpers.arrayElement<any>([
      faker.string.alpha(),
      new RandExp("^([0-9]{3})$").gen(),
    ]),
    capitalProjectId: faker.string.alpha(),
  };
}

How often does this bug happen?

Every time

What is the expected behavior?

The example below shows RandExp being used for a schema. In this snippet, 'randexp' is imported and available for use. The path parameters file is expected to also import 'randexp', as shown in the example

import { createCapitalProjectCategory } from "./createCapitalProjectCategory";
import { faker } from "@faker-js/faker";
import RandExp from "randexp";
import type { CapitalProject } from "../types/CapitalProject";

export function createCapitalProject(
  data: NonNullable<Partial<CapitalProject>> = {},
): NonNullable<CapitalProject> {
  return {
    ...{
      id: faker.string.alpha(),
      description: faker.string.alpha(),
      managingCode: faker.helpers.arrayElement<any>([
        faker.string.alpha(),
        new RandExp("^([0-9]{3})$").gen(),
      ]),
      managingAgency: faker.string.alpha(),
      minDate: faker.date.anytime().toString(),
      maxDate: faker.date.anytime().toString(),
      category: createCapitalProjectCategory(),
    },
    ...data,
  };
}

Swagger/OpenAPI file?

openapi.yaml

Additional information

The lack of import was noticed by running a typecheck on the 42/faker-randexp branch of our project, which includes the impacted app/gen/mocks folder.

The "path parameters" error code snippet is from createFindCapitalCommitmentsByManagingCodeCapitalProjectId.ts

The "schema" expected code snippet is from createCapitalProject.ts

stijnvanhulle commented 3 months ago

v2.23.1 will also add the import to operationSchemas and will resolve your issue.

TangoYankee commented 3 months ago

Thank you again!