Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support Swagger's "additionalProperties" keyword for object with dynamic keys #22

Open
GabriMcNab opened this issue Mar 8, 2022 · 1 comment

Comments

@GabriMcNab
Copy link
Contributor

In an OpenAPI document, when an object has some dynamic keys, it's declared like so:

someObject:
  type: object
  additionalProperties:
    type: string

Currently, the additionalProperties keyword is ignored, and the Typescript type is generated in this way:

someObject: {}

If you are using typescript-eslint > v5.0.0, this will raise an error, because the empty object literal syntax is banned, as it is considered unsafe. Instead we should use:

someObject: Record<string, string>

In addition to the example shown above, there are other use cases for additionalProperties:

someObject:
  type: object
  additionalProperties: true

If the dictionary values can be of any type (aka free-form object) --> Record<string, unknown>

someObject:
  type: object
  additionalProperties:
    $ref: '#/components/schemas/Message'

Instead of using an inline schema, additionalProperties can $ref another schema

someObject:
  type: object
  properties:
    default:
      type: string
  required:
    - default
  additionalProperties:
    type: string

When a dictionary has some fixed properties, and some dynamic ones.

@BenisonSam
Copy link

I am also arrived at this. Is there any solution for this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants