Skip to content

Commit

Permalink
protoc-gen-openapiv2: Add openapiv2_tag support (#1866)
Browse files Browse the repository at this point in the history
* protoc-gen-openapiv2: support for openapiv2_tag

Fixes #690

Signed-off-by: Ryan Goodfellow <[email protected]>

* more consistent with rest of codebase

Signed-off-by: Ryan Goodfellow <[email protected]>

* update example specs, revert protoc version change

Signed-off-by: Ryan Goodfellow <[email protected]>

* Address review comments

- Factor service tag rendering out of service rendering function
- Add omitempty tags where applicable

Signed-off-by: Ryan Goodfellow <[email protected]>

* remove extraneous newline

Signed-off-by: Ryan Goodfellow <[email protected]>

* renderServices call to previous form

Signed-off-by: Ryan Goodfellow <[email protected]>
  • Loading branch information
rcgoodfellow committed Dec 9, 2020
1 parent 94f7f11 commit 82c9623
Show file tree
Hide file tree
Showing 16 changed files with 115 additions and 0 deletions.
9 changes: 9 additions & 0 deletions examples/internal/clients/abe/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,15 @@ info:
name: "BSD 3-Clause License"
url: "https://github.com/grpc-ecosystem/grpc-gateway/blob/master/LICENSE.txt"
x-something-something: "yadda"
tags:
- name: "ABitOfEverythingService"
description: "ABitOfEverythingService description -- which should not be used in\
\ place of the documentation comment!"
externalDocs:
description: "Find out more about EchoService"
url: "https://github.com/grpc-ecosystem/grpc-gateway"
- name: "camelCaseServiceName"
- name: "AnotherServiceWithNoBindings"
schemes:
- "http"
- "https"
Expand Down
2 changes: 2 additions & 0 deletions examples/internal/clients/echo/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ info:
description: "Echo Service API consists of a single service which returns\na message."
version: "version not set"
title: "Echo Service"
tags:
- name: "EchoService"
consumes:
- "application/json"
produces:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ info:
\ Methods Echo Service API consists of a single service which returns\na message."
version: "version not set"
title: "examples/internal/proto/examplepb/generate_unbound_methods.proto"
tags:
- name: "GenerateUnboundMethodsEchoService"
consumes:
- "application/json"
produces:
Expand Down
2 changes: 2 additions & 0 deletions examples/internal/clients/responsebody/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ swagger: "2.0"
info:
version: "version not set"
title: "examples/internal/proto/examplepb/response_body_service.proto"
tags:
- name: "ResponseBodyService"
consumes:
- "application/json"
produces:
Expand Down
2 changes: 2 additions & 0 deletions examples/internal/clients/unannotatedecho/api/swagger.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ info:
name: "BSD 3-Clause License"
url: "https://github.com/grpc-ecosystem/grpc-gateway/blob/master/LICENSE.txt"
x-something-something: "yadda"
tags:
- name: "UnannotatedEchoService"
schemes:
- "http"
- "https"
Expand Down
16 changes: 16 additions & 0 deletions examples/internal/proto/examplepb/a_bit_of_everything.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@
},
"x-something-something": "yadda"
},
"tags": [
{
"name": "ABitOfEverythingService",
"description": "ABitOfEverythingService description -- which should not be used in place of the documentation comment!",
"externalDocs": {
"description": "Find out more about EchoService",
"url": "https://github.com/grpc-ecosystem/grpc-gateway"
}
},
{
"name": "camelCaseServiceName"
},
{
"name": "AnotherServiceWithNoBindings"
}
],
"schemes": [
"http",
"https",
Expand Down
5 changes: 5 additions & 0 deletions examples/internal/proto/examplepb/echo_service.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"description": "Echo Service API consists of a single service which returns\na message.",
"version": "version not set"
},
"tags": [
{
"name": "EchoService"
}
],
"consumes": [
"application/json"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
"description": "Generate Unannotated Methods Echo Service\nSimilar to echo_service.proto but without annotations and without external configuration.\n\nGenerate Unannotated Methods Echo Service API consists of a single service which returns\na message.",
"version": "version not set"
},
"tags": [
{
"name": "GenerateUnboundMethodsEchoService"
}
],
"consumes": [
"application/json"
],
Expand Down
11 changes: 11 additions & 0 deletions examples/internal/proto/examplepb/openapi_merge.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,17 @@
"description": "This is an example of merging two proto files.",
"version": "version not set"
},
"tags": [
{
"name": "ServiceA"
},
{
"name": "ServiceC"
},
{
"name": "ServiceB"
}
],
"consumes": [
"application/json"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"title": "examples/internal/proto/examplepb/response_body_service.proto",
"version": "version not set"
},
"tags": [
{
"name": "ResponseBodyService"
}
],
"consumes": [
"application/json"
],
Expand Down
5 changes: 5 additions & 0 deletions examples/internal/proto/examplepb/stream.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"title": "examples/internal/proto/examplepb/stream.proto",
"version": "version not set"
},
"tags": [
{
"name": "StreamService"
}
],
"consumes": [
"application/json"
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@
},
"x-something-something": "yadda"
},
"tags": [
{
"name": "UnannotatedEchoService"
}
],
"schemes": [
"http",
"https",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"title": "examples/internal/proto/examplepb/use_go_template.proto",
"version": "version not set"
},
"tags": [
{
"name": "LoginService"
}
],
"consumes": [
"application/json"
],
Expand Down
5 changes: 5 additions & 0 deletions examples/internal/proto/examplepb/wrappers.swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@
"title": "examples/internal/proto/examplepb/wrappers.proto",
"version": "version not set"
},
"tags": [
{
"name": "WrappersService"
}
],
"consumes": [
"application/json"
],
Expand Down
28 changes: 28 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -782,6 +782,33 @@ func isResourceName(prefix string) bool {
return field == "parent" || field == "name"
}

func renderServiceTags(services []*descriptor.Service) []openapiTagObject {
var tags []openapiTagObject
for _, svc := range services {
tag := openapiTagObject{
Name: *svc.Name,
}
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
}

tag.Description = opts.Description
if opts.ExternalDocs != nil {
tag.ExternalDocs = &openapiExternalDocumentationObject{
Description: opts.ExternalDocs.Description,
URL: opts.ExternalDocs.Url,
}
}
}
tags = append(tags, tag)
}
return tags
}

func renderServices(services []*descriptor.Service, paths openapiPathsObject, reg *descriptor.Registry, requestResponseRefs, customRefs refMap, msgs []*descriptor.Message) error {
// Correctness of svcIdx and methIdx depends on 'services' containing the services in the same order as the 'file.Service' array.
svcBaseIdx := 0
Expand Down Expand Up @@ -1199,6 +1226,7 @@ func applyTemplate(p param) (*openapiSwaggerObject, error) {
if err := renderServices(p.Services, s.Paths, p.reg, requestResponseRefs, customRefs, p.Messages); err != nil {
panic(err)
}
s.Tags = append(s.Tags, renderServiceTags(p.Services)...)

messages := messageMap{}
streamingMessages := messageMap{}
Expand Down
8 changes: 8 additions & 0 deletions protoc-gen-openapiv2/internal/genopenapi/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ type openapiInfoObject struct {
extensions []extension
}

// https://swagger.io/specification/#tagObject
type openapiTagObject struct {
Name string `json:"name"`
Description string `json:"description,omitempty"`
ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty"`
}

// http://swagger.io/specification/#contactObject
type openapiContactObject struct {
Name string `json:"name,omitempty"`
Expand Down Expand Up @@ -54,6 +61,7 @@ type extension struct {
type openapiSwaggerObject struct {
Swagger string `json:"swagger"`
Info openapiInfoObject `json:"info"`
Tags []openapiTagObject `json:"tags,omitempty"`
Host string `json:"host,omitempty"`
BasePath string `json:"basePath,omitempty"`
Schemes []string `json:"schemes,omitempty"`
Expand Down

0 comments on commit 82c9623

Please sign in to comment.