-
-
Notifications
You must be signed in to change notification settings - Fork 539
Feat: Add support for pattern properties #2288
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"openapi-typescript": minor | ||
--- | ||
|
||
Add support for patternProperties |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -99,6 +99,55 @@ describe("transformSchemaObject > object", () => { | |
// options: DEFAULT_OPTIONS, | ||
}, | ||
], | ||
[ | ||
"patternProperties > empty object", | ||
{ | ||
given: { type: "object", patternProperties: {} }, | ||
want: `{ | ||
[key: string]: unknown; | ||
}`, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think this test case is correct. I think the result here should be the empty object. IIUC openapi-typescript works under the base assumption that the default for
From: https://json-schema.org/understanding-json-schema/reference/object#additionalproperties @duncanbeevers maybe you can double check my understanding of the |
||
}, | ||
], | ||
[ | ||
"patternProperties > basic", | ||
{ | ||
given: { type: "object", patternProperties: { "^a": { type: "string" } } }, | ||
want: `{ | ||
[key: string]: string; | ||
}`, | ||
}, | ||
], | ||
[ | ||
"patternProperties > enum", | ||
{ | ||
given: { type: "object", patternProperties: { "^a": { type: "string", enum: ["a", "b", "c"] } } }, | ||
want: `{ | ||
[key: string]: "a" | "b" | "c"; | ||
}`, | ||
}, | ||
], | ||
[ | ||
"patternProperties > multiple patterns", | ||
{ | ||
given: { type: "object", patternProperties: { "^a": { type: "string" }, "^b": { type: "number" } } }, | ||
want: `{ | ||
[key: string]: string | number; | ||
}`, | ||
}, | ||
], | ||
[ | ||
"patternProperties > additional and patterns", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add test cases with:
|
||
{ | ||
given: { | ||
type: "object", | ||
additionalProperties: { type: "number" }, | ||
patternProperties: { "^a": { type: "string" } }, | ||
}, | ||
want: `{ | ||
[key: string]: number | string; | ||
}`, | ||
}, | ||
], | ||
[ | ||
"nullable", | ||
{ | ||
|
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.
I'm afraid the logic here is not correct: We need to add
UNKNOWN
iffadditionalProperties: true
(or it is unset andoptions.ctx.additionalProperties
is set).So for example, for the following, we should generate
string | unknown
:IIUC the current code would only generate
string
.(FWIW: I realize this was wrong in my PR as well).