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

Support free-form objects

Open kshramt opened this issue 2 years ago • 2 comments

A free-form object (arbitrary property/value pairs) is defined as:

type: object

This is equivalent to

type: object
additionalProperties: true

and

type: object
additionalProperties: {}

https://swagger.io/docs/specification/data-models/data-types/

kshramt avatar Jul 27 '22 03:07 kshramt

https://github.com/ferdikoomen/openapi-typescript-codegen/issues/1154 might be resolved by the PR.

kshramt avatar Jul 31 '22 14:07 kshramt

#1154 might be resolved by the PR.

This PR partially resolves #1154. It resolves the case where the object is completely free-form, but doesn't resolve the case where an object schema explicitly defines some properties, and also adds additionalProperties: true. For example, the following is a valid schema:

Person:
  type: object
  properties:
    name:
      type: string
  additionalProperties: true

This means the name property must be of type string, but other properties with arbitrary types are also allowed. This is equivalent to a Typescript type like:

export type Person = {
  name?: string,
  [key: string]: any
}

However the code in this PR will emit just:

export type Person = Record<string, any>;

This incorrectly allows the name property to have any type.

ajenkinski avatar Aug 08 '22 13:08 ajenkinski