Skip to content

Commit e47a645

Browse files
authored
Merge branch 'main' into dependabot/github_actions/changesets/action-2.1.2
2 parents 7339cb4 + c31c5fa commit e47a645

3 files changed

Lines changed: 70 additions & 0 deletions

File tree

src/schema-parser/schema-parser.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ export class SchemaParser {
215215
) {
216216
this.schema.type = SCHEMA_TYPES.ARRAY;
217217
}
218+
if (this.schema.additionalProperties && !this.schema.type) {
219+
this.schema.type = SCHEMA_TYPES.OBJECT;
220+
}
218221
// schema is enum with one null value
219222
if (
220223
Array.isArray(this.schema.enum) &&
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import * as fs from "node:fs/promises";
2+
import * as os from "node:os";
3+
import * as path from "node:path";
4+
import { afterAll, beforeAll, describe, expect, test } from "vitest";
5+
import { generateApi } from "../../../src/index.js";
6+
7+
describe("additionalProperties without type", async () => {
8+
let tmpdir = "";
9+
10+
beforeAll(async () => {
11+
tmpdir = await fs.mkdtemp(path.join(os.tmpdir(), "swagger-typescript-api"));
12+
});
13+
14+
afterAll(async () => {
15+
await fs.rm(tmpdir, { recursive: true });
16+
});
17+
18+
test("infers dictionary types", async () => {
19+
await generateApi({
20+
fileName: "schema",
21+
input: path.resolve(import.meta.dirname, "schema.json"),
22+
output: tmpdir,
23+
silent: true,
24+
generateClient: false,
25+
});
26+
27+
const content = await fs.readFile(path.join(tmpdir, "schema.ts"), {
28+
encoding: "utf8",
29+
});
30+
31+
expect(content).toContain("values: Record<string, string[]>;");
32+
expect(content).toContain("open?: Record<string, any>;");
33+
expect(content).toContain("closed?: any;");
34+
});
35+
});
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
{
2+
"openapi": "3.0.0",
3+
"info": {
4+
"title": "Additional properties without an explicit type",
5+
"version": "1.0.0"
6+
},
7+
"paths": {},
8+
"components": {
9+
"schemas": {
10+
"Widget": {
11+
"type": "object",
12+
"required": ["values"],
13+
"properties": {
14+
"values": {
15+
"additionalProperties": {
16+
"type": "array",
17+
"items": {
18+
"type": "string"
19+
}
20+
}
21+
},
22+
"open": {
23+
"additionalProperties": true
24+
},
25+
"closed": {
26+
"additionalProperties": false
27+
}
28+
}
29+
}
30+
}
31+
}
32+
}

0 commit comments

Comments
 (0)