Skip to content

Commit 94556f9

Browse files
Do not use null-defaults to decide type signature for pb3-optionals (this only sets the default of optional fields, it doesn't control whether a field is optional or not)
1 parent 358d336 commit 94556f9

File tree

2 files changed

+13
-4
lines changed

2 files changed

+13
-4
lines changed

cli/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ Translates between file formats and generates static code.
6868
--force-long Enforces the use of 'Long' for s-/u-/int64 and s-/fixed64 fields.
6969
--force-number Enforces the use of 'number' for s-/u-/int64 and s-/fixed64 fields.
7070
--force-message Enforces the use of message instances instead of plain objects.
71-
--pb3-optional Make type declarations respect optional fields for PB3.
71+
--null-defaults Default value for optional fields is null instead of zero value.
72+
--pb3-optional Make type declarations respect optional fields for PB3.
7273
7374
usage: pbjs [options] file1.proto file2.json ... (or pipe) other | pbjs [options] -
7475
```

cli/targets/static.js

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ function toJsType(field) {
356356

357357
function handleOptionalFields(jsType, field) {
358358

359-
if (field.optional && !field.map && !field.repeated && (field.resolvedType instanceof Type || config["null-defaults"]) || field.partOf)
359+
if (field.optional && !field.map && !field.repeated && field.resolvedType instanceof Type || field.partOf)
360360
return jsType + "|null|undefined";
361361
else
362362
return jsType
@@ -410,8 +410,16 @@ function buildType(ref, type) {
410410
if (config.comments) {
411411
push("");
412412
var jsType = toJsType(field);
413-
// No need to check config.tsOptional for member types, this logic has not changed
414-
jsType = handleOptionalFields(jsType, field);
413+
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);
417+
}
418+
else {
419+
if (field.optional && !field.map && !field.repeated && (field.resolvedType instanceof Type || config["null-defaults"]) || field.partOf)
420+
jsType = jsType + "|null|undefined";
421+
}
422+
415423
pushComment([
416424
field.comment || type.name + " " + field.name + ".",
417425
"@member {" + jsType + "} " + field.name,

0 commit comments

Comments
 (0)