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

Duplicate function name in msw when route depth is one, e.g. /NotWorking #1106

Closed
SetupCoding opened this issue Dec 8, 2023 · 2 comments · Fixed by #1217
Closed

Duplicate function name in msw when route depth is one, e.g. /NotWorking #1106

SetupCoding opened this issue Dec 8, 2023 · 2 comments · Fixed by #1217
Assignees
Labels
bug Something isn't working
Milestone

Comments

@SetupCoding
Copy link

SetupCoding commented Dec 8, 2023

What are the steps to reproduce this issue?

  1. Using this swagger.json as an example
{
  "openapi": "3.0.0",
  "info": {
    "title": "API Documentation",
    "version": "1.0.0",
    "description": "API documentation for selected endpoints.",
    "license": { "name": "ISC" }
  },
  "paths": {
    "/NotWorking": {
      "get": {
        "operationId": "NotWorking",
        "responses": {
          "200": {
            "description": "Ok",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/NotWorkingResponse" }
              }
            }
          }
        },
        "description": "NotWorking route returning JWT token",
        "tags": ["NotWorking"]
      }
    },
    "/Working/BecauseHasSecondRoute": {
      "get": {
        "operationId": "GetWorkingsSecondRoute",
        "responses": {
          "200": {
            "description": "Ok",
            "content": {
              "application/json": {
                "schema": { "items": { "$ref": "#/components/schemas/Working" }, "type": "array" }
              }
            }
          }
        },
        "description": "Working fetching route",
        "tags": ["Working"]
      }
    }
  },
  "components": {
    "schemas": {
      "NotWorkingRequest": {
        "type": "object",
        "properties": {
          "username": {
            "type": "string"
          },
          "password": {
            "type": "string"
          }
        },
        "required": ["username", "password"]
      },
      "NotWorkingResponse": {
        "type": "object",
        "properties": {
          "accessToken": {
            "type": "string"
          }
        }
      },
      "Working": {
        "type": "object",
        "properties": {
          "WorkingCode": {
            "type": "string"
          },
          "amount": {
            "type": "number"
          },
          "validFrom": {
            "type": "string",
            "format": "date-time"
          },
          "validTo": {
            "type": "string",
            "format": "date-time"
          }
        }
      }
    }
  },
  "servers": [{ "url": "http://localhost:8080", "description": "Local development" }]
}

image

  1. Run Orval with this config:
export default defineConfig({
  webapp: {
    output: {
      mode: 'tags-split',
      target: 'src/proxy/api',
      schemas: 'src/proxy/models',
      client: 'react-query',
      
      mock: true,
      override: {
        useDates: true,
        components: {
          schemas: {
            suffix: 'DTO',
          },
        },
        mutator: {
          path: './src/lib/axios.ts',
          name: 'customAxios',
        },
        query: {
          useQuery: true,
          useInfinite: true,
          useInfiniteQueryParam: 'page',
          options: {
            staleTime: 10000,
          },
        },      
      },
    },
    input: {
      target: './swagger.json',
    },
  },
});
  1. You will see, that the generated not-working.msw.ts looks like this:
/**
 * Generated by orval v6.22.1 🍺
 * Do not edit manually.
 * API Documentation
 * API documentation for selected endpoints.
 * OpenAPI spec version: 1.0.0
 */
import { faker } from '@faker-js/faker';
import { HttpResponse, delay, http } from 'msw';
// see duplicate function declaration below, only happens if only one endpoint is present per tag in swagger.json
export const getNotWorkingMock = () => ({ accessToken: faker.helpers.arrayElement([faker.word.sample(), undefined]) }); // Cannot redeclare block-scoped variable 'getNotWorkingMock'.

export const getNotWorkingMock = () => [
  http.get('http://localhost:8080/NotWorking', async () => {
    await delay(1000);
    return new HttpResponse(JSON.stringify(getNotWorkingMock()), {
      status: 200,
      headers: {
        'Content-Type': 'application/json',
      },
    });
  }),
];

What happens?

Two functions with the same name are exported from the not-working.msw.ts file if only one endpoint is present for a tag in the swagger.json file.

What were you expecting to happen?

The first function should be called differently and used inside the second function.

Any logs, error output, etc?

I do see the warning Failed to parse provided mutator function in the console but could not figure out where that came from.

Any other comments?

Many thanks for developing this great tool!

What versions are you using?

Operating System: macOS 14.1.1
Package Version:

  • "orval": "6.22.1"
  • "msw": "2.0.10",
    Browser Version: -
@melloware melloware added the bug Something isn't working label Dec 8, 2023
@SetupCoding
Copy link
Author

Inspected it a little more:

Apparently if the route is only 1 deep, e.g. /not-working and is the only route with that tag, e.g. , "tags": ["NotWorking"], the issue occurs.
If the route is 2 deep, e.g. /working/deep and is the only route with a tag, e.g. "tags": ["Working"], it works as expected.

@SetupCoding SetupCoding changed the title Cannot redeclare block-scoped variable in mock service worker when only one route is present per tag Duplicate function name in msw when route depth is one, e.g. /not-working Dec 20, 2023
@SetupCoding SetupCoding changed the title Duplicate function name in msw when route depth is one, e.g. /not-working Duplicate function name in msw when route depth is one, e.g. /NotWorking Dec 20, 2023
@soartec-lab
Copy link
Member

Hi @SetupCoding, Thanks for creating this report.
This issue is occur when the path name and operationId are the same. So a rare case because operationId is usually used like addXX or getXXX, but i will fix it because it causes unintended problems for users.

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
3 participants