Return default values for unset optional fields instead of throwing#2
Merged
Return default values for unset optional fields instead of throwing#2
Conversation
Previously, accessing an unset optional field (proto2) would throw IllegalStateException. This breaks compatibility with standard Protobuf, which returns type-appropriate default values (0, false, "", empty bytes, first enum value, empty message instance). Now only required fields throw IllegalStateException when not set. Optional fields return defaults matching Protobuf behavior: - Numbers: 0 / 0L / 0.0 / 0.0f - Booleans: false - Strings: "" - Bytes: empty byte[] / empty ByteBuf - Enums: valueOf(0) - Messages: lazily-created empty instance Also fixed clear() to properly reset all field types to defaults.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
IllegalStateExceptionwhen accessed unsetclear()to properly reset all field types (numbers, booleans, enums, strings, bytes, messages) back to defaultsMotivation
Code that works with standard Protobuf relies on optional field getters returning default values (0, false,
"", empty bytes, first enum value, default message instance). LightProto was throwingIllegalStateExceptioninstead, breaking compatibility.Changes
Code generator (6 files):
LightProtoNumberField/LightProtoBooleanField/LightProtoEnumField: Changed throw condition from!hasImplicitPresence && !isDefaultValueSettoisRequired(); fixedclear()to always resetLightProtoStringField: Required fields throw; implicit presence usesbufferLencheck; optional without default returns""LightProtoBytesField: Required fields throw; all others return empty bytes/bufferLightProtoMessageField: Required fields throw; optional fields lazily create an empty instanceTests (5 files, 10 new tests):
clear()resets: all number types, strings, bytes, sub-messages, proto3 fieldsTest plan
clear()resets values (not just presence bits) for all types