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

Cannot handle dynamic path parameters in the middle of a path #1607

Open
1 of 2 tasks
jKlag opened this issue Apr 4, 2024 · 0 comments
Open
1 of 2 tasks

Cannot handle dynamic path parameters in the middle of a path #1607

jKlag opened this issue Apr 4, 2024 · 0 comments
Labels
bug Something isn't working openapi-ts Relevant to the openapi-typescript library

Comments

@jKlag
Copy link

jKlag commented Apr 4, 2024

Description

Given the following example API routes:

  • GET /objects/${object_id}
  • GET /objects/${object_id}/some-route

Running openapi-typescript with --path-params-as-types enabled will result in a TypeScript definition that throws the following error:

'`/objects/${string}/some-route`' index type '{ get: { parameters: { path: { object_id: string; }; }; responses: { ... }; }' is not assignable to '`/objects/${string}`' index type '{ get: { parameters: { path: { object_id: string; }; }; responses: { ... }; }'

I assume this results from the fact that the ${string} portion can hypothetically match any possible string (including any number of / characters) - not sure if there's a straightforward solution to this, but figured it raise it here in case anybody has solved this or has ideas on how to solve it.

Name Version
openapi-typescript 6.7.3
Node.js 20.10.0
OS + version macOS 14
TypeScript version 4.9.5

Reproduction

Generate types from the following example API spec using the --path-params-as-types flag:

{
    "openapi": "3.1.0",
    "info": {
        "title": "MyAPI",
        "version": "1.0.0",
        "description": ""
    },
    "paths": {
        "/objects/{object_id}": {
            "get": {
                "operationId": "get_object",
                "summary": "Summary of get_object",
                "parameters": [
                    {
                        "in": "path",
                        "name": "object_id",
                        "schema": {
                            "title": "ObjectId",
                            "type": "string"
                        },
                        "required": true
                    }
                ],
                "responses": {
                "200": {
                    "description": "OK",
                    "content": {
                        "application/json": {}
                    }
                }
                },
                "tags": [],
                "security": []
            }
        },
        "/objects/${object_id}/some-route": {
            "get": {
                "operationId": "some_route",
                "summary": "Summary of some_route",
                "parameters": [
                    {
                        "in": "path",
                        "name": "object_id",
                        "schema": {
                            "title": "ObjectId",
                            "type": "string"
                        },
                        "required": true
                    }
                ],
                "responses": {
                "200": {
                    "description": "OK",
                    "content": {
                        "application/json": {}
                    }
                }
                },
                "tags": [],
                "security": []
            }
        }
    },
    "servers": []
}

Expected result

(in case it’s not obvious)

Checklist

@jKlag jKlag added bug Something isn't working openapi-ts Relevant to the openapi-typescript library labels Apr 4, 2024
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

1 participant