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);
Copy link
Contributor

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 iff additionalProperties: true (or it is unset and options.ctx.additionalProperties is set).

So for example, for the following, we should generate string | unknown:

type: object
additionalProperties: true
patternProperties:
  ".*":
    type: string

IIUC the current code would only generate string.

(FWIW: I realize this was wrong in my PR as well).

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;
}`,
Copy link
Contributor

Choose a reason for hiding this comment

The 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 additionalProperties is false (which is not actually the JSON schema default). So in this object type, no keys are allowed:

The additionalProperties keyword is used to control the handling of extra stuff, that is, properties whose names are not listed in the properties keyword or match any of the regular expressions in the patternProperties keyword. By default any additional properties are allowed.

From: https://json-schema.org/understanding-json-schema/reference/object#additionalproperties

@duncanbeevers maybe you can double check my understanding of the --additional-properties flag?

},
],
[
"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",
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you add test cases with:

  • additionalProperties: true?
  • options.additionalProperties = true?

{
given: {
type: "object",
additionalProperties: { type: "number" },
patternProperties: { "^a": { type: "string" } },
},
want: `{
[key: string]: number | string;
}`,
},
],
[
"nullable",
{
Expand Down