Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 50ad586

Browse files
author
Pontus Lundin
committed
update after feedback
1 parent 70f8d8d commit 50ad586

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

packages/openapi-typescript/src/transform/schema-object.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,18 +547,27 @@ function transformSchemaObjectCore(schemaObject: SchemaObject, options: Transfor
547547
if (schemaObject.additionalProperties || options.ctx.additionalProperties || schemaObject.patternProperties) {
548548
const hasExplicitAdditionalProperties =
549549
typeof schemaObject.additionalProperties === "object" && Object.keys(schemaObject.additionalProperties).length;
550+
const hasImplicitAdditionalProperties =
551+
schemaObject.additionalProperties === true || (typeof schemaObject.additionalProperties === "object" && Object.keys(schemaObject.additionalProperties).length === 0);
550552
const hasExplicitPatternProperties =
551553
typeof schemaObject.patternProperties === "object" && Object.keys(schemaObject.patternProperties).length;
552554
const addlTypes = [];
553555
if (hasExplicitAdditionalProperties) {
554556
addlTypes.push(transformSchemaObject(schemaObject.additionalProperties as SchemaObject, options));
555557
}
558+
if (hasImplicitAdditionalProperties || (!schemaObject.additionalProperties && options.ctx.additionalProperties)) {
559+
addlTypes.push(UNKNOWN);
560+
}
556561
if (hasExplicitPatternProperties) {
557562
for (const [_, v] of getEntries(schemaObject.patternProperties ?? {}, options.ctx)) {
558563
addlTypes.push(transformSchemaObject(v, options));
559564
}
560565
}
561-
const addlType = addlTypes.length === 0 ? UNKNOWN : tsUnion(addlTypes);
566+
567+
if (addlTypes.length === 0) return;
568+
569+
const addlType = tsUnion(addlTypes);
570+
562571
return tsIntersection([
563572
...(coreObjectType.length ? [ts.factory.createTypeLiteralNode(coreObjectType)] : []),
564573
ts.factory.createTypeLiteralNode([

packages/openapi-typescript/test/transform/schema-object/object.test.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,11 +101,22 @@ describe("transformSchemaObject > object", () => {
101101
],
102102
[
103103
"patternProperties > empty object",
104+
{
105+
given: { type: "object", patternProperties: {} },
106+
want: `Record<string, never>`,
107+
},
108+
],
109+
[
110+
"patternProperties > empty object with options.additionalProperties=true",
104111
{
105112
given: { type: "object", patternProperties: {} },
106113
want: `{
107114
[key: string]: unknown;
108115
}`,
116+
options: {
117+
...DEFAULT_OPTIONS,
118+
ctx: { ...DEFAULT_CTX, additionalProperties: true },
119+
}
109120
},
110121
],
111122
[
@@ -132,6 +143,19 @@ describe("transformSchemaObject > object", () => {
132143
given: { type: "object", patternProperties: { "^a": { type: "string" }, "^b": { type: "number" } } },
133144
want: `{
134145
[key: string]: string | number;
146+
}`,
147+
},
148+
],
149+
[
150+
"patternProperties > additional=true and patterns",
151+
{
152+
given: {
153+
type: "object",
154+
additionalProperties: true,
155+
patternProperties: { "^a": { type: "string" } },
156+
},
157+
want: `{
158+
[key: string]: unknown | string;
135159
}`,
136160
},
137161
],
@@ -148,6 +172,22 @@ describe("transformSchemaObject > object", () => {
148172
}`,
149173
},
150174
],
175+
[
176+
"patternProperties > patterns with options.additionalProperties=true",
177+
{
178+
given: {
179+
type: "object",
180+
patternProperties: { "^a": { type: "string" } },
181+
},
182+
want: `{
183+
[key: string]: unknown | string;
184+
}`,
185+
options: {
186+
...DEFAULT_OPTIONS,
187+
ctx: { ...DEFAULT_CTX, additionalProperties: true },
188+
}
189+
},
190+
],
151191
[
152192
"nullable",
153193
{

0 commit comments

Comments
 (0)