|
1 | 1 | import type { OpenAPIV3_1 as OpenAPI } from 'openapi-types'; |
| 2 | +import { toPascalCase } from './case.js'; |
2 | 3 |
|
3 | 4 | export interface TypedDictResult { |
4 | 5 | definition: string; |
5 | 6 | typingImports: Set<string>; |
6 | 7 | } |
7 | 8 |
|
8 | 9 | export function convertToTypedDict(name: string, schema: OpenAPI.SchemaObject | OpenAPI.ReferenceObject): TypedDictResult { |
| 10 | + name = toPascalCase(name); |
9 | 11 | const typingImports = new Set<string>(['TypedDict']); |
10 | 12 | const extraDefs: string[] = []; |
11 | 13 |
|
12 | | - function toPascal(str: string): string { |
13 | | - return str |
14 | | - .replace(/[^a-zA-Z0-9]+/g, ' ') |
15 | | - .split(' ') |
16 | | - .filter(Boolean) |
17 | | - .map((p) => p.charAt(0).toUpperCase() + p.slice(1)) |
18 | | - .join(''); |
19 | | - } |
20 | | - |
21 | 14 | function flatten(s: OpenAPI.SchemaObject | OpenAPI.ReferenceObject): { bases: string[]; schema: OpenAPI.SchemaObject | null } { |
22 | 15 | if ('$ref' in s) { |
23 | 16 | const match = s.$ref.match(/^#\/components\/schemas\/(.+)$/); |
24 | | - return { bases: [match?.[1] ?? s.$ref], schema: null }; |
| 17 | + const refName = match?.[1] ?? s.$ref; |
| 18 | + return { bases: [toPascalCase(refName)], schema: null }; |
25 | 19 | } |
26 | 20 |
|
27 | 21 | if ('allOf' in s && Array.isArray(s.allOf)) { |
@@ -51,7 +45,7 @@ export function convertToTypedDict(name: string, schema: OpenAPI.SchemaObject | |
51 | 45 | const fields: string[] = []; |
52 | 46 | const attrLines: string[] = []; |
53 | 47 | for (const [key, value] of Object.entries(props)) { |
54 | | - const typeStr = toType(value as any, `${className}${toPascal(key)}`); |
| 48 | + const typeStr = toType(value as any, `${className}${toPascalCase(key)}`); |
55 | 49 | const desc = (value as any).description; |
56 | 50 | if (required.has(key)) { |
57 | 51 | typingImports.add('Required'); |
@@ -95,7 +89,7 @@ export function convertToTypedDict(name: string, schema: OpenAPI.SchemaObject | |
95 | 89 | if ('$ref' in s) { |
96 | 90 | const match = s.$ref.match(/^#\/components\/schemas\/(.+)$/); |
97 | 91 | const refName = match?.[1] ?? s.$ref; |
98 | | - return `${refName}`; |
| 92 | + return `${toPascalCase(refName)}`; |
99 | 93 | } |
100 | 94 |
|
101 | 95 | if ('oneOf' in s && Array.isArray(s.oneOf)) { |
@@ -169,7 +163,7 @@ export function convertToTypedDict(name: string, schema: OpenAPI.SchemaObject | |
169 | 163 | const fields: string[] = []; |
170 | 164 | const attrLines: string[] = []; |
171 | 165 | for (const [key, value] of Object.entries(props)) { |
172 | | - const typeStr = toType(value as any, `${name}${toPascal(key)}`); |
| 166 | + const typeStr = toType(value as any, `${name}${toPascalCase(key)}`); |
173 | 167 | const desc = (value as any).description; |
174 | 168 | if (required.has(key)) { |
175 | 169 | typingImports.add('Required'); |
|
0 commit comments