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

add extensions for Tag object #2950

Merged
merged 3 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/internal/clients/abe/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ tags:
- name: "SnakeEnumService"
- name: "echo rpc"
description: "Echo Rpc description"
x-traitTag: true
schemes:
- "http"
- "https"
Expand Down
5 changes: 5 additions & 0 deletions examples/internal/clients/unannotatedecho/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ tags:
externalDocs:
description: "Find out more about UnannotatedEchoService"
url: "https://github.com/grpc-ecosystem/grpc-gateway"
- name: "Echo"
description: "Echo description"
- name: "Internal"
description: "Internal description"
x-traitTag: true
schemes:
- "http"
- "https"
Expand Down
29 changes: 15 additions & 14 deletions examples/internal/proto/examplepb/a_bit_of_everything.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions examples/internal/proto/examplepb/a_bit_of_everything.proto
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,12 @@ option (grpc.gateway.protoc_gen_openapiv2.options.openapiv2_swagger) = {
tags: {
name: "echo rpc"
description: "Echo Rpc description"
extensions: {
key: "x-traitTag";
value {
bool_value: true;
}
}
}
extensions: {
key: "x-grpc-gateway-foo";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@
},
{
"name": "echo rpc",
"description": "Echo Rpc description"
"description": "Echo Rpc description",
"x-traitTag": true
}
],
"schemes": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,15 @@
"description": "Find out more about UnannotatedEchoService",
"url": "https://github.com/grpc-ecosystem/grpc-gateway"
}
},
{
"name": "Echo",
"description": "Echo description"
},
{
"name": "Internal",
"description": "Internal description",
"x-traitTag": true
}
],
"schemes": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,13 @@ openapiOptions:
version: "1.0"
extensions:
x-something-something: yadda
tags:
- name: Echo
description: Echo description
- name: Internal
description: Internal description
extensions:
x-traitTag: true
schemes:
- HTTP
- HTTPS
Expand Down
17 changes: 17 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,23 @@ func (so openapiParameterObject) MarshalYAML() (interface{}, error) {
}, nil
}

func (so openapiTagObject) MarshalJSON() ([]byte, error) {
type alias openapiTagObject
return extensionMarshalJSON(alias(so), so.extensions)
}

func (so openapiTagObject) MarshalYAML() (interface{}, error) {
type Alias openapiTagObject

return struct {
Extension map[string]interface{} `yaml:",inline"`
Alias `yaml:",inline"`
}{
Extension: extensionsToMap(so.extensions),
Alias: Alias(so),
}, nil
}

func extensionMarshalJSON(so interface{}, extensions []extension) ([]byte, error) {
// To append arbitrary keys to the struct we'll render into json,
// we're creating another struct that embeds the original one, and
Expand Down
7 changes: 7 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1858,6 +1858,13 @@ func applyTemplate(p param) (*openapiSwaggerObject, error) {
URL: v.ExternalDocs.Url,
}
}
if v.Extensions != nil {
exts, err := processExtensions(v.Extensions)
if err != nil {
return nil, err
}
newTag.extensions = exts
}
s.Tags = append(s.Tags, newTag)
}
}
Expand Down
19 changes: 19 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1834,6 +1834,15 @@ func TestApplyTemplateExtensions(t *testing.T) {
},
},
},
Tags: []*openapi_options.Tag{
{
Name: "test tag",
Description: "test tag description",
Extensions: map[string]*structpb.Value{
"x-traitTag": {Kind: &structpb.Value_BoolValue{BoolValue: true}},
},
},
},
}
openapiOperation := openapi_options.Operation{
Responses: map[string]*openapi_options.Response{
Expand Down Expand Up @@ -1936,6 +1945,16 @@ func TestApplyTemplateExtensions(t *testing.T) {
}, response.extensions, "response.Extensions"; !reflect.DeepEqual(is, want) {
t.Errorf("applyTemplate(%#v).%s = %s want to be %s", file, name, is, want)
}

var tag openapiTagObject
for _, v := range result.Tags {
tag = v
}
if want, is, name := []extension{
{key: "x-traitTag", value: json.RawMessage("true")},
}, tag.extensions, "Tags[0].Extensions"; !reflect.DeepEqual(is, want) {
t.Errorf("applyTemplate(%#v).%s = %s want to be %s", file, name, is, want)
}
}
t.Run("verify template options set via proto options", func(t *testing.T) {
file := newFile()
Expand Down
2 changes: 2 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ type openapiTagObject struct {
Name string `json:"name" yaml:"name"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty" yaml:"externalDocs,omitempty"`

extensions []extension `json:"-" yaml:"-"`
}

// http://swagger.io/specification/#contactObject
Expand Down
Loading