Skip to content

Generates invalid types when API paths are nested #1752

Open
@mat-certn

Description

@mat-certn

Description

We have multiple paths in our schema that are "nested" below each other, like so:

/some/path/{id}
/some/path/{id}/summary

openapi-typescript using the argument --path-params-as-types generates the following:

  [path: `/some/path/${string}`]: SchemaOne;
  [path: `/some/path/${string}/summary`]: SchemaTwo;

which conflicts with the error

'x' index type 'SchemaOne' is not assignable to 'x' index type 'SchemaTwo'.

this is because technically /some/path/${string}/summary is generated as incorrectly assignable to /some/path/${string} - instead of ${string} it should be some sort of type that cannot include "/"

Name Version
openapi-typescript 7.0.2
TypeScript 5.3.3 or 5.5.3 (have tried a few versions)
Node.js 18.18.2
OS + version macOS 14.5

Reproduction

Generate types using the following schema:

openapi: 3.0.3
servers:
  - url: http://some-server.com
    description: Local development server
info:
  title: Example API with conflicting paths
  version: 0.0.1
  license:
    name: MIT
    url: https://opensource.org/licenses/MIT
paths:
  /api/internal/cases/{short_id}:
    get:
      summary: "Get case details"
      operationId: cases_retrieve
      parameters:
        - in: path
          name: short_id
          schema:
            type: string
          required: true
      tags:
        - cases
      security:
        - AuthZeroUserAuth: []
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CaseDetail"
          description: ""
        "404":
          description: No response body
  /api/internal/cases/{short_id}/summary:
    get:
      summary: "Get case summary"
      operationId: cases_summary_retrieve
      parameters:
        - in: path
          name: short_id
          schema:
            type: string
          required: true
      tags:
        - cases
      security:
        - AuthZeroUserAuth: []
      responses:
        "200":
          content:
            application/json:
              schema:
                $ref: "#/components/schemas/CaseSummary"
          description: ""
        "404":
          description: No response body
components:
  schemas:
    CaseDetail:
      type: object
      properties:
        case_name:
          type: string
          format: uuid
          readOnly: true
      required:
        - case_name
    CaseSummary:
      type: object
      properties:
        case_summary:
          type: string
          format: uuid
          readOnly: true
      required:
        - case_summary
  securitySchemes:
    AuthZeroUserAuth:
      type: http
      scheme: bearer
      bearerFormat: JWT

npx openapi-typescript example.yml --output example.ts --path-params-as-types

Expected result

A more complex type than ${string} that prevents both endpoints from matching each other.

Checklist

❯ npx @redocly/cli lint example.yml
No configurations were provided -- using built in recommended configuration by default.

validating example.yml...
example.yml: validated in 12ms

Woohoo! Your API description is valid. 🎉

Metadata

Metadata

Assignees

Labels

PRs welcomePRs are welcome to solve this issue!bugSomething isn't workingopenapi-tsRelevant to the openapi-typescript library

Type

No type

Projects

Status

No status

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions