@@ -354,14 +354,6 @@ function toJsType(field) {
354
354
return type ;
355
355
}
356
356
357
- function handleOptionalFields ( jsType , field ) {
358
-
359
- if ( field . optional && ! field . map && ! field . repeated && field . resolvedType instanceof Type || field . partOf )
360
- return jsType + "|null|undefined" ;
361
- else
362
- return jsType
363
- }
364
-
365
357
function buildType ( ref , type ) {
366
358
367
359
if ( config . comments ) {
@@ -375,10 +367,13 @@ function buildType(ref, type) {
375
367
prop = prop . substring ( 1 , prop . charAt ( 0 ) === "[" ? prop . length - 1 : prop . length ) ;
376
368
var jsType = toJsType ( field ) ;
377
369
378
- // Hide the fix for PB3 optional fields behind a config flag, it is an API change in generated output
379
- if ( config . pb3Optional ) {
380
- jsType = handleOptionalFields ( jsType , field ) ;
370
+ // New behaviour - fields explicitly marked as optional and members of a one-of are nullable
371
+ // Maps and repeated fields are not explicitly optional, they default to empty instances
372
+ if ( config [ "force-optional" ] ) {
373
+ if ( field . explicitOptional || field . partOf )
374
+ jsType = jsType + "|null|undefined" ;
381
375
}
376
+ // Old behaviour - field.optional is true for all fields in proto3
382
377
else {
383
378
if ( field . optional )
384
379
jsType = jsType + "|null" ;
@@ -411,10 +406,13 @@ function buildType(ref, type) {
411
406
push ( "" ) ;
412
407
var jsType = toJsType ( field ) ;
413
408
414
- // Hide the fix for PB3 optional fields behind a config flag, it is an API change in generated output
415
- if ( config . pb3Optional ) {
416
- jsType = handleOptionalFields ( jsType , field ) ;
409
+ // New behaviour - fields explicitly marked as optional and members of a one-of are nullable
410
+ // Maps and repeated fields are not explicitly optional, they default to empty instances
411
+ if ( config [ "force-optional" ] ) {
412
+ if ( field . explicitOptional || field . partOf )
413
+ jsType = jsType + "|null|undefined" ;
417
414
}
415
+ // Old behaviour - field.optional is true for all fields in proto3
418
416
else {
419
417
if ( field . optional && ! field . map && ! field . repeated && ( field . resolvedType instanceof Type || config [ "null-defaults" ] ) || field . partOf )
420
418
jsType = jsType + "|null|undefined" ;
@@ -430,11 +428,16 @@ function buildType(ref, type) {
430
428
push ( "" ) ;
431
429
firstField = false ;
432
430
}
431
+ // New behaviour sets a null default when the optional keyword is used explicitly
432
+ // Old behaviour considers all proto3 fields optional and uses the null-defaults config flag
433
+ var nullDefault = config [ "force-optional" ]
434
+ ? field . explicitOptional
435
+ : field . optional && config [ "null-defaults" ] ;
433
436
if ( field . repeated )
434
437
push ( escapeName ( type . name ) + ".prototype" + prop + " = $util.emptyArray;" ) ; // overwritten in constructor
435
438
else if ( field . map )
436
439
push ( escapeName ( type . name ) + ".prototype" + prop + " = $util.emptyObject;" ) ; // overwritten in constructor
437
- else if ( field . partOf || field . optional && config [ "null-defaults" ] )
440
+ else if ( field . partOf || nullDefault )
438
441
push ( escapeName ( type . name ) + ".prototype" + prop + " = null;" ) ; // do not set default value for oneof members
439
442
else if ( field . long )
440
443
push ( escapeName ( type . name ) + ".prototype" + prop + " = $util.Long ? $util.Long.fromBits("
0 commit comments