InvalidProtocolBufferException data schema_error ai_generated true

Protobuf deserialization fails with 'Unknown enum value' when reading data with new enum values

ID: data/protobuf-unknown-enum-value

Also available as: JSON · Markdown · 中文
85%Fix Rate
86%Confidence
1Evidence
2023-12-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
Protocol Buffers 3.21+ active
protoc 24.0+ active
gRPC 1.60+ active

Root Cause

A new enum value was added to a Protobuf definition, but the reader's generated code is not updated, causing it to reject the unknown value instead of treating it as an unrecognized field.

generic

中文

Protobuf定义中添加了新的枚举值,但读取器的生成代码未更新,导致其拒绝未知值而非将其视为未识别字段。

Official Documentation

https://protobuf.dev/programming-guides/proto3/#enum

Workarounds

  1. 100% success Update the reader to use the latest .proto file and regenerate the code. For Java: run `protoc --java_out=src/main/java myproto.proto` with the updated definition.
    Update the reader to use the latest .proto file and regenerate the code. For Java: run `protoc --java_out=src/main/java myproto.proto` with the updated definition.
  2. 70% success Use the `allow_alias` option in the .proto file to map the new enum value to an existing one as a temporary measure: `option allow_alias = true; NEW_VALUE = EXISTING_VALUE;`
    Use the `allow_alias` option in the .proto file to map the new enum value to an existing one as a temporary measure: `option allow_alias = true; NEW_VALUE = EXISTING_VALUE;`
  3. 75% success Treat the field as `google.protobuf.Int32Value` wrapper to bypass enum validation, then convert the integer to the appropriate enum in application code.
    Treat the field as `google.protobuf.Int32Value` wrapper to bypass enum validation, then convert the integer to the appropriate enum in application code.

中文步骤

  1. 更新读取器使用最新的.proto文件并重新生成代码。对于Java:使用更新后的定义运行`protoc --java_out=src/main/java myproto.proto`。
  2. 在.proto文件中使用`allow_alias`选项将新枚举值临时映射到现有值:`option allow_alias = true; NEW_VALUE = EXISTING_VALUE;`
  3. 将字段视为`google.protobuf.Int32Value`包装器以绕过枚举验证,然后在应用程序代码中将整数转换为适当的枚举。

Dead Ends

Common approaches that don't work:

  1. 100% fail

    The reader still runs old code; regenerating without redeployment has no effect.

  2. 80% fail

    Enums in proto3 are already implicitly optional; making them explicit does not change how unknown values are handled.

  3. 65% fail

    Protobuf's generated code throws an exception during deserialization that cannot be easily caught without modifying the generated code itself.