forked from Eywek/typoa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresolve.ts
More file actions
940 lines (908 loc) 路 30.2 KB
/
Copy pathresolve.ts
File metadata and controls
940 lines (908 loc) 路 30.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
import {
SymbolFlags,
Type,
Node,
ts,
Symbol as TsSymbol,
MethodDeclaration,
MethodSignature,
EnumDeclaration,
ParameterDeclaration,
PropertyDeclaration
} from 'ts-morph'
import { OpenAPIV3 } from 'openapi-types'
import { getConfig } from '.'
export function buildRef(name: string): string {
return `#/components/schemas/${name}`
}
function capitalizeFirstLetter(str: string): string {
return str.charAt(0).toUpperCase() + str.slice(1)
}
function resolveNullableType(
nonNullableType: Type,
isUndefined: boolean,
isNull: boolean,
spec: OpenAPIV3.Document
): OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject {
if (isNull) {
return appendMetaToResolvedType(resolve(nonNullableType, spec), {
nullable: true
})
}
return resolve(nonNullableType, spec)
}
function retrieveTypeName(type: Type): string {
if (type.isArray()) {
return `Array_${retrieveTypeName(type.getArrayElementType()!)}`
}
if (type.isIntersection()) {
return `Intersection_${type
.getIntersectionTypes()
.map(type => retrieveTypeName(type))
.join('_')}`
}
if (type.isUnion()) {
return `Union_${type
.getUnionTypes()
.map(type => retrieveTypeName(type))
.join('_')}`
}
const typeName = type.getSymbol()?.getName()
if (typeof typeName === 'undefined') {
return type.getText().replace(/-/g, '_').replace(/"/g, '')
}
if (typeName === '__type') {
const aliasName = type.getAliasSymbol()?.getName()
if (typeof aliasName !== 'undefined' && aliasName !== '__type') {
return aliasName
}
const declaration = type.getSymbolOrThrow().getDeclarations()[0]
if (declaration && Node.isLiteralTypeNode(declaration)) {
const aliasSymbol = declaration.getType().getAliasSymbol()
if (aliasSymbol) {
return aliasSymbol.getName()
}
}
// handle literal record
if (type.isObject()) {
return type
.getProperties()
.map(
prop =>
`${prop.getName()}_${retrieveTypeName(prop.getTypeAtLocation(getDeclarationForProperty(type, prop)))}`
)
.join('_')
}
}
return typeName
}
/**
* Returns true if the type could be identified
*/
function isTypeIdentifier(type: Type): boolean {
const kindName = type.getSymbol()?.getDeclarations()?.[0]?.getKindName()
return (
typeof kindName !== 'undefined' &&
kindName !== 'MappedType' &&
(type.isAnonymous() === false ||
typeof type.getAliasSymbol() !== 'undefined')
)
}
export function resolve(
type: Type,
spec: OpenAPIV3.Document,
resolveNullableTypeFn = resolveNullableType
): OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject {
// Promises
if (type.getSymbol()?.getEscapedName() === 'Promise') {
return resolve(type.getTypeArguments()[0], spec)
}
// Nullable
// We allow to override the behavior because for undefined values
// we want to avoid putting it in the `required` prop for objects
if (type.isNullable()) {
const unionTypes = type.getUnionTypes()
const isUndefined = unionTypes.some(t => t.isUndefined())
const isNull = unionTypes.some(t => t.isNull())
return resolveNullableTypeFn(
type.getNonNullableType(),
isUndefined,
isNull,
spec
)
}
// Handle types
if (type.isArray()) {
const elementType = type.getArrayElementTypeOrThrow()
// Handle empty arrays where element type is 'never'
if (elementType.getText() === 'never') {
return {
type: 'array',
items: {}
}
}
return {
type: 'array',
items: resolve(elementType, spec)
}
}
if (type.isBoolean()) {
return { type: 'boolean' }
}
if (type.isBooleanLiteral()) {
return {
type: 'boolean',
// @ts-expect-error https://github.com/microsoft/TypeScript/issues/26075
enum: [type.compilerType.intrinsicName]
}
}
if (type.isUnknown() || type.isAny()) {
spec.components!.schemas!.AnyValue = {
description: 'Can be any value',
nullable: true
}
return { $ref: buildRef('AnyValue') }
}
if (type.isTuple()) {
return {
type: 'array',
items: {
oneOf: type.getTupleElements().map(type => resolve(type, spec))
}
}
}
if (type.isEnum()) {
const symbol = type.getSymbolOrThrow()
const enumName = symbol.getName()
const declaration = symbol.getDeclarations()[0] as EnumDeclaration
// Add to spec components if not already resolved
if (typeof spec.components!.schemas![enumName] === 'undefined') {
const values = declaration.getMembers().map(m => m.getValue()!)
const names = declaration.getMembers().map(m => m.getName())
spec.components!.schemas![enumName] = {
type: typeof values[0] as 'string' | 'number',
enum: values,
...(getConfig()?.openapi?.xEnumVarnames
? { 'x-enum-varnames': names }
: {})
}
}
return { $ref: buildRef(enumName) }
}
if (type.isClassOrInterface() || type.isObject()) {
let typeName = retrieveTypeName(type)
// Special case for date
if (typeName === 'Date') {
return { type: 'string', format: 'date-time' }
}
// Special case for Buffer, treat as binary string
if (typeName === 'Buffer') {
return { type: 'string', format: 'binary' }
}
// Handle mapped types
const helperName = type.getAliasSymbol()?.getEscapedName()
if (
helperName === 'Partial' ||
helperName === 'Omit' ||
helperName === 'Pick' ||
helperName === 'Promise'
) {
const typeArguments = type.getAliasTypeArguments()
const subjectType = typeArguments[0]
if (isTypeIdentifier(subjectType) === false) {
return resolveMappedObjectType(
type,
spec,
helperName,
subjectType,
typeArguments
)
}
switch (helperName) {
case 'Omit':
case 'Pick':
const args = typeArguments[1].isUnion()
? typeArguments[1]
.getUnionTypes()
.map(t =>
capitalizeFirstLetter(
String(t.getAliasSymbol()?.getName() ?? t.getLiteralValue())
)
)
: [
capitalizeFirstLetter(
String(typeArguments[1].getLiteralValue())
)
]
typeName = `${retrieveTypeName(subjectType)}_With${helperName === 'Omit' ? 'out' : ''}_${args.join('_')}`
break
case 'Partial':
typeName = `${retrieveTypeName(subjectType)}_Partial`
break
case 'Promise':
typeName = retrieveTypeName(subjectType)
break
}
} else if (
(type.getAliasTypeArguments().length === 1 ||
type.getTypeArguments().length === 1) &&
isTypeIdentifier(
type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
)
) {
// i.e. Serialized<Datasource> -> Serialized_Datasource
const subjectType =
type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
const name =
type.getSymbol()?.getEscapedName() !== '__type'
? type.getSymbol()?.getEscapedName()
: helperName
typeName = `${name}_${retrieveTypeName(subjectType)}`
} else if (
(type.getAliasTypeArguments().length === 1 ||
type.getTypeArguments().length === 1) &&
(
type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
)?.isAnonymous() === true
) {
// i.e. Serialized<{ datasource: Datasource }> -> Serialized_datasource
const subjectType =
type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
const name =
type.getSymbol()?.getEscapedName() !== '__type'
? type.getSymbol()?.getEscapedName()
: helperName
typeName = `${name}_${retrieveTypeName(subjectType)}`
} else if (
(type.getAliasTypeArguments().length === 1 ||
type.getTypeArguments().length === 1) &&
(
type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
)?.isUnion() === true
) {
// i.e. Serialized<WorkerDatasource | ProxyDatasource> -> Serialized_Union_WorkerDatasource_ProxyDatasource
const subjectType =
type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
const name =
type.getSymbol()?.getEscapedName() !== '__type'
? type.getSymbol()?.getEscapedName()
: helperName
typeName = `${name}_Union_${subjectType
.getUnionTypes()
.map(t => retrieveTypeName(t))
.join('_')}`
} else if (
(type.getAliasTypeArguments().length === 1 ||
type.getTypeArguments().length === 1) &&
(
type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
)?.isIntersection() === true
) {
// i.e. Serialized<WorkerDatasource & ProxyDatasource> -> Serialized_Intersection_WorkerDatasource_ProxyDatasource
const subjectType =
type.getTypeArguments()[0] ?? type.getAliasTypeArguments()[0]
const name =
type.getSymbol()?.getEscapedName() !== '__type'
? type.getSymbol()?.getEscapedName()
: helperName
typeName = `${name}_Intersection_${subjectType
.getIntersectionTypes()
.map(t => retrieveTypeName(t))
.join('_')}`
} else if (isTypeIdentifier(type) === false) {
// For other and anonymous types, don't use ref
return resolveObjectType(type, spec)
}
if (typeof spec.components!.schemas![typeName] === 'undefined') {
if (
helperName === 'Partial' ||
helperName === 'Omit' ||
helperName === 'Pick'
) {
const typeArguments = type.getAliasTypeArguments()
const subjectType = typeArguments[0]
spec.components!.schemas![typeName] = resolveMappedObjectType(
type,
spec,
helperName,
subjectType,
typeArguments
)
} else {
spec.components!.schemas![typeName] = resolveObjectType(type, spec)
}
}
// Return
return { $ref: buildRef(typeName) }
}
if (type.isIntersection()) {
return {
allOf: type.getIntersectionTypes().map(type => resolve(type, spec))
}
}
if (type.isUnion()) {
const values = type.getUnionTypes().map(type => resolve(type, spec))
return {
oneOf: values
}
}
if (
(type.isEnumLiteral() || type.isLiteral()) &&
type.compilerType.isLiteral()
) {
return {
type: type.isNumberLiteral() ? 'number' : 'string',
enum: [type.compilerType.value]
}
}
const typeName = type.getText() as 'string' | 'number' | 'void' | 'null'
// map null or void as object so that the schema stays valid
if (typeName === 'null' || typeName === 'void') {
return { type: 'object' as any }
}
return {
type: typeName
}
}
type ResolvePropertiesReturnType = Required<
Pick<OpenAPIV3.BaseSchemaObject, 'properties'>
> & {
required?: string[]
additionalProperties?:
| OpenAPIV3.ReferenceObject
| OpenAPIV3.SchemaObject
| boolean
}
function resolveProperties(
type: Type,
spec: OpenAPIV3.Document
): ResolvePropertiesReturnType {
const result: ResolvePropertiesReturnType = type.getProperties().reduce(
(schema, property) => {
const node = getDeclarationForProperty(type, property)
const propertyType = property.getTypeAtLocation(node)
if (
Node.isMethodDeclaration(node) ||
Node.isMethodSignature(node) ||
propertyType.getCallSignatures().length > 0
) {
return schema // ignore functions
}
const jsDocTags = property.compilerSymbol.getJsDocTags()
// Handle readonly / getters props / @readonly tag
const modifierFlags =
property.getValueDeclaration()?.getCombinedModifierFlags() ??
node.getCombinedModifierFlags()
const hasFlags = (flag: SymbolFlags) =>
property.hasFlags(flag) || node.getSymbol()?.hasFlags(flag)
const isReadonly =
modifierFlags === ts.ModifierFlags.Readonly ||
(hasFlags(SymbolFlags.GetAccessor) === true &&
hasFlags(SymbolFlags.SetAccessor) === false) ||
jsDocTags.some(tag => tag.name === 'readonly')
// Required by default
let required = hasFlags(SymbolFlags.Optional) ? false : true
// We resolve the property, overriding the behavior for nullable values
// if the value is optional (isUndefined = true) we don't push in the required array
const resolvedType = resolve(
propertyType,
spec,
(nonNullableType, isUndefined, isNull, spec) => {
if (isUndefined) {
required = false
}
if (isNull) {
return appendMetaToResolvedType(resolve(nonNullableType, spec), {
nullable: true
})
}
return resolve(nonNullableType, spec)
}
)
if (isReadonly) {
appendMetaToResolvedType(resolvedType, {
readOnly: true
})
}
if (jsDocTags.some(tag => tag.name === 'writeonly')) {
appendMetaToResolvedType(resolvedType, {
writeOnly: true
})
}
// JSDoc tags
appendJsDocTags(jsDocTags, resolvedType)
// initializer
if (Node.isPropertyDeclaration(node)) {
if (appendInitializer(node, resolvedType)) {
required = false
}
}
// Add to spec
if (
'type' in resolvedType &&
(resolvedType.type as any) === 'undefined'
) {
return schema
}
schema.properties[property.getName()] = resolvedType
if (required) {
schema.required.push(property.getName())
}
return schema
},
{ properties: {}, required: [] } as Required<
Omit<ResolvePropertiesReturnType, 'additionalProperties'>
>
)
if (result.required?.length === 0) {
// OpenAPI don't want the required[] prop if it's empty
delete result.required
}
// Check for @additionalproperties JSDoc tag on the type declaration
// First try the alias symbol (for type aliases), then the regular symbol (for interfaces)
const aliasSymbol = type.getAliasSymbol()
const typeSymbol = type.getSymbol()
let additionalPropertiesFromJSDoc: boolean | undefined = undefined
// Check alias symbol first (for type aliases like `type X = { ... }`)
if (aliasSymbol) {
const jsDocTags = aliasSymbol.compilerSymbol.getJsDocTags()
const additionalPropsTag = jsDocTags.find(
tag => tag.name === 'additionalproperties'
)
if (additionalPropsTag && additionalPropsTag.text) {
const tagText = additionalPropsTag.text
.map(t => t.text)
.join(' ')
.trim()
.toLowerCase()
if (tagText === 'true') {
additionalPropertiesFromJSDoc = true
} else if (tagText === 'false') {
additionalPropertiesFromJSDoc = false
}
}
}
// If not found in alias symbol, check the regular symbol (for interfaces)
if (additionalPropertiesFromJSDoc === undefined && typeSymbol) {
const jsDocTags = typeSymbol.compilerSymbol.getJsDocTags()
const additionalPropsTag = jsDocTags.find(
tag => tag.name === 'additionalproperties'
)
if (additionalPropsTag && additionalPropsTag.text) {
const tagText = additionalPropsTag.text
.map(t => t.text)
.join(' ')
.trim()
.toLowerCase()
if (tagText === 'true') {
additionalPropertiesFromJSDoc = true
} else if (tagText === 'false') {
additionalPropertiesFromJSDoc = false
}
}
}
// If JSDoc tag is explicitly set, use it (overrides index signature detection)
if (additionalPropertiesFromJSDoc !== undefined) {
result.additionalProperties = additionalPropertiesFromJSDoc
return result
} else {
// Check for index signatures regardless of whether explicit properties exist
const stringIndexType = type.getStringIndexType()
const numberIndexType = type.getNumberIndexType()
// Handle mapped types and objects with index signatures (ex: { [key: string]: any } or Record<string, any>)
if (
(typeof stringIndexType !== 'undefined' &&
stringIndexType.getText() !== 'never') ||
(typeof numberIndexType !== 'undefined' &&
numberIndexType.getText() !== 'never')
) {
result.additionalProperties = resolve(
stringIndexType ?? numberIndexType!,
spec
)
} else {
// Edge cases where TypeScript's getStringIndexType() might fail to detect
// index signatures in complex type scenarios (e.g., after type transformations)
// Check if the type represents a Record-like structure that should have additionalProperties
const typeText = type.getText()
// Handle cases where the type might be a transformed Record type
if (typeSymbol?.getName() === '__type' || typeText.includes('Record<')) {
// For anonymous types that might be transformed Record types,
// check if the structure suggests it should have additional properties
// Check the type's apparent type for index signatures
const apparentType = type.getApparentType()
if (apparentType && apparentType !== type) {
const apparentStringIndexType = apparentType.getStringIndexType()
const apparentNumberIndexType = apparentType.getNumberIndexType()
if (
(typeof apparentStringIndexType !== 'undefined' &&
apparentStringIndexType.getText() !== 'never') ||
(typeof apparentNumberIndexType !== 'undefined' &&
apparentNumberIndexType.getText() !== 'never')
) {
result.additionalProperties = resolve(
apparentStringIndexType ?? apparentNumberIndexType!,
spec
)
}
}
} else {
// No index signature found and no JSDoc tag - default to false (no additional properties allowed)
result.additionalProperties = false
}
}
return result
}
}
/**
* Resolves mapped object types when applied to types with toJSON methods.
*
* When a mapped type is applied to a class/interface that has a toJSON method, we need to
* apply the transformation to the toJSON return type rather than the original object properties.
* This is because the toJSON method defines the actual serialized structure.
*
* @returns The resolved OpenAPI schema with transformations applied
*/
function resolveMappedObjectType(
type: Type,
spec: OpenAPIV3.Document,
helperName: string,
subjectType: Type,
typeArguments?: Type[]
): OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject {
// Check if we're omitting or picking the toJSON property
let shouldSkipToJSON = false
if (
(helperName === 'Omit' || helperName === 'Pick') &&
typeArguments &&
typeArguments.length > 1
) {
const omittedKeys = typeArguments[1].isUnion()
? typeArguments[1].getUnionTypes().map(t => String(t.getLiteralValue()))
: [String(typeArguments[1].getLiteralValue())]
if (helperName === 'Omit' && omittedKeys.includes('toJSON')) {
// If we're omitting toJSON, don't follow it to avoid infinite loops
shouldSkipToJSON = true
} else if (helperName === 'Pick' && !omittedKeys.includes('toJSON')) {
// If we're picking properties and toJSON is not in the list, skip it
shouldSkipToJSON = true
}
}
// Check if the subject type has a toJSON method
const toJSONProperty = subjectType.getProperty('toJSON')
if (toJSONProperty && !shouldSkipToJSON) {
const node = getDeclarationForProperty(subjectType, toJSONProperty) as
| MethodDeclaration
| MethodSignature
const toJSONReturnType = resolve(node.getReturnType(), spec)
// Apply mapped type transformation to the toJSON return type
if ('$ref' in toJSONReturnType) {
// For reference types, we need to create a new schema with the transformation applied
// This is more complex, so for now we'll fall back to normal resolution
return resolveObjectType(type, spec)
}
if (toJSONReturnType.type === 'object' && toJSONReturnType.properties) {
const transformedSchema = { ...toJSONReturnType }
switch (helperName) {
case 'Partial':
// Make all properties optional by removing them from required array
delete transformedSchema.required
break
// Note: Omit and Pick are not implemented because they require understanding
// the relationship between input properties and toJSON output properties,
// which cannot be determined generically without analyzing the toJSON implementation
}
return transformedSchema
}
}
// Fall back to normal object resolution if no toJSON or transformation failed
return resolveObjectType(type, spec)
}
/**
* Checks if a type represents an interface that extends other interfaces
*/
function hasInterfaceInheritance(type: Type): boolean {
const symbol = type.getSymbol()
if (!symbol) return false
const declarations = symbol.getDeclarations()
return declarations.some(
decl => Node.isInterfaceDeclaration(decl) && decl.getExtends().length > 0
)
}
/**
* Gets the base interfaces that an interface extends
*/
function getBaseInterfaces(
type: Type,
spec: OpenAPIV3.Document
): OpenAPIV3.ReferenceObject[] {
const symbol = type.getSymbol()
if (!symbol) return []
const baseRefs: OpenAPIV3.ReferenceObject[] = []
const declarations = symbol.getDeclarations()
for (const decl of declarations) {
if (!Node.isInterfaceDeclaration(decl)) continue
const extendsExpressions = decl.getExtends()
for (const extendsExpr of extendsExpressions) {
const baseType = extendsExpr.getType()
const baseRef = resolve(baseType, spec)
if ('$ref' in baseRef) {
baseRefs.push(baseRef)
}
}
}
return baseRefs
}
/**
* Gets only the properties declared directly in an interface (not inherited)
*/
function getOwnInterfaceProperties(
type: Type,
spec: OpenAPIV3.Document
): ResolvePropertiesReturnType {
const symbol = type.getSymbol()
if (!symbol) {
// Fallback to normal property resolution
return resolveProperties(type, spec)
}
const declarations = symbol.getDeclarations()
const interfaceDecl = declarations.find(decl =>
Node.isInterfaceDeclaration(decl)
)
if (!interfaceDecl) {
// Fallback to normal property resolution
return resolveProperties(type, spec)
}
const result: ResolvePropertiesReturnType = {
properties: {},
required: []
}
// Get only the properties declared in this interface
const propertySignatures = interfaceDecl.getProperties()
for (const propSig of propertySignatures) {
const propName = propSig.getName()
const propType = propSig.getType()
let required = !propSig.hasQuestionToken()
// Resolve the property type
const resolvedType = resolve(
propType,
spec,
(nonNullableType, isUndefined, isNull, spec) => {
if (isUndefined) {
required = false
}
if (isNull) {
return appendMetaToResolvedType(resolve(nonNullableType, spec), {
nullable: true
})
}
return resolve(nonNullableType, spec)
}
)
// Handle JSDoc tags
const jsDocTags = propSig.getSymbol()?.compilerSymbol.getJsDocTags() ?? []
appendJsDocTags(jsDocTags, resolvedType)
// Add to properties
if (
!('type' in resolvedType && (resolvedType.type as any) === 'undefined')
) {
result.properties[propName] = resolvedType
if (required) {
result.required!.push(propName)
}
}
}
// Handle method signatures (but ignore them like in resolveProperties)
// Methods are already filtered out by only looking at property signatures
if (result.required!.length === 0) {
delete result.required
}
return result
}
/**
* Resolves object types (classes, interfaces, and plain objects) to OpenAPI schemas.
*
* This function handles the conversion of TypeScript object types to OpenAPI schema objects.
* It has special handling for classes/interfaces that have a toJSON method - in such cases,
* it resolves to the return type of the toJSON method instead of the object's properties.
* For regular objects without toJSON, it resolves all properties using resolveProperties.
*
* @returns The resolved OpenAPI schema (either a reference or inline schema)
*/
function resolveObjectType(
type: Type,
spec: OpenAPIV3.Document
): OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject {
// toJSON methods
const toJSONProperty = type.getProperty('toJSON')
if (toJSONProperty) {
const node = getDeclarationForProperty(type, toJSONProperty) as
| MethodDeclaration
| MethodSignature
return resolve(node.getReturnType(), spec)
}
// Check for interface inheritance
if (!hasInterfaceInheritance(type)) {
const propertiesResult = resolveProperties(type, spec)
// Check for @additionalproperties JSDoc tag on the type declaration
// This needs to be checked here because resolveProperties might not have access to the alias symbol
const aliasSymbol = type.getAliasSymbol()
const typeSymbol = type.getSymbol()
let additionalPropertiesFromJSDoc: boolean | undefined = undefined
// Check alias symbol first (for type aliases like `type X = { ... }`)
if (aliasSymbol) {
const jsDocTags = aliasSymbol.compilerSymbol.getJsDocTags()
const additionalPropsTag = jsDocTags.find(
tag => tag.name === 'additionalproperties'
)
if (additionalPropsTag && additionalPropsTag.text) {
const tagText = additionalPropsTag.text
.map(t => t.text)
.join(' ')
.trim()
.toLowerCase()
if (tagText === 'true') {
additionalPropertiesFromJSDoc = true
} else if (tagText === 'false') {
additionalPropertiesFromJSDoc = false
}
}
}
// If not found in alias symbol, check the regular symbol (for interfaces)
if (additionalPropertiesFromJSDoc === undefined && typeSymbol) {
const jsDocTags = typeSymbol.compilerSymbol.getJsDocTags()
const additionalPropsTag = jsDocTags.find(
tag => tag.name === 'additionalproperties'
)
if (additionalPropsTag && additionalPropsTag.text) {
const tagText = additionalPropsTag.text
.map(t => t.text)
.join(' ')
.trim()
.toLowerCase()
if (tagText === 'true') {
additionalPropertiesFromJSDoc = true
} else if (tagText === 'false') {
additionalPropertiesFromJSDoc = false
}
}
}
// Override additionalProperties if JSDoc tag is set
if (additionalPropertiesFromJSDoc !== undefined) {
propertiesResult.additionalProperties = additionalPropertiesFromJSDoc
} else if (propertiesResult.additionalProperties === undefined) {
// If no JSDoc tag and no index signature, default to false
propertiesResult.additionalProperties = false
}
return {
type: 'object',
...propertiesResult
}
}
const baseRefs = getBaseInterfaces(type, spec)
// If there are no base interfaces, fall back to normal resolution
if (baseRefs.length === 0) {
return {
type: 'object',
...resolveProperties(type, spec)
}
}
// Shallow copy to avoid mutating the original baseRefs array
const allOfElements = Array.from<
OpenAPIV3.ReferenceObject | OpenAPIV3.SchemaObject
>(baseRefs)
const ownProps = getOwnInterfaceProperties(type, spec)
// Add own properties if any exist
if (
Object.keys(ownProps.properties).length > 0 ||
ownProps.additionalProperties
) {
allOfElements.push({
type: 'object',
...ownProps
})
}
return {
allOf: allOfElements
}
}
function getDeclarationForProperty(rootType: Type, property: TsSymbol): Node {
const firstDeclaration = property.getDeclarations()[0] as Node | undefined // Can be undefined with Record<'foo', string>.foo
return firstDeclaration ?? rootType.getSymbolOrThrow().getDeclarations()[0]
}
export function appendMetaToResolvedType(
type:
| OpenAPIV3.ReferenceObject
| OpenAPIV3.ArraySchemaObject
| OpenAPIV3.NonArraySchemaObject,
metas: Partial<OpenAPIV3.NonArraySchemaObject>
):
| OpenAPIV3.ReferenceObject
| OpenAPIV3.ArraySchemaObject
| OpenAPIV3.NonArraySchemaObject {
if ('$ref' in type) {
// siblings aren't allowed with ref (ex: for readonly) see https://stackoverflow.com/a/51402417
const ref = type.$ref
// @ts-expect-error $ref isn't optional but we need to delete it to mutate the type
delete type.$ref
return Object.assign(type, {
// Mutate type deleting $ref
allOf: [{ $ref: ref }],
...metas
})
}
return Object.assign(type, metas)
}
export function appendJsDocTags(
jsDocTags: ts.JSDocTagInfo[],
resolvedType:
| OpenAPIV3.ReferenceObject
| OpenAPIV3.ArraySchemaObject
| OpenAPIV3.NonArraySchemaObject
) {
const supportedTags = [
'format',
'example',
'description',
'title',
'pattern',
'minimum',
'maximum',
'minLength',
'maxLength',
'minItems',
'maxItems'
]
const numericTags = [
'minimum',
'maximum',
'minLength',
'maxLength',
'minItems',
'maxItems'
]
for (const tag of jsDocTags) {
if (!supportedTags.includes(tag.name) || !tag.text) {
continue
}
const textValue = tag.text.map(t => t.text).join('\n')
const value = numericTags.includes(tag.name)
? parseFloat(textValue)
: textValue
appendMetaToResolvedType(resolvedType, {
[tag.name]: value
})
}
}
export function appendInitializer(
node: ParameterDeclaration | PropertyDeclaration,
schema:
| OpenAPIV3.ReferenceObject
| OpenAPIV3.ArraySchemaObject
| OpenAPIV3.NonArraySchemaObject
): boolean {
// Default value
const initializer = node.getInitializer()
const initializerType = initializer?.getType()
if (initializerType?.isLiteral()) {
const initializerLiteralType = initializerType.compilerType as
| ts.StringLiteralType
| ts.NumberLiteralType
| ts.TrueLiteral
| ts.FalseLiteral
| ts.NullLiteral
let value: boolean | string | number | null
if ('value' in initializerLiteralType) {
value = initializerLiteralType.value
} else if (initializerLiteralType.kind === ts.SyntaxKind.TrueKeyword) {
value = true
} else if (initializerLiteralType.kind === ts.SyntaxKind.FalseKeyword) {
value = false
} else {
value = null
}
appendMetaToResolvedType(schema, { default: value })
return true
}
return false
}