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

Swagger-faker pattern incorrectly applies slashes around regex #844

Closed
editedredx opened this issue Mar 6, 2024 · 1 comment · Fixed by #848
Closed

Swagger-faker pattern incorrectly applies slashes around regex #844

editedredx opened this issue Mar 6, 2024 · 1 comment · Fixed by #848
Labels
bug Something isn't working

Comments

@editedredx
Copy link

editedredx commented Mar 6, 2024

What version of kubb is running?

kubb/2.6.5 win32-x64 node-v18.14.0

What platform is your computer?

Windows

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

"@faker-js/faker": "^8.4.1",     "@kubb/cli": "^2.6.5",     "@kubb/core": "^2.6.5",     "@kubb/swagger": "^2.6.5",     "@kubb/swagger-client": "^2.6.5",     "@kubb/swagger-tanstack-query": "^2.6.5",     "@kubb/swagger-ts": "^2.6.5",     "@mswjs/http-middleware": "^0.9.2",     "@tanstack/react-query": "^5.24.1",     "axios": "^1.6.7",     "msw": "^2.2.2",     "ts-node": "^10.9.2",     "@kubb/swagger-faker": "^2.6.5",     "@kubb/swagger-msw": "^2.6.5"

What steps can reproduce the bug?

When using the following simple config to create MSW mocks with the spec below:

import { defineConfig } from "@kubb/core";
import createSwagger from "@kubb/swagger";
import createSwaggerFaker from "@kubb/swagger-faker";
import createSwaggerMsw from "@kubb/swagger-msw";
import createSwaggerTS from "@kubb/swagger-ts";

export default defineConfig({
  root: ".",
  input: {
    path: "./test-spec.yaml",
  },
  output: {
    path: "./dist",
  },
  plugins: [
    createSwagger({ output: false }),
    createSwaggerTS({}),
    createSwaggerFaker({}),
    createSwaggerMsw({
      output: {
        path: "./mocks",
      },
    }),
  ],
});

This will create the mocks for an MSW server, if you start one up and visit http://localhost:9090/test you get something like this:

{
    "nationalityCode": "/^ZF$/"
}

How often does this bug happen?

Every time

What is the expected behavior?

I would expect the regex characters not to appear in the mocked output.

{
    "nationalityCode": "ZF"
}

Swagger/OpenAPI file?

openapi: 3.0.0
info:
  title: test API
  version: 1.0.0

paths:
  /test:
    get:
      operationId: getTest
      responses:
        "200":
          description: Successful operation
          content:
            application/json:
              schema:
                type: object
                properties:
                  nationalityCode:
                    type: string
                    pattern: "^[A-Z]{2}$"

Additional information

I noticed the slashes are wrapped around the value by this lib which I'm not sure why (https://github.com/kubb-project/kubb/blob/main/packages/swagger-faker/src/FakerGenerator.ts#L334).

Could there be a configurable way to clean up patterns before they are placed inside the mock? For this example I would like to remove the ^ and $ since Faker will just leave those in place. They do make sense in the source yaml so I wouldn't want to change that.

@editedredx editedredx added the bug Something isn't working label Mar 6, 2024
@editedredx
Copy link
Author

While digging in the source code I found the undocumented feature of transforming the schema via de configuration and created the following workaround:

import { defineConfig } from "@kubb/core";
import createSwagger from "@kubb/swagger";
import createSwaggerFaker from "@kubb/swagger-faker";
import createSwaggerMsw from "@kubb/swagger-msw";
import createSwaggerTS from "@kubb/swagger-ts";

export default defineConfig({
  root: ".",
  input: {
    path: "./test-spec.yaml",
  },
  output: {
    path: "./dist",
  },
  plugins: [
    createSwagger({ output: false }),
    createSwaggerTS({}),
    createSwaggerFaker({
      transformers: {
        schema(schema) {
          if (
            schema.type &&
            schema.pattern &&
            typeof schema.pattern === "string"
          ) {
            return [
              {
                keyword: "matches",
                args: schema.pattern.replace(/(^\^)|(\$$)/g, ""),
              },
            ];
          }
          return undefined;
        },
      },
    }),
    createSwaggerMsw({
      output: {
        path: "./mocks",
      },
    }),
  ],
});

While this does create a workaround I think at least the wrapping of slashes should not happen.

@stijnvanhulle stijnvanhulle linked a pull request Mar 8, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant