Skip to content

Commit

Permalink
protoc-gen-openapiv2: Do not add invisible enum values as default (#5129
Browse files Browse the repository at this point in the history
)

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

* Return default integer value

* Added a new enum
  • Loading branch information
navruzm authored Jan 21, 2025
1 parent 3b7b760 commit e1364b5
Show file tree
Hide file tree
Showing 8 changed files with 418 additions and 138 deletions.
351 changes: 213 additions & 138 deletions examples/internal/proto/examplepb/visibility_rule_echo_service.pb.go

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ message VisibilityRuleSimpleMessage {
VISIBILITY_ENUM_PREVIEW = 3 [(google.api.value_visibility).restriction = "INTERNAL,PREVIEW"];
}

enum VisibilityEnumHiddenDefault {
VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED = 0 [(google.api.value_visibility).restriction = "INTERNAL"];
VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE = 1;
}

// Id represents the message identifier.
string id = 1;
int64 num = 2;
Expand All @@ -51,6 +56,7 @@ message VisibilityRuleSimpleMessage {
string internal_field = 8 [(google.api.field_visibility).restriction = "INTERNAL"];
string preview_field = 9 [(google.api.field_visibility).restriction = "INTERNAL,PREVIEW"];
VisibilityEnum an_enum = 10;
VisibilityEnumHiddenDefault ahidden_default_enum = 11;
}

// MessageInPreviewMethod doesn't define its own visibility restrictions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,16 @@
1
],
"default": 0
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "integer",
"enum": [
1
],
"default": 0
}
],
"tags": [
Expand All @@ -124,6 +134,14 @@
],
"default": 0
},
"VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault": {
"type": "integer",
"format": "int32",
"enum": [
1
],
"default": 0
},
"examplepbVisibilityRuleEmbedded": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -167,6 +185,9 @@
},
"anEnum": {
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnum"
},
"ahiddenDefaultEnum": {
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault"
}
},
"description": "SimpleMessage represents a simple message sent to the Echo service."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
}
],
"tags": [
Expand Down Expand Up @@ -281,6 +292,17 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
}
],
"tags": [
Expand Down Expand Up @@ -415,6 +437,17 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
}
],
"tags": [
Expand Down Expand Up @@ -549,6 +582,17 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
}
],
"tags": [
Expand All @@ -568,6 +612,14 @@
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
"VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault": {
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
},
"examplepbVisibilityRuleEmbedded": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -623,6 +675,9 @@
},
"anEnum": {
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnum"
},
"ahiddenDefaultEnum": {
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault"
}
},
"description": "SimpleMessage represents a simple message sent to the Echo service."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,15 @@
"VISIBILITY_ENUM_VISIBLE"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
]
}
],
"tags": [
Expand All @@ -123,6 +132,12 @@
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
"VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault": {
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
]
},
"examplepbVisibilityRuleEmbedded": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -166,6 +181,9 @@
},
"anEnum": {
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnum"
},
"ahiddenDefaultEnum": {
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault"
}
},
"description": "SimpleMessage represents a simple message sent to the Echo service."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
}
],
"tags": [
Expand Down Expand Up @@ -281,6 +292,17 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
}
],
"tags": [
Expand Down Expand Up @@ -415,6 +437,17 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
}
],
"tags": [
Expand Down Expand Up @@ -549,6 +582,17 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
}
],
"tags": [
Expand Down Expand Up @@ -683,6 +727,17 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
}
],
"tags": [
Expand All @@ -702,6 +757,14 @@
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
"VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault": {
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
],
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
},
"examplepbVisibilityRuleEmbedded": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -780,6 +843,9 @@
},
"anEnum": {
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnum"
},
"ahiddenDefaultEnum": {
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault"
}
},
"description": "SimpleMessage represents a simple message sent to the Echo service."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,15 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
]
}
],
"tags": [
Expand Down Expand Up @@ -240,6 +249,15 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
]
}
],
"tags": [
Expand Down Expand Up @@ -355,6 +373,15 @@
"VISIBILITY_ENUM_PREVIEW"
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
{
"name": "ahiddenDefaultEnum",
"in": "query",
"required": false,
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
]
}
],
"tags": [
Expand All @@ -373,6 +400,12 @@
],
"default": "VISIBILITY_ENUM_UNSPECIFIED"
},
"VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault": {
"type": "string",
"enum": [
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
]
},
"examplepbVisibilityRuleEmbedded": {
"type": "object",
"properties": {
Expand Down Expand Up @@ -445,6 +478,9 @@
},
"anEnum": {
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnum"
},
"ahiddenDefaultEnum": {
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault"
}
},
"description": "SimpleMessage represents a simple message sent to the Echo service."
Expand Down
3 changes: 3 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,9 @@ func getEnumDefault(reg *descriptor.Registry, enum *descriptor.Enum) interface{}
if !reg.GetOmitEnumDefaultValue() {
for _, value := range enum.GetValue() {
if value.GetNumber() == 0 {
if !isVisible(getEnumValueVisibilityOption(value), reg) {
return nil
}
return value.GetName()
}
}
Expand Down

0 comments on commit e1364b5

Please sign in to comment.