Skip to content

Generated MessageView loses proto2 required-field presence #170

@nyurik

Description

@nyurik

When generating zero-copy view types for proto2 schemas, required scalar/string fields are emitted as bare defaulted values in the generated *View<'a> type. This makes it impossible for users to distinguish a field that was absent on the wire from a field that was explicitly present with its default value.

This matters for proto2 schemas where missing required fields should be rejected, or where downstream code needs to validate the input according to the schema.

syntax = "proto2";

package example;

message Tile {
  message Layer {
    required uint32 version = 15 [default = 1];
    required string name = 1;
    optional uint32 extent = 5 [default = 4096];
  }
  repeated Layer layers = 3;
}

The generated view shape is effectively:

pub struct LayerView<'a> {
    pub version: u32,
    pub name: &'a str,
    pub extent: Option<u32>,
    // ...
}

After LayerView::decode_view(...), these cases are indistinguishable:

  • version missing vs version explicitly encoded as 1
  • name missing vs name explicitly encoded as ""

Would it be possible to either:

  • making decode_view reject messages missing proto2 required fields (possibly with a strict codegen flag)
  • exposing required-field presence in generated view types, e.g. via generated has_* accessors or internal presence bits?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions