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 free-form objects #1165

Merged
merged 1 commit into from
Oct 28, 2022
Merged

Conversation

kshramt
Copy link

@kshramt kshramt commented Jul 27, 2022

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/

> 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/
/**
* This is a free-form object with additionalProperties: true.
*/
export type FreeFormObjectWithAdditionalPropertiesEqTrue = Record<string, any>;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi! Why are you using Record type?
AFAU Record used to fix fields in a object. https://www.typescriptlang.org/docs/handbook/utility-types.html#recordkeys-type

But additionalProperties are about free keys in object.

Thank you!

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the comment.

Hi! Why are you using Record type?

IIUC, the use case for Record is not limited to restricting keys to the union type of literals. According to https://stackoverflow.com/a/54104476, Record<string, any> is equivalent to {[key: string]: any}. Also,

"dictionaryWithEnumFromDescription": {
"type": "object",
"additionalProperties": {
"type": "integer",
"description": "Success=1,Warning=2,Error=3"
}
},
is converted to a Record (
dictionaryWithEnumFromDescription?: Record<string, number>;
).

@kshramt
Copy link
Author

kshramt commented Jul 31, 2022

#1154 might be resolved by the PR.

@ajenkinski
Copy link

#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.

@ferdikoomen ferdikoomen merged commit 4379538 into ferdikoomen:master Oct 28, 2022
mb21 added a commit to DND-IT/openapi-typescript-codegen that referenced this pull request Mar 8, 2023
…ject"

This reverts commit 4379538, reversing
changes made to 168da7a.
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

Successfully merging this pull request may close these issues.

4 participants