openapi-typescript-codegen icon indicating copy to clipboard operation
openapi-typescript-codegen copied to clipboard

Support `additionalProperties: true`

Open ajenkins-cargometrics opened this issue 2 years ago • 3 comments

In OpenAPI 3, a schema like:

    AnyValues:
      description: An object whose property values can be of any type
      type: object
      additionalProperties: true

Should be equivalent to a typescript type like:

type AnyValues = {
    [key: string]: any
};

However what openapi-typescript-codegen actually generates is a type like this:

type AnyValues = {
};

That is, an object with no properties.

Additionally, OpenAPI allows combining additionalProperties: true with explicit property definitions. So an OpenAPI schema like this:

SomeType:
    type: object
    required: [name]
    properties:
        name:
            type: string
    additionalProperties: true

Should result in a typescript type like:

type SomeType = {
    name: string,
    [key: string]: any
};

but in fact it results in:

type SomeType = {
    name: string
};

Desired result: additionalProperties: true in an object schema should result in a [key: string]: any property in the corresponding typescript type.

ajenkins-cargometrics avatar Jul 18 '22 23:07 ajenkins-cargometrics

This is seems to be related to this one: #1066

garcipat avatar Jul 24 '22 15:07 garcipat

This is seems to be related to this one: #1066

That issue is different. In that case, the problem is that the spec doesn't specify a type for the schemas. That is, it has types defined like

  schemas:
    Account:
      properties:
        balance:
          type: string
          description: balance in unit WEI, presented with hex string
          example: '0x47ff1f90327aa0f8e'
        energy:
          type: string
          description: energy in uint WEI, presented with hex string
          example: '0xcf624158d591398'
        hasCode:
          type: boolean
          description: whether the account has code
          example: false

Simply adding type: object to the type fixes the problem. I'm not sure if the type field is supposed to be optional, but in any case it's a different problem.

ajenkinski avatar Aug 05 '22 01:08 ajenkinski

Hey @ajenkins-cargometrics, if this is still relevant, we got this feature in our fork that is kept in sync with this repository, would love you to give it a try

mrlubos avatar Feb 04 '24 15:02 mrlubos