@@ -5,6 +5,11 @@ import { SchemaTSContext, type SchemaTSOptions } from "./context";
5
5
import type { JSONSchema } from "./types" ;
6
6
import { isValidIdentifier , isValidIdentifierCamelized , toCamelCase , toPascalCase } from "./utils" ;
7
7
8
+ const safeProperty = ( str : string ) : string => {
9
+ if ( str . match ( / \. / ) ) return str . replace ( / \. / g, '_' ) ;
10
+ return str ;
11
+ } ;
12
+
8
13
const identifier = ( name : string , typeAnnotation : t . TSTypeAnnotation ) => {
9
14
const i = t . identifier ( name ) ;
10
15
i . typeAnnotation = typeAnnotation ;
@@ -32,7 +37,7 @@ export function generateTypeScript(schema: JSONSchema, options?: Partial<SchemaT
32
37
// Process both $defs and definitions
33
38
const definitions = schema . $defs || schema . definitions || { } ;
34
39
for ( const key in definitions ) {
35
- interfaces . push ( createInterfaceDeclaration ( ctx , toPascalCase ( key ) , definitions [ key ] ) ) ;
40
+ interfaces . push ( createInterfaceDeclaration ( ctx , key , definitions [ key ] ) ) ;
36
41
}
37
42
} catch ( e ) {
38
43
console . error ( 'Error processing interfaces' ) ;
@@ -45,7 +50,7 @@ export function generateTypeScript(schema: JSONSchema, options?: Partial<SchemaT
45
50
console . error ( 'schema or options require a title' ) ;
46
51
return '' ; // Ensure there's a return on error condition
47
52
}
48
- interfaces . push ( createInterfaceDeclaration ( ctx , toPascalCase ( title ) , schema ) ) ;
53
+ interfaces . push ( createInterfaceDeclaration ( ctx , title , schema ) ) ;
49
54
return generate ( t . file ( t . program ( interfaces ) ) ) . code ;
50
55
}
51
56
@@ -94,14 +99,14 @@ function createInterfaceDeclaration(
94
99
const combinedType = types . length > 1 ? t . tsUnionType ( types ) : types [ 0 ] ;
95
100
96
101
// Create a type alias instead of an interface if we're only handling these constructs
97
- const typeAlias = t . tsTypeAliasDeclaration ( t . identifier ( name ) , null , combinedType ) ;
102
+ const typeAlias = t . tsTypeAliasDeclaration ( t . identifier ( toPascalCase ( safeProperty ( name ) ) ) , null , combinedType ) ;
98
103
return t . exportNamedDeclaration ( typeAlias ) ;
99
104
}
100
105
101
106
// Finally, create the interface declaration if there are any body elements
102
107
if ( bodyElements . length > 0 ) {
103
108
const interfaceDeclaration = t . tsInterfaceDeclaration (
104
- t . identifier ( name ) ,
109
+ t . identifier ( toPascalCase ( safeProperty ( name ) ) ) ,
105
110
null ,
106
111
[ ] ,
107
112
t . tsInterfaceBody ( bodyElements )
@@ -110,12 +115,21 @@ function createInterfaceDeclaration(
110
115
}
111
116
112
117
if ( schema . type ) {
113
- return t . exportNamedDeclaration ( t . tsTypeAliasDeclaration ( t . identifier ( name ) , null , getTypeForProp ( ctx , schema , [ ] , schema ) ) ) ;
118
+ return t . exportNamedDeclaration ( t . tsTypeAliasDeclaration ( t . identifier ( toPascalCase ( safeProperty ( name ) ) ) , null , getTypeForProp ( ctx , schema , [ ] , schema ) ) ) ;
119
+ }
120
+
121
+ console . log ( name ) ;
122
+ if ( ctx . options . overrides && Object . prototype . hasOwnProperty . call ( ctx . options . overrides , name ) ) {
123
+ return t . exportNamedDeclaration ( t . tsTypeAliasDeclaration ( t . identifier ( toPascalCase ( safeProperty ( name ) ) ) , null ,
124
+ getTypeForProp ( ctx , ctx . options . overrides [ name ] , [ ] , schema )
125
+ ) ) ;
114
126
}
115
127
128
+
129
+
116
130
// Fallback to exporting a basic type if nothing else is possible
117
131
console . warn ( `No properties or type definitions found for ${ name } , defaulting to 'any'.` ) ;
118
- return t . exportNamedDeclaration ( t . tsTypeAliasDeclaration ( t . identifier ( name ) , null , t . tsAnyKeyword ( ) ) ) ;
132
+ return t . exportNamedDeclaration ( t . tsTypeAliasDeclaration ( t . identifier ( toPascalCase ( safeProperty ( name ) ) ) , null , t . tsAnyKeyword ( ) ) ) ;
119
133
}
120
134
121
135
@@ -133,7 +147,7 @@ function createPropertySignature(
133
147
let identifier : t . Identifier | t . StringLiteral ;
134
148
let isIdent : boolean ;
135
149
if ( ctx . options . camelCase ) {
136
- isIdent = isValidIdentifierCamelized ( key ) ;
150
+ isIdent = isValidIdentifierCamelized ( key ) ;
137
151
} else {
138
152
isIdent = isValidIdentifier ( key ) ;
139
153
}
@@ -162,8 +176,8 @@ function getTypeForProp(ctx: SchemaTSContext, prop: JSONSchema, required: string
162
176
163
177
if ( prop . type ) {
164
178
if ( Array . isArray ( prop . type ) ) {
165
- const arrayType = prop . type . map ( type => getTypeForProp ( ctx , { type, items : prop . items } , [ ] , schema ) ) ;
166
- return t . tsUnionType ( arrayType ) ;
179
+ const arrayType = prop . type . map ( type => getTypeForProp ( ctx , { type, items : prop . items } , [ ] , schema ) ) ;
180
+ return t . tsUnionType ( arrayType ) ;
167
181
}
168
182
169
183
switch ( prop . type ) {
@@ -219,12 +233,17 @@ function getTypeForProp(ctx: SchemaTSContext, prop: JSONSchema, required: string
219
233
220
234
}
221
235
222
- function getTypeReferenceFromSchema ( schema : JSONSchema , definitionName : string ) : t . TSType | null {
236
+ function getTypeReferenceFromSchema ( ctx : SchemaTSContext , schema : JSONSchema , definitionName : string ) : t . TSType | null {
223
237
if ( definitionName ) {
238
+
239
+ // if (ctx.options.overrides && Object.prototype.hasOwnProperty.call(ctx.options.overrides, definitionName)) {
240
+ // return getTypeForProp(ctx, ctx.options.overrides[definitionName], [], schema);
241
+ // }
242
+
224
243
if ( schema . $defs && schema . $defs [ definitionName ] ) {
225
- return t . tsTypeReference ( t . identifier ( toPascalCase ( definitionName ) ) ) ;
244
+ return t . tsTypeReference ( t . identifier ( toPascalCase ( safeProperty ( definitionName ) ) ) ) ;
226
245
} else if ( schema . definitions && schema . definitions [ definitionName ] ) {
227
- return t . tsTypeReference ( t . identifier ( toPascalCase ( definitionName ) ) ) ;
246
+ return t . tsTypeReference ( t . identifier ( toPascalCase ( safeProperty ( definitionName ) ) ) ) ;
228
247
}
229
248
}
230
249
return null ; // Return null if no type reference is found
@@ -236,13 +255,13 @@ function resolveRefType(ctx: SchemaTSContext, ref: string, schema: JSONSchema):
236
255
const definitionName = path . pop ( ) ;
237
256
238
257
// Try to resolve the type reference from the local schema
239
- const localTypeReference = getTypeReferenceFromSchema ( schema , definitionName ) ;
258
+ const localTypeReference = getTypeReferenceFromSchema ( ctx , schema , definitionName ) ;
240
259
if ( localTypeReference ) {
241
260
return localTypeReference ;
242
261
}
243
262
244
263
// Try to resolve the type reference from the root schema
245
- const rootTypeReference = getTypeReferenceFromSchema ( ctx . root , definitionName ) ;
264
+ const rootTypeReference = getTypeReferenceFromSchema ( ctx , ctx . root , definitionName ) ;
246
265
if ( rootTypeReference ) {
247
266
return rootTypeReference ;
248
267
}
0 commit comments