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

fix: support service tags in OpenAPI config file (#2817) #2858

Merged
merged 4 commits into from
Sep 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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 @@ -17,6 +17,11 @@ info:
x-something-something: "yadda"
tags:
- name: "UnannotatedEchoService"
description: "UnannotatedEchoService description -- which should not be used in\
\ place of the documentation comment!"
externalDocs:
description: "Find out more about UnannotatedEchoService"
url: "https://github.com/grpc-ecosystem/grpc-gateway"
schemes:
- "http"
- "https"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,12 @@
},
"tags": [
{
"name": "UnannotatedEchoService"
"name": "UnannotatedEchoService",
"description": "UnannotatedEchoService description -- which should not be used in place of the documentation comment!",
"externalDocs": {
"description": "Find out more about UnannotatedEchoService",
"url": "https://github.com/grpc-ecosystem/grpc-gateway"
}
johanbrandhorst marked this conversation as resolved.
Show resolved Hide resolved
}
],
"schemes": [
Expand Down
41 changes: 34 additions & 7 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -1018,14 +1018,13 @@ func renderServiceTags(services []*descriptor.Service, reg *descriptor.Registry)
tag := openapiTagObject{
Name: tagName,
}
if proto.HasExtension(svc.Options, openapi_options.E_Openapiv2Tag) {
ext := proto.GetExtension(svc.Options, openapi_options.E_Openapiv2Tag)
opts, ok := ext.(*openapi_options.Tag)
if !ok {
glog.Errorf("extension is %T; want an OpenAPI Tag object", ext)
return nil
}

opts, err := getServiceOpenAPIOption(reg, svc)
if err != nil {
glog.Error(err)
return nil
}
if opts != nil {
tag.Description = opts.Description
if opts.ExternalDocs != nil {
tag.ExternalDocs = &openapiExternalDocumentationObject{
Expand Down Expand Up @@ -2362,6 +2361,23 @@ func extractSchemaOptionFromMessageDescriptor(msg *descriptorpb.DescriptorProto)
return opts, nil
}

// extractTagOptionFromServiceDescriptor extracts the tag of type
// openapi_options.Tag from a given proto service's descriptor.
func extractTagOptionFromServiceDescriptor(svc *descriptorpb.ServiceDescriptorProto) (*openapi_options.Tag, error) {
if svc.Options == nil {
return nil, nil
}
if !proto.HasExtension(svc.Options, openapi_options.E_Openapiv2Tag) {
return nil, nil
}
ext := proto.GetExtension(svc.Options, openapi_options.E_Openapiv2Tag)
opts, ok := ext.(*openapi_options.Tag)
if !ok {
return nil, fmt.Errorf("extension is %T; want a Tag", ext)
}
return opts, nil
}

// extractOpenAPIOptionFromFileDescriptor extracts the message of type
// openapi_options.OpenAPI from a given proto method's descriptor.
func extractOpenAPIOptionFromFileDescriptor(file *descriptorpb.FileDescriptorProto) (*openapi_options.Swagger, error) {
Expand Down Expand Up @@ -2499,6 +2515,17 @@ func getMessageOpenAPIOption(reg *descriptor.Registry, msg *descriptor.Message)
return opts, nil
}

func getServiceOpenAPIOption(reg *descriptor.Registry, svc *descriptor.Service) (*openapi_options.Tag, error) {
if opts, ok := reg.GetOpenAPIServiceOption(svc.FQSN()); ok {
return opts, nil
}
opts, err := extractTagOptionFromServiceDescriptor(svc.ServiceDescriptorProto)
if err != nil {
return nil, err
}
return opts, nil
}

func getFileOpenAPIOption(reg *descriptor.Registry, file *descriptor.File) (*openapi_options.Swagger, error) {
opts, err := extractOpenAPIOptionFromFileDescriptor(file.FileDescriptorProto)
if err != nil {
Expand Down
104 changes: 104 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1517,6 +1517,110 @@ func TestApplyTemplateMultiService(t *testing.T) {
}
}

func TestApplyTemplateOpenAPIConfigFromYAML(t *testing.T) {
msgdesc := &descriptorpb.DescriptorProto{
Name: proto.String("ExampleMessage"),
}
meth := &descriptorpb.MethodDescriptorProto{
Name: proto.String("Example"),
InputType: proto.String("ExampleMessage"),
OutputType: proto.String("ExampleMessage"),
}
svc := &descriptorpb.ServiceDescriptorProto{
Name: proto.String("ExampleService"),
Method: []*descriptorpb.MethodDescriptorProto{meth},
}
msg := &descriptor.Message{
DescriptorProto: msgdesc,
}
file := descriptor.File{
FileDescriptorProto: &descriptorpb.FileDescriptorProto{
SourceCodeInfo: &descriptorpb.SourceCodeInfo{},
Name: proto.String("example.proto"),
Package: proto.String("example"),
MessageType: []*descriptorpb.DescriptorProto{msgdesc},
Service: []*descriptorpb.ServiceDescriptorProto{svc},
Options: &descriptorpb.FileOptions{
GoPackage: proto.String("github.com/grpc-ecosystem/grpc-gateway/runtime/internal/examplepb;example"),
},
},
GoPkg: descriptor.GoPackage{
Path: "example.com/path/to/example/example.pb",
Name: "example_pb",
},
Messages: []*descriptor.Message{msg},
Services: []*descriptor.Service{
{
ServiceDescriptorProto: svc,
Methods: []*descriptor.Method{
{
MethodDescriptorProto: meth,
RequestType: msg,
ResponseType: msg,
Bindings: []*descriptor.Binding{
{
HTTPMethod: "GET",
Body: &descriptor.Body{FieldPath: nil},
PathTmpl: httprule.Template{
Version: 1,
OpCodes: []int{0, 0},
Template: "/v1/echo", // TODO(achew22): Figure out what this should really be
},
},
},
},
},
},
},
}
reg := descriptor.NewRegistry()
if err := AddErrorDefs(reg); err != nil {
t.Errorf("AddErrorDefs(%#v) failed with %v; want success", reg, err)
return
}
fileCL := crossLinkFixture(&file)
err := reg.Load(reqFromFile(fileCL))
if err != nil {
t.Errorf("reg.Load(%#v) failed with %v; want success", file, err)
return
}
openapiOptions := &openapiconfig.OpenAPIOptions{
Service: []*openapiconfig.OpenAPIServiceOption{
{
Service: "example.ExampleService",
Option: &openapi_options.Tag{
Description: "ExampleService description",
ExternalDocs: &openapi_options.ExternalDocumentation{
Description: "Find out more about ExampleService",
},
},
},
},
}
if err := reg.RegisterOpenAPIOptions(openapiOptions); err != nil {
t.Errorf("reg.RegisterOpenAPIOptions for Service %#v failed with %v; want success", openapiOptions.Service, err)
return
}

result, err := applyTemplate(param{File: fileCL, reg: reg})
if err != nil {
t.Errorf("applyTemplate(%#v) failed with %v; want success", file, err)
return
}
if want, is, name := "ExampleService description", result.Tags[0].Description, "Tags[0].Description"; !reflect.DeepEqual(is, want) {
t.Errorf("applyTemplate(%#v).%s = %s want to be %s", file, name, is, want)
}
if want, is, name := "Find out more about ExampleService", result.Tags[0].ExternalDocs.Description, "Tags[0].ExternalDocs.Description"; !reflect.DeepEqual(is, want) {
t.Errorf("applyTemplate(%#v).%s = %s want to be %s", file, name, is, want)
}

// If there was a failure, print out the input and the json result for debugging.
if t.Failed() {
t.Errorf("had: %s", file)
t.Errorf("got: %s", fmt.Sprint(result))
}
}

func TestApplyTemplateOverrideOperationID(t *testing.T) {
newFile := func() *descriptor.File {
msgdesc := &descriptorpb.DescriptorProto{
Expand Down