Skip to content

[BUG][typescript-axios] The wrong type is generated by additionalProperties #20029

Open
@punkyoon

Description

@punkyoon

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)

Description

When there is an additionalProperty value in schemas, it generates incorrect TypeScript code typed as Null instead of null.

openapi-generator version

7.9.0 (@openapitools/openapi-generator-cli is 2.15.3)

OpenAPI declaration file content or url

Note

It works fine when there is no additionalProperties option

openapi: 3.1.0
info:
  title: Sample API
  description: This is sample api
  version: 0.1.0
components:
  schemas:
    SampleData:
      # No `additionalProperties`
      properties:
        sample_property:
          oneOf:
            - type: 'null'
            - $ref: '#/components/schemas/ExampleData'
        sample_property2:
          format: float
          type:
            - number
            - 'null'
    ExampleData:
      type: object
      properties:
        example_property:
          type: string

Caution

It works incorrectly when there is an additionalProperties option

openapi: 3.1.0
info:
  title: Sample API
  description: This is sample api
  version: 0.1.0
components:
  schemas:
    SampleData:
      # With `additionalProperties`
      additionalProperties: {}
      properties:
        sample_property:
          oneOf:
            - type: 'null'
            - $ref: '#/components/schemas/ExampleData'
        sample_property2:
          format: float
          type:
            - number
            - 'null'
    ExampleData:
      type: object
      properties:
        example_property:
          type: string

Generation Details

I've removed the generated comments

Note

It works fine when there is no additionalProperties option

.openapi-generator/FILES

.gitignore
.npmignore
.openapi-generator-ignore
api.ts
base.ts
common.ts
configuration.ts
git_push.sh
index.ts
models/example-data.ts
models/index.ts
models/sample-data.ts

models/sample-data.ts

import type { ExampleData } from './example-data';

export interface SampleData {
    'sample_property'?: ExampleData | null;

    'sample_property2'?: number | null;
}

Caution

It works incorrectly when there is an additionalProperties option

.openapi-generator/FILES

.gitignore
.npmignore
.openapi-generator-ignore
api.ts
base.ts
common.ts
configuration.ts
git_push.sh
index.ts
models/example-data.ts
models/index.ts
models/sample-data-sample-property.ts
models/sample-data.ts

models/sample-data.ts

import type { SampleDataSampleProperty } from './sample-data-sample-property';

export interface SampleData {
    [key: string]: any;

    'sample_property'?: SampleDataSampleProperty;

    'sample_property2'?: number;
}

models/sample-data-sample-property.ts

There is Null instead of null

import type { ExampleData } from './example-data';

export type SampleDataSampleProperty = ExampleData | Null;  //////// This is wrong!

Steps to reproduce

$ npm install @openapitools/openapi-generator-cli@2.15.3

# Generate the code with the `sample.yaml` I've uploaded the above
$ npx openapi-generator-cli generate \
  -g typescript-axios \
  -i sample.yaml \
  -o sample \
  --additional-properties stringEnums=true,legacyDiscriminatorBehavior=false,withSeparateModelsAndApi=true,modelPackage=models,apiPackage=apis,useSingleRequestParameter=true

Related issues/PRs

Nothing

Suggest a fix

It should generate the null instead of Null

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions