Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

required field #948

Open
oo-work opened this issue Nov 29, 2024 · 1 comment
Open

required field #948

oo-work opened this issue Nov 29, 2024 · 1 comment

Comments

@oo-work
Copy link

oo-work commented Nov 29, 2024

message SayRequest{
  required string str1 = 1;
  optional int32 int1 = 2;
}

When I use proto2's required and optional, the code generated by protoc --dart_out=grpc:lib/src/generated -Iprotos .\protos\* is as follows

class SayRequest extends $pb.GeneratedMessage {
  factory SayRequest({
    $core.String? str1,
    $core.int? int1,
  }) {
    final $result = create();
    if (str1 != null) {
      $result.str1 = str1;
    }
    if (int1 != null) {
      $result.int1 = int1;
    }
    return $result;
  }
  SayRequest._() : super();
}

All fields are optional, it does not follow the required as expected

factory SayRequest({
    required String str1,
    int? int1,
  }) 
@oo-work oo-work changed the title required filed required field Nov 30, 2024
@esrauchg
Copy link

esrauchg commented Jan 10, 2025

Required in protobuf semantics generally doesn't quite behave that way; it is normal and legal to create an in-memory instance without the required fields set, the notion of required is really only only 'this must be set across the wire'.

Also worth calling out the 'required' is considered a regretted feature of Protobuf and is strongly discouraged for new usages (https://protobuf.dev/best-practices/dos-donts/#add-required), because it is extremely difficult to safely remove the field if you realize you don't need it anymore, and so will unduly constrains you from evolving your schema going forward (and the ability to have predictable behavior when safely evolving your schema over time is the main feature of protobuf).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants