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

protoc-gen-openapiv2: Do not add invisible enum values as default #5129

Merged
merged 3 commits into from
Jan 21, 2025

Conversation

navruzm
Copy link
Contributor

@navruzm navruzm commented Jan 10, 2025

Have you read the Contributing Guidelines?

Yes

Brief description of what is fixed or changed

Right now, if we hide the default enum value with value_visibility, it does not appear in the enum list but still shows up in the default field. This PR adds a check that skips the default enum value if it is hidden. That way, we will not see a hidden enum as the default. With this PR, if unknown_type is hidden, it will no longer appear as the default.

Example proto:

enum Type {
    unknown_type = 0 [(google.api.value_visibility).restriction = "INTERNAL"];
    created_at = 1;
    updated_at = 2;
}

Example output (before fix):

{
  "name": "type",
  "description": "",
  "in": "query",
  "required": false,
  "type": "string",
  "enum": [
    "created_at",
    "updated_at"
  ],
  "default": "unknown_type"
}

Other comments

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little conflicted about this. On the one hand, it makes sense that if you want to hide the value it shouldn't show up as a default. On the other hand, all protobuf enum values have defaults that will be set if no value is explicitly supplied. What do you think about returning the integer value while hiding the name?

Also could you try adding add an enum with this property to the https://github.com/grpc-ecosystem/grpc-gateway/blob/main/examples/internal/proto/examplepb/visibility_rule_echo_service.proto test file? Thanks!

@navruzm
Copy link
Contributor Author

navruzm commented Jan 11, 2025

What do you think about returning the integer value while hiding the name?

Done.

Also could you try adding add an enum with this property to the https://github.com/grpc-ecosystem/grpc-gateway/blob/main/examples/internal/proto/examplepb/visibility_rule_echo_service.proto test file?

Sorry, I didn't get this. Can you explain this a bit more? There is already an enum in the file, VisibilityEnum, and no Swagger file is generated for the file.

Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I didn't get this. Can you explain this a bit more? There is already an enum in the file, VisibilityEnum, and no Swagger file is generated for the file.

Sorry, I was unclear. What I meant was, lets add a new enum to the proto file https://github.com/grpc-ecosystem/grpc-gateway/blob/main/examples/internal/proto/examplepb/visibility_rule_echo_service.proto. Lets have the default value be hidden using the visibility specification. This should update the *.swagger.json files generated by various visibility test configs we have living alongside this file (see https://github.com/grpc-ecosystem/grpc-gateway/tree/main/examples/internal/proto/examplepb). It would be great to see that the enum is there but the default value is omitted, in the case where the enum is used as a string. Does that make sense?

@navruzm
Copy link
Contributor Author

navruzm commented Jan 15, 2025

Done!

@johanbrandhorst johanbrandhorst enabled auto-merge (squash) January 21, 2025 04:17
@johanbrandhorst johanbrandhorst merged commit e1364b5 into grpc-ecosystem:main Jan 21, 2025
14 checks passed
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

Successfully merging this pull request may close these issues.

2 participants