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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

protoc-gen-grpc-gateway: should respect (google.api.field_visibility).restriction annotation #3334

Open
varnastadeus opened this issue May 31, 2023 · 4 comments

Comments

@varnastadeus
Copy link

varnastadeus commented May 31, 2023

馃殌 Feature

I am using [(google.api.field_visibility).restriction = "INTERNAL"] and it works perfectly by excluding internal items from OpenAPI spec. I would expect internal fields to be excluded from grpc-gateway responses as well.

Having:

service VisibilityRuleEchoService {
  rpc Echo(VisibilityRuleSimpleMessage) returns (VisibilityRuleSimpleMessage) {
    option (google.api.http) = {post: "/v1/example/echo/{id}"};
  }

message VisibilityRuleSimpleMessage {
  string id = 1;
  int64 num = 2;
  string internal_field = 3 [(google.api.field_visibility).restriction = "INTERNAL"];
  string preview_field = 4 [(google.api.field_visibility).restriction = "INTERNAL,PREVIEW"];
}

curl -L -X POST 'localhost:8080/v1/example/echo/5?num=6&previewField=ok&internalField=not_ok'

I would expect following response:

{
    "id": "5",
    "num": "6",
    "previewField": "ok",
}

Current response:

{
    "id": "5",
    "num": "6",
    "internalField": "not_ok",
    "previewField": "ok",
}
@johanbrandhorst
Copy link
Collaborator

In theory you could accomplish this today with a custom marshaler that performs introspection on the fields as it marshals. I don't think we need to add this feature to the gateway directly unless there is a great demand for it. I encourage you to explore a custom marshaler implementation though, that shouldn't be too hard and completely possible today.

@mjudeikis
Copy link

Custom marshaler would do this on runtime, and might be added performance penalty as it would travers most types and do some field checking.

easiest way to achieve something like this would be to add json:"-" into generated code to just not marshal those fields using default marshaler. But this is not something you can do now.

@varnastadeus
Copy link
Author

But shouldn't this be the default behaviour? Having it only hidden in OpenAPI spec adds even more confusion since user sees one state of the API while looking at the spec and then gets completely different response while calling it

@johanbrandhorst
Copy link
Collaborator

The grpc-gateway depends on the behavior of the official Go protobuf JSON marshaler in this case. It's not something we control. If you want to make the case that the official Go protobuf JSON marshaler should ignore these fields, you'll have to raise an issue at https://github.com/golang/protobuf/issues.

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

No branches or pull requests

3 participants