1- import ts , { skipPartiallyEmittedExpressions } from "typescript" ;
1+ import ts from "typescript" ;
22
33import type { OpenApi } from "../../../types" ;
44import { FeatureDevelopmentError } from "../../Exception" ;
@@ -10,6 +10,20 @@ import type { AnySchema, ArraySchema, ObjectSchema, PrimitiveSchema } from "../t
1010import type * as Walker from "../Walker" ;
1111import * as ExternalDocumentation from "./ExternalDocumentation" ;
1212
13+ const nullable = ( factory : Factory . Type , typeNode : ts . TypeNode , nullable : boolean ) : ts . TypeNode => {
14+ if ( nullable ) {
15+ return factory . UnionTypeNode . create ( {
16+ typeNodes : [
17+ typeNode ,
18+ factory . TypeNode . create ( {
19+ type : "null" ,
20+ } ) ,
21+ ] ,
22+ } ) ;
23+ }
24+ return typeNode ;
25+ } ;
26+
1327export const generatePropertySignatures = (
1428 entryPoint : string ,
1529 currentPoint : string ,
@@ -42,6 +56,41 @@ export const generatePropertySignatures = (
4256 } ) ;
4357} ;
4458
59+ export const generateTypeAliasDeclarationForObject = (
60+ entryPoint : string ,
61+ currentPoint : string ,
62+ factory : Factory . Type ,
63+ name : string ,
64+ schema : ObjectSchema ,
65+ context : ToTypeNode . Context ,
66+ convertContext : ConvertContext . Types ,
67+ ) : ts . TypeAliasDeclaration => {
68+ if ( schema . type !== "object" ) {
69+ throw new FeatureDevelopmentError ( "Please use generateTypeAlias" ) ;
70+ }
71+ let members : ts . TypeElement [ ] = [ ] ;
72+ const propertySignatures = generatePropertySignatures ( entryPoint , currentPoint , factory , schema , context , convertContext ) ;
73+ if ( Guard . isObjectSchemaWithAdditionalProperties ( schema ) ) {
74+ const additionalProperties = ToTypeNode . convertAdditionalProperties ( entryPoint , currentPoint , factory , schema , context , convertContext ) ;
75+ if ( schema . additionalProperties === true ) {
76+ members = members . concat ( additionalProperties ) ;
77+ } else {
78+ members = [ ...propertySignatures , additionalProperties ] ;
79+ }
80+ } else {
81+ members = propertySignatures ;
82+ }
83+ const typeNode = factory . TypeLiteralNode . create ( {
84+ members,
85+ } ) ;
86+ return factory . TypeAliasDeclaration . create ( {
87+ export : true ,
88+ name : convertContext . escapeDeclarationText ( name ) ,
89+ comment : [ schema . title , schema . description ] . filter ( v => ! ! v ) . join ( "\n\n" ) ,
90+ type : nullable ( factory , typeNode , schema . nullable === true ) ,
91+ } ) ;
92+ } ;
93+
4594export const generateInterface = (
4695 entryPoint : string ,
4796 currentPoint : string ,
@@ -164,11 +213,10 @@ export const generateTypeAlias = (
164213 type : schema . type ,
165214 } ) ;
166215 }
167-
168216 return factory . TypeAliasDeclaration . create ( {
169217 export : true ,
170218 name : convertContext . escapeDeclarationText ( name ) ,
171- type,
219+ type : nullable ( factory , type , schema . nullable === true ) ,
172220 comment : [ schema . title , schema . description ] . filter ( v => ! ! v ) . join ( "\n\n" ) ,
173221 } ) ;
174222} ;
@@ -239,11 +287,19 @@ export const addSchema = (
239287 value : generateArrayTypeAlias ( entryPoint , currentPoint , factory , declarationName , schema , context , convertContext ) ,
240288 } ) ;
241289 } else if ( Guard . isObjectSchema ( schema ) ) {
242- store . addStatement ( targetPoint , {
243- kind : "interface" ,
244- name : convertContext . escapeDeclarationText ( declarationName ) ,
245- value : generateInterface ( entryPoint , currentPoint , factory , declarationName , schema , context , convertContext ) ,
246- } ) ;
290+ if ( schema . nullable ) {
291+ store . addStatement ( targetPoint , {
292+ kind : "typeAlias" ,
293+ name : convertContext . escapeDeclarationText ( declarationName ) ,
294+ value : generateTypeAliasDeclarationForObject ( entryPoint , currentPoint , factory , declarationName , schema , context , convertContext ) ,
295+ } ) ;
296+ } else {
297+ store . addStatement ( targetPoint , {
298+ kind : "interface" ,
299+ name : convertContext . escapeDeclarationText ( declarationName ) ,
300+ value : generateInterface ( entryPoint , currentPoint , factory , declarationName , schema , context , convertContext ) ,
301+ } ) ;
302+ }
247303 } else if ( Guard . isPrimitiveSchema ( schema ) ) {
248304 store . addStatement ( targetPoint , {
249305 kind : "typeAlias" ,
0 commit comments