Repository: openapi-ts/openapi-typescript

Issue #915: Schema escape issues for $ref
Created: 2022-06-06T04:04:28Z
version: openapi-typescript@5.3.0
system: windws
node: v14.19.3
my openapi3 json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "bff to openapi3",
    "version": "1.0.0"
  },
  "paths": {
    "/v1/getUser2": {
      "get": {
        "operationId": "UserController.getUser2.DjHhjSamaD",
        "tags": ["User"],
        "summary": "",
        "description": "UserController.getUser2",
        "parameters": [
          {
            "in": "query",
            "name": "param1",
            "description": "",
            "required": false,
            "schema": {
              "type": "number"
            }
          },
          {
            "in": "query",
            "name": "param2",
            "description": "",
            "required": false,
            "schema": {
              "type": "object",
              "oneOf": [
                {
                  "type": "string"
                },
                {
                  "$ref": "#/components/schemas/IUserWithId"
                }
              ]
            }
          },
          {
            "in": "query",
            "name": "param3",
            "description": "",
            "required": false,
            "schema": {
              "type": "object",
              "oneOf": [
                {
                  "type": "string"
                },
                {
                  "$ref": "#/components/schemas/IUserWithName"
                }
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {}
              }
            },
            "description": "Successful response"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "IUserWithId": {
        "oneOf": [
          {
            "$ref": "#/components/schemas/Partial<Pick<IUser,\"id\">>"
          },
          {
            "type": "string"
          }
        ]
      },
      "Partial<Pick<IUser,\"id\">>": {
        "type": "object",
        "properties": {
          "id": {
            "type": "number"
          }
        }
      },
      "IUserWithName": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          }
        },
        "required": ["name"]
      },
      "IUser": {
        "type": "object",
        "properties": {
          "id": {
            "type": "number"
          },
          "name": {
            "type": "string"
          }
        },
        "required": ["id"]
      }
    }
  }
}


I use this openapi3 json file, generate ts file error
error info:
(node:9836) UnhandledPromiseRejectionWarning: SyntaxError: Property or signature expected. (17:1)
  15 |   schemas: {
  16 |     "IUserWithId": (components["schemas"]["Partial<Pick<IUser,\"id\">>"]) | (string);
> 17 | "Partial<Pick<IUser,"id">>": {
     | ^
  18 | "id"?: number;
  19 |
  20 | };
    at qe (*****\node_modules\prettier\parser-typescript.js:1)
    at UU (*****\node_modules\prettier\parser-typescript.js:280:5918)
    at Object.zU [as parse] (*****\node_modules\prettier\parser-typescript.js:280:6241)
    at Object.parse (*****\node_modules\prettier\index.js:7361:23)
    at coreFormat (*****\node_modules\prettier\index.js:8672:18)
    at formatWithCursor2 (*****\node_modules\prettier\index.js:8864:18)
    at *****\node_modules\prettier\index.js:39174:12
    at Proxy.format (*****\node_modules\prettier\index.js:39188:12)
    at openapiTS (*****\node_modules\openapi-typescript\src\index.ts:128:19)
    at processTicksAndRejections (internal/process/task_queues.js:95:5)


in line 17, "Partial<Pick<IUser,"id">>" ------error in this position, should generate to  "Partial<Pick<IUser,\"id\">>"

Duplicate issue #1000: generate ts file SyntaxError
Created: 2022-11-20T07:16:38Z
Description
generate ts file SyntaxError
my openapi3 json file
{
  "openapi": "3.0.0",
  "info": {
    "title": "bff to openapi3",
    "version": "1.0.0"
  },
  "paths": {
    "/v1/getUser2": {
      "get": {
        "operationId": "UserController.getUser2.DjHhjSamaD",
        "tags": ["User"],
        "summary": "",
        "description": "UserController.getUser2",
        "parameters": [
          {
            "in": "query",
            "name": "param1",
            "description": "",
            "required": false,
            "schema": {
              "type": "number"
            }
          },
          {
            "in": "query",
            "name": "param2",
            "description": "",
            "required": false,
            "schema": {
              "type": "object",
              "oneOf": [
                {
                  "type": "string"
                },
                {
                  "$ref": "#/components/schemas/IUserWithId"
                }
              ]
            }
          },
          {
            "in": "query",
            "name": "param3",
            "description": "",
            "required": false,
            "schema": {
              "type": "object",
              "oneOf": [
                {
                  "type": "string"
                },
                {
                  "$ref": "#/components/schemas/IUserWithName"
                }
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "content": {
              "application/json": {
                "schema": {}
              }
            },
            "description": "Successful response"
          }
        }
      }
    }
  },
  "components": {
    "schemas": {
      "IUserWithId": {
        "oneOf": [
          {
            "$ref": "#/components/schemas/Partial<Pick<IUser,\"id\">>"
          },
          {
            "type": "string"
          }
        ]
      },
      "Partial<Pick<IUser,\"id\">>": {
        "type": "object",
        "properties": {
          "id": {
            "type": "number"
          }
        }
      },
      "IUserWithName": {
        "type": "object",
        "properties": {
          "name": {
            "type": "string"
          }
        },
        "required": ["name"]
      },
      "IUser": {
        "type": "object",
        "properties": {
          "id": {
            "type": "number"
          },
          "name": {
            "type": "string"
          }
        },
        "required": ["id"]
      }
    }
  }
}


generate ts file
  
/** Type helpers */
type Without<T, U> = { [P in Exclude<keyof T, keyof U>]?: never };
type XOR<T, U> = (T | U) extends object ? (Without<T, U> & U) | (Without<U, T> & T) : T | U;
type OneOf<T extends any[]> = T extends [infer Only] ? Only : T extends [infer A, infer B, ...infer Rest] ? OneOf<[XOR<A, B>, ...Rest]> : never;

export interface paths {
  "/v1/getUser2": {
    /** @description UserController.getUser2 */
    get: operations["UserController.getUser2.DjHhjSamaD"];
  };
}

export interface components {
  schemas: {
    IUserWithId: components["schemas"]["Partial<Pick<IUser,\\"id\\">>"] | string;
    "Partial<Pick<IUser,\"id\">>": {
      id?: number;
    };
    IUserWithName: {
      name: string;
    };
    IUser: {
      id: number;
      name?: string;
    };
  };
  responses: never;
  parameters: never;
  requestBodies: never;
  headers: never;
  pathItems: never;
}

export type external = Record<string, never>;

export interface operations {

  "UserController.getUser2.DjHhjSamaD": {
    /** @description UserController.getUser2 */
    parameters?: {
      query?: {
        param1?: number;
        param2?: string | components["schemas"]["IUserWithId"];
        param3?: string | components["schemas"]["IUserWithName"];
      };
    };
    responses: {
      /** @description Successful response */
      200: {
        content: {
          "application/json": Record<string, never>;
        };
      };
    };
  };
}


SyntaxError in this, you can use openapi.json file to generate ts file
IUserWithId: components["schemas"]["Partial<Pick<IUser,\\"id\\">>"] | string;

The correct one should be
IUserWithId: components["schemas"]["Partial<Pick<IUser,\"id\">>"] | string;




Name
Version




openapi-typescript
6.0.3


Node.js
14.19.3


OS + version
macOS 12

Closing comment (2023-04-09T23:51:28Z): Duplicate of #915