File tree Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Expand file tree Collapse file tree 2 files changed +21
-2
lines changed Original file line number Diff line number Diff line change @@ -231,4 +231,16 @@ describe("EchoMsg", () => {
231
231
const msg2 = EchoMsg . fromJsonString ( jsonString ) ;
232
232
expect ( msg ) . toEqual ( msg2 ) ;
233
233
} ) ;
234
+
235
+ it ( "toBinary with null returns empty Uint8Array" , ( ) => {
236
+ const binary = EchoMsg . toBinary ( null as any ) ;
237
+ expect ( binary ) . toBeInstanceOf ( Uint8Array ) ;
238
+ expect ( binary . length ) . toBe ( 0 ) ;
239
+ } ) ;
240
+
241
+ it ( "toBinary with undefined returns empty Uint8Array" , ( ) => {
242
+ const binary = EchoMsg . toBinary ( undefined as any ) ;
243
+ expect ( binary ) . toBeInstanceOf ( Uint8Array ) ;
244
+ expect ( binary . length ) . toBe ( 0 ) ;
245
+ } ) ;
234
246
} ) ;
Original file line number Diff line number Diff line change @@ -151,7 +151,10 @@ export interface MessageType<T extends Message<T> = AnyMessage> {
151
151
/**
152
152
* Serialize the message to binary data.
153
153
*/
154
- toBinary ( a : Message < T > , options ?: Partial < BinaryWriteOptions > ) : Uint8Array ;
154
+ toBinary (
155
+ a : Message < T > | undefined | null ,
156
+ options ?: Partial < BinaryWriteOptions > ,
157
+ ) : Uint8Array ;
155
158
156
159
/**
157
160
* Serialize the message to a JSON value, a JavaScript value that can be
@@ -286,7 +289,11 @@ export function createMessageType<
286
289
return mt . fromJson ( json , options ) ;
287
290
} ,
288
291
289
- toBinary ( a : T , options ?: Partial < BinaryWriteOptions > ) : Uint8Array {
292
+ toBinary (
293
+ a : T | undefined | null ,
294
+ options ?: Partial < BinaryWriteOptions > ,
295
+ ) : Uint8Array {
296
+ if ( a == null ) return new Uint8Array ( 0 ) ;
290
297
const opt = binaryMakeWriteOptions ( options ) ;
291
298
const writer = opt . writerFactory ( ) ;
292
299
binaryWriteMessage ( a , fields , writer , opt ) ;
You can’t perform that action at this time.
0 commit comments