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

Use go templates on tag descriptions too #3595

Merged
merged 1 commit into from
Sep 14, 2023
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
12 changes: 12 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1096,11 +1096,17 @@ func renderServiceTags(services []*descriptor.Service, reg *descriptor.Registry)
}
if opts != nil {
tag.Description = opts.Description
if reg.GetUseGoTemplate() {
tag.Description = goTemplateComments(tag.Description, svc, reg)
}
if opts.ExternalDocs != nil {
tag.ExternalDocs = &openapiExternalDocumentationObject{
Description: opts.ExternalDocs.Description,
URL: opts.ExternalDocs.Url,
}
if reg.GetUseGoTemplate() {
tag.ExternalDocs.Description = goTemplateComments(opts.ExternalDocs.Description, svc, reg)
}
}
}
tags = append(tags, tag)
Expand Down Expand Up @@ -1992,11 +1998,17 @@ func applyTemplate(p param) (*openapiSwaggerObject, error) {
newTag := openapiTagObject{}
newTag.Name = v.Name
newTag.Description = v.Description
if p.reg.GetUseGoTemplate() {
newTag.Description = goTemplateComments(newTag.Description, nil, p.reg)
}
if v.ExternalDocs != nil {
newTag.ExternalDocs = &openapiExternalDocumentationObject{
Description: v.ExternalDocs.Description,
URL: v.ExternalDocs.Url,
}
if p.reg.GetUseGoTemplate() {
newTag.ExternalDocs.Description = goTemplateComments(v.ExternalDocs.Description, nil, p.reg)
}
}
if v.Extensions != nil {
exts, err := processExtensions(v.Extensions)
Expand Down
76 changes: 76 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6171,6 +6171,82 @@ func TestMessageOptionsWithGoTemplate(t *testing.T) {
}
}

func TestTagsWithGoTemplate(t *testing.T) {
reg := descriptor.NewRegistry()
reg.SetUseGoTemplate(true)

svc := &descriptorpb.ServiceDescriptorProto{
Name: proto.String("ExampleService"),
Options: &descriptorpb.ServiceOptions{},
}

file := descriptor.File{
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
SourceCodeInfo: &descriptorpb.SourceCodeInfo{},
Name: proto.String("example.proto"),
Package: proto.String("example"),
Dependency: []string{},
MessageType: []*descriptorpb.DescriptorProto{},
EnumType: []*descriptorpb.EnumDescriptorProto{},
Service: []*descriptorpb.ServiceDescriptorProto{svc},
Options: &descriptorpb.FileOptions{
GoPackage: proto.String("github.com/grpc-ecosystem/grpc-gateway/runtime/internal/examplepb;example"),
},
},
Messages: []*descriptor.Message{},
Services: []*descriptor.Service{
{
ServiceDescriptorProto: svc,
},
},
}

// Set tag through service extension
proto.SetExtension(file.GetService()[0].Options, openapi_options.E_Openapiv2Tag, &openapi_options.Tag{
Name: "service tag",
Description: "{{ .Name }}!"})

// Set tags through file extension
swagger := openapi_options.Swagger{
Tags: []*openapi_options.Tag{
{
Name: "not a service tag",
Description: "{{ import \"file\" }}",
},
{
Name: "ExampleService",
},
{
Name: "not a service tag 2",
Description: "{{ import \"file\" }}",
},
},
}
proto.SetExtension(proto.Message(file.FileDescriptorProto.Options), openapi_options.E_Openapiv2Swagger, &swagger)

actual, err := applyTemplate(param{File: crossLinkFixture(&file), reg: reg})
if err != nil {
t.Fatalf("applyTemplate(%#v) failed with %v; want success", file, err)
}
expectedTags := []openapiTagObject{
{
Name: "not a service tag",
Description: "open file: no such file or directory",
},
{
Name: "ExampleService",
Description: "ExampleService!",
},
{
Name: "not a service tag 2",
Description: "open file: no such file or directory",
},
}
if !reflect.DeepEqual(actual.Tags, expectedTags) {
t.Errorf("Expected tags %+v, not %+v", expectedTags, actual.Tags)
}
}

func TestTemplateWithoutErrorDefinition(t *testing.T) {
msgdesc := &descriptorpb.DescriptorProto{
Name: proto.String("ExampleMessage"),
Expand Down