OpenAPITools / OpenAPITools/openapi-generator

[BUG][typescript-axios] Object type instead any

Open
#5,279 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: TypeScript Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • 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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the openapi-generator generate -g typescript-axios command and the provided ./src/api/openapi.json schema. Trace how the typescript-axios generator maps an untyped nullable property, then add or update coverage for the generated DataBlock model so the field uses the intended TypeScript type instead of object.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, typescript
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.