Skip to content

Commit ab516bc

Browse files
authored
add extensions for Tag object (#2950)
* add extensions for Tag object * comments for extensions in proto file * update extensions url
1 parent 74b6360 commit ab516bc

File tree

13 files changed

+306
-165
lines changed

13 files changed

+306
-165
lines changed

examples/internal/clients/abe/api/swagger.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ tags:
2323
- name: "SnakeEnumService"
2424
- name: "echo rpc"
2525
description: "Echo Rpc description"
26+
x-traitTag: true
2627
schemes:
2728
- "http"
2829
- "https"

examples/internal/clients/unannotatedecho/api/swagger.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ tags:
2222
externalDocs:
2323
description: "Find out more about UnannotatedEchoService"
2424
url: "https://github.com/grpc-ecosystem/grpc-gateway"
25+
- name: "Echo"
26+
description: "Echo description"
27+
- name: "Internal"
28+
description: "Internal description"
29+
x-traitTag: true
2530
schemes:
2631
- "http"
2732
- "https"

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

Lines changed: 15 additions & 14 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/internal/proto/examplepb/a_bit_of_everything.proto

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,12 @@ option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
188188
tags: {
189189
name: "echo rpc"
190190
description: "Echo Rpc description"
191+
extensions: {
192+
key: "x-traitTag";
193+
value {
194+
bool_value: true;
195+
}
196+
}
191197
}
192198
extensions: {
193199
key: "x-grpc-gateway-foo";

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
},
3535
{
3636
"name": "echo rpc",
37-
"description": "Echo Rpc description"
37+
"description": "Echo Rpc description",
38+
"x-traitTag": true
3839
}
3940
],
4041
"schemes": [

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,15 @@
2323
"description": "Find out more about UnannotatedEchoService",
2424
"url": "https://github.com/grpc-ecosystem/grpc-gateway"
2525
}
26+
},
27+
{
28+
"name": "Echo",
29+
"description": "Echo description"
30+
},
31+
{
32+
"name": "Internal",
33+
"description": "Internal description",
34+
"x-traitTag": true
2635
}
2736
],
2837
"schemes": [

examples/internal/proto/examplepb/unannotated_echo_service.swagger.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,13 @@ openapiOptions:
1515
version: "1.0"
1616
extensions:
1717
x-something-something: yadda
18+
tags:
19+
- name: Echo
20+
description: Echo description
21+
- name: Internal
22+
description: Internal description
23+
extensions:
24+
x-traitTag: true
1825
schemes:
1926
- HTTP
2027
- HTTPS

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,23 @@ func (so openapiParameterObject) MarshalYAML() (interface{}, error) {
218218
}, nil
219219
}
220220

221+
func (so openapiTagObject) MarshalJSON() ([]byte, error) {
222+
type alias openapiTagObject
223+
return extensionMarshalJSON(alias(so), so.extensions)
224+
}
225+
226+
func (so openapiTagObject) MarshalYAML() (interface{}, error) {
227+
type Alias openapiTagObject
228+
229+
return struct {
230+
Extension map[string]interface{} `yaml:",inline"`
231+
Alias `yaml:",inline"`
232+
}{
233+
Extension: extensionsToMap(so.extensions),
234+
Alias: Alias(so),
235+
}, nil
236+
}
237+
221238
func extensionMarshalJSON(so interface{}, extensions []extension) ([]byte, error) {
222239
// To append arbitrary keys to the struct we'll render into json,
223240
// we're creating another struct that embeds the original one, and

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,6 +1858,13 @@ func applyTemplate(p param) (*openapiSwaggerObject, error) {
18581858
URL: v.ExternalDocs.Url,
18591859
}
18601860
}
1861+
if v.Extensions != nil {
1862+
exts, err := processExtensions(v.Extensions)
1863+
if err != nil {
1864+
return nil, err
1865+
}
1866+
newTag.extensions = exts
1867+
}
18611868
s.Tags = append(s.Tags, newTag)
18621869
}
18631870
}

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

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1834,6 +1834,15 @@ func TestApplyTemplateExtensions(t *testing.T) {
18341834
},
18351835
},
18361836
},
1837+
Tags: []*openapi_options.Tag{
1838+
{
1839+
Name: "test tag",
1840+
Description: "test tag description",
1841+
Extensions: map[string]*structpb.Value{
1842+
"x-traitTag": {Kind: &structpb.Value_BoolValue{BoolValue: true}},
1843+
},
1844+
},
1845+
},
18371846
}
18381847
openapiOperation := openapi_options.Operation{
18391848
Responses: map[string]*openapi_options.Response{
@@ -1936,6 +1945,16 @@ func TestApplyTemplateExtensions(t *testing.T) {
19361945
}, response.extensions, "response.Extensions"; !reflect.DeepEqual(is, want) {
19371946
t.Errorf("applyTemplate(%#v).%s = %s want to be %s", file, name, is, want)
19381947
}
1948+
1949+
var tag openapiTagObject
1950+
for _, v := range result.Tags {
1951+
tag = v
1952+
}
1953+
if want, is, name := []extension{
1954+
{key: "x-traitTag", value: json.RawMessage("true")},
1955+
}, tag.extensions, "Tags[0].Extensions"; !reflect.DeepEqual(is, want) {
1956+
t.Errorf("applyTemplate(%#v).%s = %s want to be %s", file, name, is, want)
1957+
}
19391958
}
19401959
t.Run("verify template options set via proto options", func(t *testing.T) {
19411960
file := newFile()

0 commit comments

Comments
 (0)