openapi-typescript icon indicating copy to clipboard operation
openapi-typescript copied to clipboard

Cannot handle dynamic path parameters in the middle of a path

Open jKlag opened this issue 1 year ago • 0 comments

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 avatar Apr 04 '24 22:04 jKlag