ferdikoomen / openapi-typescript-codegen

NodeJS library that generates Typescript or Javascript clients based on the OpenAPI specification
MIT License
2.97k stars 525 forks source link

Tuple with multiple data types not converted #1890

Open nicholaschiang opened 1 year ago

nicholaschiang commented 1 year ago

Describe the bug When a response model is a tuple with two different data types (e.g. [Task, StarSchemaDataSource]), the generated code has an Array<any> instead of [Task, StarSchemaDataSource].

The OpenAPI spec:

        "responses": {
          "200": {
            "description": "Successful Response",
            "content": {
              "application/json": {
                "schema": {
                  "items": [
                    { "$ref": "#/components/schemas/Task" },
                    { "$ref": "#/components/schemas/StarSchemaDataSource" }
                  ],
                  "type": "array",
                  "maxItems": 2,
                  "minItems": 2,
                  "title": "Response Cold Start From Tables Api V1 Accounts  Account Name  Star Schema Data Source Cold Start From Tables Post"
                }
              }
            }
          },
          "422": {
            "description": "Validation Error",
            "content": {
              "application/json": {
                "schema": { "$ref": "#/components/schemas/HTTPValidationError" }
              }
            }
          }
        },

The generated code:

  /**
   * Cold Start From Tables
   * Cold start from table.
   *
   * The fully_qualified_table_name is expected to be in the format:
   * <database_name>.<schema_name>.<table_name>
   * or for warehouses that don't support schemas:
   * <database_name>.<table_name>
   * @param accountName
   * @param connectionId
   * @param requestBody
   * @returns any Successful Response
   * @throws ApiError
   */
  public coldStartFromTables(
    accountName: string,
    connectionId: string,
    requestBody?: Body_cold_start_from_tables_api_v1_accounts__account_name__star_schema_data_source_cold_start_from_tables_post,
  ): CancelablePromise<Array<any>> {
    return this.httpRequest.request({
      method: 'POST',
      url: '/api/v1/accounts/{account_name}/star_schema_data_source/cold_start_from_tables',
      path: {
        account_name: accountName,
      },
      query: {
        connection_id: connectionId,
      },
      body: requestBody,
      mediaType: 'application/json',
      errors: {
        422: `Validation Error`,
      },
    })
  }

The expected generated code:

  /**
   * Cold Start From Tables
   * Cold start from table.
   *
   * The fully_qualified_table_name is expected to be in the format:
   * <database_name>.<schema_name>.<table_name>
   * or for warehouses that don't support schemas:
   * <database_name>.<table_name>
   * @param accountName
   * @param connectionId
   * @param requestBody
   * @returns any Successful Response
   * @throws ApiError
   */
  public coldStartFromTables(
    accountName: string,
    connectionId: string,
    requestBody?: Body_cold_start_from_tables_api_v1_accounts__account_name__star_schema_data_source_cold_start_from_tables_post,
  ): CancelablePromise<[Task, StarSchemaDataSource]> {
    return this.httpRequest.request({
      method: 'POST',
      url: '/api/v1/accounts/{account_name}/star_schema_data_source/cold_start_from_tables',
      path: {
        account_name: accountName,
      },
      query: {
        connection_id: connectionId,
      },
      body: requestBody,
      mediaType: 'application/json',
      errors: {
        422: `Validation Error`,
      },
    })
  }

Potentially related to #1885 (#1884).

orhanhenrik commented 1 year ago

@nicholaschiang you need to wrap your two data types in oneOf:

                  "items": {
                    "oneOf": [
                      { "$ref": "#/components/schemas/Task" },
                      { "$ref": "#/components/schemas/StarSchemaDataSource" }
                    ]
                  },

Then you will get Array<Task | StarSchemaDataSource> as the generated type.