@@ -79,16 +79,16 @@ exports[`Marshalling preset date unmarshal should convert date-formatted string
7979 instance.createdAt = obj[\\"createdAt\\"] == null ? null : new Date (obj [\\" createdAt\\ " ]);
8080 }
8181 if (obj [\\" birthDate\\ " ] !== undefined ) {
82- instance.birthDate = obj[\\"birthDate\\"] == null ? null : new Date (obj [\\" birthDate\\ " ]);
82+ instance.birthDate = obj[\\"birthDate\\"] == null ? undefined : new Date (obj [\\" birthDate\\ " ]);
8383 }
8484 if (obj [\\" meetingTime\\ " ] !== undefined ) {
85- instance.meetingTime = obj[\\"meetingTime\\"] == null ? null : new Date (obj [\\" meetingTime\\ " ]);
85+ instance.meetingTime = obj[\\"meetingTime\\"] == null ? undefined : new Date (obj [\\" meetingTime\\ " ]);
8686 }
8787 if (obj [\\" regularString\\ " ] !== undefined ) {
8888 instance.regularString = obj[\\"regularString\\"];
8989 }
9090 if (obj [\\" optionalDate\\ " ] !== undefined ) {
91- instance.optionalDate = obj[\\"optionalDate\\"] == null ? null : new Date (obj [\\" optionalDate\\ " ]);
91+ instance.optionalDate = obj[\\"optionalDate\\"] == null ? undefined : new Date (obj [\\" optionalDate\\ " ]);
9292 }
9393
9494 instance .additionalProperties = new Map ();
@@ -101,6 +101,95 @@ exports[`Marshalling preset date unmarshal should convert date-formatted string
101101}"
102102` ;
103103
104+ exports [` Marshalling preset nullable types (type: [null, string]) should generate correct types and unmarshal for nullable properties 1` ] = `
105+ "class NullableTest {
106+ private _nullableString ?: string | null ;
107+ private _nullableDate ?: Date | null ;
108+ private _requiredNullableDate : Date | null ;
109+ private _requiredDate : Date ;
110+ private _additionalProperties ?: Map < string , any > ;
111+
112+ constructor (input : {
113+ nullableString?: string | null ,
114+ nullableDate?: Date | null ,
115+ requiredNullableDate: Date | null ,
116+ requiredDate: Date ,
117+ additionalProperties?: Map<string , any >,
118+ }) {
119+ this._nullableString = input.nullableString;
120+ this._nullableDate = input.nullableDate;
121+ this._requiredNullableDate = input.requiredNullableDate;
122+ this._requiredDate = input.requiredDate;
123+ this._additionalProperties = input.additionalProperties;
124+ }
125+
126+ get nullableString (): string | null | undefined { return this._nullableString; }
127+ set nullableString (nullableString : string | null | undefined ) { this._nullableString = nullableString; }
128+
129+ get nullableDate (): Date | null | undefined { return this._nullableDate; }
130+ set nullableDate (nullableDate : Date | null | undefined ) { this._nullableDate = nullableDate; }
131+
132+ get requiredNullableDate (): Date | null { return this._requiredNullableDate; }
133+ set requiredNullableDate (requiredNullableDate : Date | null ) { this._requiredNullableDate = requiredNullableDate; }
134+
135+ get requiredDate (): Date { return this._requiredDate; }
136+ set requiredDate (requiredDate : Date ) { this._requiredDate = requiredDate; }
137+
138+ get additionalProperties (): Map < string , any > | undefined { return this._additionalProperties; }
139+ set additionalProperties (additionalProperties : Map < string , any > | undefined ) { this._additionalProperties = additionalProperties; }
140+
141+ public marshal () : string {
142+ let json = '{'
143+ if(this .nullableString !== undefined ) {
144+ json += \` \\ "nullableString\\ ": \$ { typeof this .nullableString === ' number' || typeof this .nullableString === ' boolean' ? this .nullableString : JSON .stringify (this .nullableString )} ,\` ;
145+ }
146+ if(this.nullableDate !== undefined) {
147+ json += \` \\ "nullableDate\\ ": \$ { typeof this .nullableDate === ' number' || typeof this .nullableDate === ' boolean' ? this .nullableDate : JSON .stringify (this .nullableDate )} ,\` ;
148+ }
149+ if(this.requiredNullableDate !== undefined) {
150+ json += \` \\ "requiredNullableDate\\ ": \$ { typeof this .requiredNullableDate === ' number' || typeof this .requiredNullableDate === ' boolean' ? this .requiredNullableDate : JSON .stringify (this .requiredNullableDate )} ,\` ;
151+ }
152+ if(this.requiredDate !== undefined) {
153+ json += \` \\ "requiredDate\\ ": \$ { typeof this .requiredDate === ' number' || typeof this .requiredDate === ' boolean' ? this .requiredDate : JSON .stringify (this .requiredDate )} ,\` ;
154+ }
155+ if(this.additionalProperties !== undefined) {
156+ for (const [key , value ] of this .additionalProperties .entries ()) {
157+ // Only unwrap those that are not already a property in the JSON object
158+ if([\\"nullableString \\",\\"nullableDate \\",\\"requiredNullableDate \\",\\"requiredDate \\",\\"additionalProperties \\"].includes (String (key ))) continue;
159+ json += \`\\" \$ {key}\\ " : \$ {typeof value === 'number' || typeof value === 'boolean' ? value : JSON .stringify (value )},\`;
160+ }
161+ }
162+ //Remove potential last comma
163+ return \`\$ { json .charAt (json .length - 1 ) === ' ,' ? json .slice (0 , json .length - 1 ) : json } }\` ;
164+ }
165+
166+ public static unmarshal(json: string | object): NullableTest {
167+ const obj = typeof json === \\" object\\ " ? json : JSON .parse (json );
168+ const instance = new NullableTest ({} as any );
169+
170+ if (obj [\\" nullableString\\ " ] !== undefined ) {
171+ instance.nullableString = obj[\\"nullableString\\"];
172+ }
173+ if (obj [\\" nullableDate\\ " ] !== undefined ) {
174+ instance.nullableDate = obj[\\"nullableDate\\"] == null ? undefined : new Date (obj [\\" nullableDate\\ " ]);
175+ }
176+ if (obj [\\" requiredNullableDate\\ " ] !== undefined ) {
177+ instance.requiredNullableDate = obj[\\"requiredNullableDate\\"] == null ? null : new Date (obj [\\" requiredNullableDate\\ " ]);
178+ }
179+ if (obj [\\" requiredDate\\ " ] !== undefined ) {
180+ instance.requiredDate = obj[\\"requiredDate\\"] == null ? null : new Date (obj [\\" requiredDate\\ " ]);
181+ }
182+
183+ instance .additionalProperties = new Map ();
184+ const propsToCheck = Object .entries (obj ).filter ((([key ,]) => {return ! [\\" nullableString\\ " ,\\" nullableDate\\ " ,\\" requiredNullableDate\\ " ,\\" requiredDate\\ " ,\\" additionalProperties\\ " ].includes (key );}));
185+ for (const [key , value ] of propsToCheck ) {
186+ instance.additionalProperties.set(key , value as any );
187+ }
188+ return instance ;
189+ }
190+ }"
191+ ` ;
192+
104193exports [` Marshalling preset should render un/marshal code 1` ] = `
105194"class Test {
106195 private _stringProp : string ;
@@ -273,7 +362,7 @@ exports[`Marshalling preset should render un/marshal code 1`] = `
273362 }
274363 if (obj [\\" arrayTest\\ " ] !== undefined ) {
275364 instance.arrayTest = obj[\\"arrayTest\\"] == null
276- ? null
365+ ? undefined
277366 : obj[\\"arrayTest\\"].map((item : any ) => NestedTest.unmarshal(item ));
278367 }
279368 if (obj [\\" primitiveArrayTest\\ " ] !== undefined ) {
0 commit comments