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

Incorrect enum ApiPaths members when parametrized paths #2194

Open
1 of 2 tasks
wgebczyk opened this issue Mar 9, 2025 · 5 comments
Open
1 of 2 tasks

Incorrect enum ApiPaths members when parametrized paths #2194

wgebczyk opened this issue Mar 9, 2025 · 5 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@wgebczyk
Copy link

wgebczyk commented Mar 9, 2025

openapi-typescript version

7.6.1

Node.js version

23.9.0

OS + version

win11

Description

Hey!

It seems that code that generates ApiPath enum incorrectly tries to deduplicate enum identifiers when paths differs on params. This is as well visible when path has multple params of various level of reusing parameter names.

Reproduction

Small open ap spec

{
  "openapi": "3.0.4",
  "info": { "title": "some api", "version": "1" },
  "servers": [ { "url": "uri:some-api" } ],
  "paths": {
    "/api/block": {
      "get": {
        "summary": "whatever",
        "tags": [ "Block" ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": { "schema": { "type": "string" } }
            }
          }
        }
      }
    },
    "/api/block/{blockId}": {
      "get": {
        "summary": "whatever",
        "tags": [ "Block" ],
        "parameters": [
          { "name": "blockId", "in": "path", "schema": { "type": "string" } }
        ],
        "responses": {
          "200": {
            "description": "OK",
            "content": {
              "application/json": { "schema": { "type": "string" } }
            }
          }
        }
      }
    }
  },
  "components": {
    "securitySchemes": {
      "awesome": {
        "type": "oauth2",
        "flows": {
          "implicit": {
            "authorizationUrl": "https://example.com/oauth2/authorize",
            "scopes": { "awesome": "Awesome scope" }
          }
        }
      }
    }
  },
  "security": [ { "awesome": [ "awesome" ] } ]
}

command line:

npx openapi-typescript "C:\...\repro.json" -o ./repro.schema.d.ts --make-paths-enum

output:

(...)
export enum ApiPaths {
    GetApiBlock = "/api/block",
    GetApiBlock = "/api/block/:blockId"
}

Expected result

not have same enum member identifiers

Required

  • My OpenAPI schema is valid and passes the Redocly validator (npx @redocly/cli@latest lint)

Extra

@wgebczyk wgebczyk added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Mar 9, 2025
@eugene-sy
Copy link

It looks like https://github.com/openapi-ts/openapi-typescript/blob/main/packages/openapi-typescript/src/transform/paths-enum.ts#L34-L35 is the root cause.
Is there a reason why the transformation is required?

@theo-staizen
Copy link

theo-staizen commented Mar 25, 2025

@wgebczyk @wgebczyk
FYI, this was also discussed (and potentially solved?) in this PR #2152
Someone shared a temporary workaround script #2152 (comment)
In my case, since I only have a few path params, I manually replace :param with {param}
E.g ActivityLogs_GetByStaffId = "/ActivityLogs/staff/{id}",

@wgebczyk
Copy link
Author

@theo-staizen Probably we misunderstood each other. I'm not referring to use of :param or {param}. For above short OAS, it generated enum member twice of same name (identifier) GetApiBlock.

The only case that it might help in above case is to use postprocessing to find out generated enum, parse it's values (not names/identifiers for it's values) and try manually prepare/guess GetApiBlock. In such case it would be easier/better/safer to extend/configure generation of enum members with custom function (?) if correcting it is not possible.

... or maybe I'm misreading issue and your suggestion...

@theo-staizen
Copy link

theo-staizen commented Mar 25, 2025

@wgebczyk Oh I see, yes I did misunderstand your question.

Lucky for you, I did run across that issue as well, which I fixed by adding an operationId to each operation under paths. For example:

{
  "paths": {
    "/ActivityLogs/staff/{id}": {
      "get": {
        "tags": [
          "ActivityLogs"
        ],
        "operationId": "ActivityLogs_GetByStaffId",
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "required": true,
            "schema": {
              "type": "string",
              "format": "uuid"
            }
          },
          {
            "name": "pageSize",
            "in": "query",
            "schema": {
              "type": "integer",
              "format": "int32",
              "default": 100
            }
          },
          {
            "name": "cursor",
            "in": "query",
            "schema": {
              "type": "integer",
              "format": "int32"
            }
          }
        ],
        "responses": {
          "200": {
            "description": "OK"
          }
        }
      }
    },
}

P.s: The issue template mentions linting your scheme using the @redocly/cli package, which is how I discovered my schema was missing fields. If you are using a tool to autogenerate the schema based on annotations in your source code, this should be easy enough to fix

@wgebczyk
Copy link
Author

Uhm... yeah... Just reading sources :) Probably I should stop being lazy and manually define all operations with ID and would be fine.
Well.. some work in front of me, but long term probably better that rely on auto generation! :)

Still some error during generation would be helpful , like: ERROR in api.json (123) - hold your horses cowboy! can't gen for you enums as there would be duplicates - just please define damn operationId and you will be fine :D

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library
Projects
None yet
Development

No branches or pull requests

3 participants