Skip to content

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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/eleven-shoes-mate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"openapi-typescript": minor
---

Add support for patternProperties
20 changes: 15 additions & 5 deletions packages/openapi-typescript/src/transform/schema-object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,7 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
if (
("properties" in schemaObject && schemaObject.properties && Object.keys(schemaObject.properties).length) ||
("additionalProperties" in schemaObject && schemaObject.additionalProperties) ||
("patternProperties" in schemaObject && schemaObject.patternProperties) ||
("$defs" in schemaObject && schemaObject.$defs)
) {
// properties
Expand Down Expand Up @@ -547,13 +548,22 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
);
}

// additionalProperties
if (schemaObject.additionalProperties || options.ctx.additionalProperties) {
// additionalProperties / patternProperties
if (schemaObject.additionalProperties || options.ctx.additionalProperties || schemaObject.patternProperties) {
const hasExplicitAdditionalProperties =
typeof schemaObject.additionalProperties === "object" && Object.keys(schemaObject.additionalProperties).length;
const addlType = hasExplicitAdditionalProperties
? transformSchemaObject(schemaObject.additionalProperties as SchemaObject, options)
: UNKNOWN;
const hasExplicitPatternProperties =
typeof schemaObject.patternProperties === "object" && Object.keys(schemaObject.patternProperties).length;
const addlTypes = [];
if (hasExplicitAdditionalProperties) {
addlTypes.push(transformSchemaObject(schemaObject.additionalProperties as SchemaObject, options));
}
if (hasExplicitPatternProperties) {
for (const [_, v] of getEntries(schemaObject.patternProperties ?? {}, options.ctx)) {
addlTypes.push(transformSchemaObject(v, options));
}
}
const addlType = addlTypes.length === 0 ? UNKNOWN : tsUnion(addlTypes);
return tsIntersection([
...(coreObjectType.length ? [ts.factory.createTypeLiteralNode(coreObjectType)] : []),
ts.factory.createTypeLiteralNode([
Expand Down
1 change: 1 addition & 0 deletions packages/openapi-typescript/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ export interface ObjectSubtype {
type: "object" | ["object", "null"];
properties?: { [name: string]: SchemaObject | ReferenceObject };
additionalProperties?: boolean | Record<string, never> | SchemaObject | ReferenceObject;
patternProperties?: Record<string, SchemaObject | ReferenceObject>;
required?: string[];
allOf?: (SchemaObject | ReferenceObject)[];
anyOf?: (SchemaObject | ReferenceObject)[];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,55 @@ describe("transformSchemaObject > object", () => {
// options: DEFAULT_OPTIONS,
},
],
[
"patternProperties > empty object",
{
given: { type: "object", patternProperties: {} },
want: `{
[key: string]: unknown;
}`,
},
],
[
"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",
{
given: {
type: "object",
additionalProperties: { type: "number" },
patternProperties: { "^a": { type: "string" } },
},
want: `{
[key: string]: number | string;
}`,
},
],
[
"nullable",
{
Expand Down