-
-
Notifications
You must be signed in to change notification settings - Fork 524
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
Conversation
> 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>; |
There was a problem hiding this comment.
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!
There was a problem hiding this comment.
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,
openapi-typescript-codegen/test/spec/v3.json
Lines 1831 to 1837 in 2721045
"dictionaryWithEnumFromDescription": { | |
"type": "object", | |
"additionalProperties": { | |
"type": "integer", | |
"description": "Success=1,Warning=2,Error=3" | |
} | |
}, |
Record
(dictionaryWithEnumFromDescription?: Record<string, number>; |
#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 Person:
type: object
properties:
name:
type: string
additionalProperties: true This means the 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 |
https://swagger.io/docs/specification/data-models/data-types/