Repository: openapi-ts/openapi-typescript

Issue #956: Error when `$ref` uses escape characters
Created: 2022-10-24T15:00:31Z
In a file these paths are defined:
paths:
  /public/v1/foos:
    get:
      operationId: getFoos
      tags:
        - Foo
      description: Foo foo foo foo.
      responses:
        "200":
          description: The foos
          content:
            application/json:
              schema:
                $ref: './otherfile.yaml#/components/schemas/Foos'
        "422":
          description: A validation error, invalid or missing supplier ID.
  /public/v1/{id}/foos:
    get:
      operationId: getFoosWithID
      parameters:
        - in: path
          name: id
          schema:
            type: string
          required: true
      description:
        $ref: "#/paths/~1public~1v1~1foos/get/description"
      tags:
        $ref: "#/paths/~1public~1v1~1foos/get/tags"
      responses:
        $ref: "#/paths/~1public~1v1~1foos/get/responses"
It looks like openapi-typescript doesn't like the escape characters ~1. I get the following error:
file://.../node_modules/openapi-typescript/dist/utils.js:41
    const commentText = text.trim().replace(COMMENT_RE, "*\\/");
                             ^
                             
TypeError: text.trim is not a function
    at comment (file://.../node_modules/openapi-typescript/dist/utils.js:41:30)
    at transformPathsObj (file://.../node_modules/openapi-typescript/dist/transform/paths.js:53:27)
    at transformAll (file://.../node_modules/openapi-typescript/dist/transform/index.js:27:25)
    at openapiTS (file://.../node_modules/openapi-typescript/dist/index.js:57:23)
    at async generateSchema (file://.../node_modules/openapi-typescript/bin/cli.js:97:18)
    at async file://.../node_modules/openapi-typescript/bin/cli.js:196:7
    at async Promise.all (index 63)
    at async main (file://.../node_modules/openapi-typescript/bin/cli.js:185:3)

Duplicate issue #1275: Simple api spec results in invalid typescript
Created: 2023-08-07T11:37:04Z
Description
I have a small example of a valid openapi spec as a json file. When bundling it with openapi-typescript, the generated typescript is invalid.
The input is as following:
{
  "openapi": "3.0.2",
  "info": { "title": "foo", "version": "1.0" },
  "servers": [{ "url": "http://localhost:9005", "description": "dev environment" }],
  "paths": {
    "/lame": {
      "get": {
        "summary": "sum",
        "operationid": "foo",
        "responses": {
          "200": {
            "description": "desc",
            "content": {
              "application/json": {
                "schema": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "somestring": {
                        "type": "string",
                        "description": "the id number of a specific invoice"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    },
    "/someurl": {
      "post": {
        "summary": "sum",
        "operationid": "bar",
        "responses": {
          "200": {
            "description": "desc",
            "content": {
              "application/json": {
                "schema": {
                  "type": "object",
                  "properties": {
                    "invoices": {
                      "type": "array",
                      "items": {
                        "$ref": "#/paths/~1lame/get/responses/200/content/application~1json/schema/items"
                      }
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}


I run const output = await openapiTS(input).
The output is as following:
/**
 * This file was auto-generated by openapi-typescript.
 * Do not make direct changes to the file.
 */

export interface paths {
  "/lame": {
    /** sum */
    get: operations["foo"]
  }
  "/someurl": {
    /** sum */
    post: operations["bar"]
  }
}

export type webhooks = Record<string, never>

export type components = Record<string, never>

export type external = Record<string, never>

export interface operations {
  /** sum */
  foo: {
    responses: {
      /** @description desc */
      200: {
        content: {
          "application/json": {
            /** @description The id number of a specific invoice */
            someString?: string
          }[]
        }
      }
    }
  }
  /** sum */
  bar: {
    responses: {
      /** @description desc */
      200: {
        content: {
          "application/json": {
            invoices?: paths["/lame"]["get"]["responses"]["200"]["content"]["application/json"]["schema"]["items"][]
          }
        }
      }
    }
  }
}



To get the typescript to compile I would have to remove ["schema"]["items"][] from the last row.



Name
Version




openapi-typescript
6.4.0


Node.js
v18.16.1


OS + version
macOS 13



Reproduction
I hope input/output is sufficient. I could upload a small sample project if it would help.
Checklist

 My OpenAPI schema passes the Redocly validator (npx @redocly/cli@latest lint)
 I’m willing to open a PR (see CONTRIBUTING.md)

The redocly validator did give some hints, but they were not relevant and I ignored them to keep the example small. The api does pass validators such as https://apitools.dev/swagger-parser/online/

Closing comment (2023-08-20T00:40:03Z): This is a duplicate of #956. The underlying issue is this library doesn’t currently support referencing schema properties like you’re doing. I know some tools support it; some don’t. I’m a little fuzzy on whether or not this is technically valid spec or not. But we haven’t made a decision yet whether or not to add this feature. Either way, tracking in #956.
As a temporary workaround, move all $refs either into the Components object, or move them to external files (referencing partial schemas works just fine)