Openapi-generator: [BUG][typescript-axios] Object type instead any

Created on 11 Feb 2020  ·  2Comments  ·  Source: OpenAPITools/openapi-generator

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] What's the version of OpenAPI Generator used?
  • [x] Have you search for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Bounty to sponsor the fix (example)
Description

I want to use any type in typescript, but generator gives me object type.
From OpenAPI 3 Data Types (section Any Type)

A schema without a type matches any data type – numbers, strings, objects, and so on.

openapi-generator version

4.2.3

OpenAPI declaration file content or url
{
  "openapi": "3.0.2",
  "info": {
    "title": "Api",
    "description": "Api",
    "version": "1.0.0"
  },
  "paths": {},
  "components": {
    "schemas": {
      "DataBlock": {
        "title": "DataBlock",
        "required": [
          "data"
        ],
        "type": "object",
        "properties": {
          "version": {
            "title": "Version",
            "type": "string",
            "description": "Version of data block"
          },
          "data": {
            "title": "Data",
            "description": "Block data",
            "nullable": true,
            "example": "here may be any json value"
          }
        },
        "description": "Data block with version"
      }
    }
  }
}
Command line used for generation

openapi-generator generate -g typescript-axios -i ./src/api/openapi.json -o ./src/api/ -c ./src/api/.openapi-generator/config.yaml -DskipFormModel=true

Steps to reproduce

Do not specify the type in the schema, and generate

Actual output
/**
 * Data block with version
 * @export
 * @interface DataBlock
 */
export interface DataBlock {
    /**
     * Version of data block
     * @type {string}
     * @memberof DataBlock
     */
    version?: string;
    /**
     * Block data
     * @type {object}
     * @memberof DataBlock
     */
    data: object | null;
}
Expected output
/**
 * Data block with version
 * @export
 * @interface DataBlock
 */
export interface DataBlock {
    /**
     * Version of data block
     * @type {string}
     * @memberof DataBlock
     */
    version?: string;
    /**
     * Block data
     * @type {any}
     * @memberof DataBlock
     */
    data: any; // <----- Any type
}
Related issues/PRs

Not found

Suggest a fix

I have no idea

TypeScript Bug

Most helpful comment

I agree object type (default) is not helpful in Typescript. In fact, the same issue also exists for other typescript generators (e.g. typescript-angular ), not just typescript-axios.

This can be solved at your end by providing a custom typemapping via cli arguments:

openapi-generator generate ...other-args... --type-mappings object=any

This should probably be a default mapping for the typescript generators.

Maybe even unknown instead of any - for better type safety (to enforce users to always verify the shape of data at runtime)

All 2 comments

👍 Thanks for opening this issue!
🏷 I have applied any labels matching special text in your issue.

The team will review the labels and make any necessary changes.

I agree object type (default) is not helpful in Typescript. In fact, the same issue also exists for other typescript generators (e.g. typescript-angular ), not just typescript-axios.

This can be solved at your end by providing a custom typemapping via cli arguments:

openapi-generator generate ...other-args... --type-mappings object=any

This should probably be a default mapping for the typescript generators.

Maybe even unknown instead of any - for better type safety (to enforce users to always verify the shape of data at runtime)

Was this page helpful?
0 / 5 - 0 ratings