Skip to content

Commit 635c0ad

Browse files
FabioWannerfabio.wanner
andauthored
504 - Fix additionalProperties on object type (#523)
As stated in the documentation: https://swagger.io/docs/specification/data-models/dictionaries/ Freeform objects (objects with any value) can be defined by setting additionalProperties either to true or {}. Previously: foo?: { [key: string]: { [key: string]: any } } was the output instead of: foo?: { [key: string]: any } This is fixed and tested with this PR. Co-authored-by: fabio.wanner <[email protected]>
1 parent 39b8982 commit 635c0ad

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/transform/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ export function transformSchemaObj(node: any): string {
9494
// if additional properties, add an intersection with a generic map type
9595
let additionalProperties: string | undefined;
9696
if (node.additionalProperties) {
97-
if (node.additionalProperties === true) {
97+
if (node.additionalProperties === true || Object.keys(node.additionalProperties).length === 0) {
9898
additionalProperties = `{ [key: string]: any }`;
9999
} else if (typeof node.additionalProperties === "object") {
100100
const oneOf: any[] | undefined = (node.additionalProperties as any).oneOf || undefined; // TypeScript does a really bad job at inference here, so we enforce a type

tests/schema.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ describe("SchemaObject", () => {
163163
// boolean
164164
expect(transform({ additionalProperties: true })).toBe(`{ [key: string]: any }`);
165165

166+
// empty object
167+
expect(transform({ additionalProperties: {} })).toBe(`{ [key: string]: any }`);
168+
166169
// type
167170
expect(transform({ additionalProperties: { type: "string" } })).toBe(`{ [key: string]: string; }`);
168171

0 commit comments

Comments
 (0)