Skip to content

Commit e1364b5

Browse files
authored
protoc-gen-openapiv2: Do not add invisible enum values as default (#5129)
* protoc-gen-openapiv2: Do not add invisible enum values as default * Return default integer value * Added a new enum
1 parent 3b7b760 commit e1364b5

8 files changed

+418
-138
lines changed

examples/internal/proto/examplepb/visibility_rule_echo_service.pb.go

+213-138
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/internal/proto/examplepb/visibility_rule_echo_service.proto

+6
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ message VisibilityRuleSimpleMessage {
3636
VISIBILITY_ENUM_PREVIEW = 3 [(google.api.value_visibility).restriction = "INTERNAL,PREVIEW"];
3737
}
3838

39+
enum VisibilityEnumHiddenDefault {
40+
VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED = 0 [(google.api.value_visibility).restriction = "INTERNAL"];
41+
VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE = 1;
42+
}
43+
3944
// Id represents the message identifier.
4045
string id = 1;
4146
int64 num = 2;
@@ -51,6 +56,7 @@ message VisibilityRuleSimpleMessage {
5156
string internal_field = 8 [(google.api.field_visibility).restriction = "INTERNAL"];
5257
string preview_field = 9 [(google.api.field_visibility).restriction = "INTERNAL,PREVIEW"];
5358
VisibilityEnum an_enum = 10;
59+
VisibilityEnumHiddenDefault ahidden_default_enum = 11;
5460
}
5561

5662
// MessageInPreviewMethod doesn't define its own visibility restrictions,

examples/internal/proto/examplepb/visibility_rule_enums_as_ints_echo_service.swagger.json

+21
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,16 @@
106106
1
107107
],
108108
"default": 0
109+
},
110+
{
111+
"name": "ahiddenDefaultEnum",
112+
"in": "query",
113+
"required": false,
114+
"type": "integer",
115+
"enum": [
116+
1
117+
],
118+
"default": 0
109119
}
110120
],
111121
"tags": [
@@ -124,6 +134,14 @@
124134
],
125135
"default": 0
126136
},
137+
"VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault": {
138+
"type": "integer",
139+
"format": "int32",
140+
"enum": [
141+
1
142+
],
143+
"default": 0
144+
},
127145
"examplepbVisibilityRuleEmbedded": {
128146
"type": "object",
129147
"properties": {
@@ -167,6 +185,9 @@
167185
},
168186
"anEnum": {
169187
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnum"
188+
},
189+
"ahiddenDefaultEnum": {
190+
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault"
170191
}
171192
},
172193
"description": "SimpleMessage represents a simple message sent to the Echo service."

examples/internal/proto/examplepb/visibility_rule_internal_echo_service.swagger.json

+55
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@
147147
"VISIBILITY_ENUM_PREVIEW"
148148
],
149149
"default": "VISIBILITY_ENUM_UNSPECIFIED"
150+
},
151+
{
152+
"name": "ahiddenDefaultEnum",
153+
"in": "query",
154+
"required": false,
155+
"type": "string",
156+
"enum": [
157+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
158+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
159+
],
160+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
150161
}
151162
],
152163
"tags": [
@@ -281,6 +292,17 @@
281292
"VISIBILITY_ENUM_PREVIEW"
282293
],
283294
"default": "VISIBILITY_ENUM_UNSPECIFIED"
295+
},
296+
{
297+
"name": "ahiddenDefaultEnum",
298+
"in": "query",
299+
"required": false,
300+
"type": "string",
301+
"enum": [
302+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
303+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
304+
],
305+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
284306
}
285307
],
286308
"tags": [
@@ -415,6 +437,17 @@
415437
"VISIBILITY_ENUM_PREVIEW"
416438
],
417439
"default": "VISIBILITY_ENUM_UNSPECIFIED"
440+
},
441+
{
442+
"name": "ahiddenDefaultEnum",
443+
"in": "query",
444+
"required": false,
445+
"type": "string",
446+
"enum": [
447+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
448+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
449+
],
450+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
418451
}
419452
],
420453
"tags": [
@@ -549,6 +582,17 @@
549582
"VISIBILITY_ENUM_PREVIEW"
550583
],
551584
"default": "VISIBILITY_ENUM_UNSPECIFIED"
585+
},
586+
{
587+
"name": "ahiddenDefaultEnum",
588+
"in": "query",
589+
"required": false,
590+
"type": "string",
591+
"enum": [
592+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
593+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
594+
],
595+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
552596
}
553597
],
554598
"tags": [
@@ -568,6 +612,14 @@
568612
],
569613
"default": "VISIBILITY_ENUM_UNSPECIFIED"
570614
},
615+
"VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault": {
616+
"type": "string",
617+
"enum": [
618+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
619+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
620+
],
621+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
622+
},
571623
"examplepbVisibilityRuleEmbedded": {
572624
"type": "object",
573625
"properties": {
@@ -623,6 +675,9 @@
623675
},
624676
"anEnum": {
625677
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnum"
678+
},
679+
"ahiddenDefaultEnum": {
680+
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault"
626681
}
627682
},
628683
"description": "SimpleMessage represents a simple message sent to the Echo service."

examples/internal/proto/examplepb/visibility_rule_none_echo_service.swagger.json

+18
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,15 @@
106106
"VISIBILITY_ENUM_VISIBLE"
107107
],
108108
"default": "VISIBILITY_ENUM_UNSPECIFIED"
109+
},
110+
{
111+
"name": "ahiddenDefaultEnum",
112+
"in": "query",
113+
"required": false,
114+
"type": "string",
115+
"enum": [
116+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
117+
]
109118
}
110119
],
111120
"tags": [
@@ -123,6 +132,12 @@
123132
],
124133
"default": "VISIBILITY_ENUM_UNSPECIFIED"
125134
},
135+
"VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault": {
136+
"type": "string",
137+
"enum": [
138+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
139+
]
140+
},
126141
"examplepbVisibilityRuleEmbedded": {
127142
"type": "object",
128143
"properties": {
@@ -166,6 +181,9 @@
166181
},
167182
"anEnum": {
168183
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnum"
184+
},
185+
"ahiddenDefaultEnum": {
186+
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault"
169187
}
170188
},
171189
"description": "SimpleMessage represents a simple message sent to the Echo service."

examples/internal/proto/examplepb/visibility_rule_preview_and_internal_echo_service.swagger.json

+66
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,17 @@
147147
"VISIBILITY_ENUM_PREVIEW"
148148
],
149149
"default": "VISIBILITY_ENUM_UNSPECIFIED"
150+
},
151+
{
152+
"name": "ahiddenDefaultEnum",
153+
"in": "query",
154+
"required": false,
155+
"type": "string",
156+
"enum": [
157+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
158+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
159+
],
160+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
150161
}
151162
],
152163
"tags": [
@@ -281,6 +292,17 @@
281292
"VISIBILITY_ENUM_PREVIEW"
282293
],
283294
"default": "VISIBILITY_ENUM_UNSPECIFIED"
295+
},
296+
{
297+
"name": "ahiddenDefaultEnum",
298+
"in": "query",
299+
"required": false,
300+
"type": "string",
301+
"enum": [
302+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
303+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
304+
],
305+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
284306
}
285307
],
286308
"tags": [
@@ -415,6 +437,17 @@
415437
"VISIBILITY_ENUM_PREVIEW"
416438
],
417439
"default": "VISIBILITY_ENUM_UNSPECIFIED"
440+
},
441+
{
442+
"name": "ahiddenDefaultEnum",
443+
"in": "query",
444+
"required": false,
445+
"type": "string",
446+
"enum": [
447+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
448+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
449+
],
450+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
418451
}
419452
],
420453
"tags": [
@@ -549,6 +582,17 @@
549582
"VISIBILITY_ENUM_PREVIEW"
550583
],
551584
"default": "VISIBILITY_ENUM_UNSPECIFIED"
585+
},
586+
{
587+
"name": "ahiddenDefaultEnum",
588+
"in": "query",
589+
"required": false,
590+
"type": "string",
591+
"enum": [
592+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
593+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
594+
],
595+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
552596
}
553597
],
554598
"tags": [
@@ -683,6 +727,17 @@
683727
"VISIBILITY_ENUM_PREVIEW"
684728
],
685729
"default": "VISIBILITY_ENUM_UNSPECIFIED"
730+
},
731+
{
732+
"name": "ahiddenDefaultEnum",
733+
"in": "query",
734+
"required": false,
735+
"type": "string",
736+
"enum": [
737+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
738+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
739+
],
740+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
686741
}
687742
],
688743
"tags": [
@@ -702,6 +757,14 @@
702757
],
703758
"default": "VISIBILITY_ENUM_UNSPECIFIED"
704759
},
760+
"VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault": {
761+
"type": "string",
762+
"enum": [
763+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED",
764+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
765+
],
766+
"default": "VISIBILITY_ENUM_HIDDEN_DEFAULT_UNSPECIFIED"
767+
},
705768
"examplepbVisibilityRuleEmbedded": {
706769
"type": "object",
707770
"properties": {
@@ -780,6 +843,9 @@
780843
},
781844
"anEnum": {
782845
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnum"
846+
},
847+
"ahiddenDefaultEnum": {
848+
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault"
783849
}
784850
},
785851
"description": "SimpleMessage represents a simple message sent to the Echo service."

examples/internal/proto/examplepb/visibility_rule_preview_echo_service.swagger.json

+36
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,15 @@
125125
"VISIBILITY_ENUM_PREVIEW"
126126
],
127127
"default": "VISIBILITY_ENUM_UNSPECIFIED"
128+
},
129+
{
130+
"name": "ahiddenDefaultEnum",
131+
"in": "query",
132+
"required": false,
133+
"type": "string",
134+
"enum": [
135+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
136+
]
128137
}
129138
],
130139
"tags": [
@@ -240,6 +249,15 @@
240249
"VISIBILITY_ENUM_PREVIEW"
241250
],
242251
"default": "VISIBILITY_ENUM_UNSPECIFIED"
252+
},
253+
{
254+
"name": "ahiddenDefaultEnum",
255+
"in": "query",
256+
"required": false,
257+
"type": "string",
258+
"enum": [
259+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
260+
]
243261
}
244262
],
245263
"tags": [
@@ -355,6 +373,15 @@
355373
"VISIBILITY_ENUM_PREVIEW"
356374
],
357375
"default": "VISIBILITY_ENUM_UNSPECIFIED"
376+
},
377+
{
378+
"name": "ahiddenDefaultEnum",
379+
"in": "query",
380+
"required": false,
381+
"type": "string",
382+
"enum": [
383+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
384+
]
358385
}
359386
],
360387
"tags": [
@@ -373,6 +400,12 @@
373400
],
374401
"default": "VISIBILITY_ENUM_UNSPECIFIED"
375402
},
403+
"VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault": {
404+
"type": "string",
405+
"enum": [
406+
"VISIBILITY_ENUM_HIDDEN_DEFAULT_VISIBLE"
407+
]
408+
},
376409
"examplepbVisibilityRuleEmbedded": {
377410
"type": "object",
378411
"properties": {
@@ -445,6 +478,9 @@
445478
},
446479
"anEnum": {
447480
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnum"
481+
},
482+
"ahiddenDefaultEnum": {
483+
"$ref": "#/definitions/VisibilityRuleSimpleMessageVisibilityEnumHiddenDefault"
448484
}
449485
},
450486
"description": "SimpleMessage represents a simple message sent to the Echo service."

protoc-gen-openapiv2/internal/genopenapi/template.go

+3
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,9 @@ func getEnumDefault(reg *descriptor.Registry, enum *descriptor.Enum) interface{}
150150
if !reg.GetOmitEnumDefaultValue() {
151151
for _, value := range enum.GetValue() {
152152
if value.GetNumber() == 0 {
153+
if !isVisible(getEnumValueVisibilityOption(value), reg) {
154+
return nil
155+
}
153156
return value.GetName()
154157
}
155158
}

0 commit comments

Comments
 (0)