From 4e0ecd0f283a990d61c867c2e55635a11931e2aa Mon Sep 17 00:00:00 2001 From: Stefan Sauer Date: Tue, 8 May 2018 09:31:09 +0200 Subject: [PATCH] Add Handling of 'License' message to the annotation proto. This information is used by the default html doc generator and so it is nice to be able to provide it. Add the message to openapiv2.proto and extend template.go to handle it. Tested via: bazel build //examples/proto/examplepb:expamplepb_protoc_gen_swagger more bazel-bin/examples/proto/examplepb/a_bit_of_everything.swagger.json --- examples/proto/examplepb/a_bit_of_everything.proto | 4 ++++ protoc-gen-swagger/genswagger/template.go | 11 +++++++++++ protoc-gen-swagger/options/openapiv2.proto | 14 ++++++++++++-- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/examples/proto/examplepb/a_bit_of_everything.proto b/examples/proto/examplepb/a_bit_of_everything.proto index 6eaa7d8fd9a..ff7eda46b68 100644 --- a/examples/proto/examplepb/a_bit_of_everything.proto +++ b/examples/proto/examplepb/a_bit_of_everything.proto @@ -19,6 +19,10 @@ option (grpc.gateway.protoc_gen_swagger.options.openapiv2_swagger) = { url: "https://github.com/grpc-ecosystem/grpc-gateway"; email: "none@example.com"; }; + license: { + name: "BSD 3-Clause License"; + url: "https://github.com/grpc-ecosystem/grpc-gateway/blob/master/LICENSE.txt"; + }; }; // Overwriting host entry breaks tests, so this is not done here. external_docs: { diff --git a/protoc-gen-swagger/genswagger/template.go b/protoc-gen-swagger/genswagger/template.go index d2b73e1ad6c..2d6cd52da8c 100644 --- a/protoc-gen-swagger/genswagger/template.go +++ b/protoc-gen-swagger/genswagger/template.go @@ -784,6 +784,17 @@ func applyTemplate(p param) (string, error) { s.Info.Contact.Email = spb.Info.Contact.Email } } + if spb.Info.License != nil { + if s.Info.License == nil { + s.Info.License = &swaggerLicenseObject{} + } + if spb.Info.License.Name != "" { + s.Info.License.Name = spb.Info.License.Name + } + if spb.Info.License.Url != "" { + s.Info.License.URL = spb.Info.License.Url + } + } } if spb.Host != "" { s.Host = spb.Host diff --git a/protoc-gen-swagger/options/openapiv2.proto b/protoc-gen-swagger/options/openapiv2.proto index b3b9b89d995..fb9c6493e18 100644 --- a/protoc-gen-swagger/options/openapiv2.proto +++ b/protoc-gen-swagger/options/openapiv2.proto @@ -74,8 +74,7 @@ message Info { string description = 2; string terms_of_service = 3; Contact contact = 4; - // field 5 is reserved for 'license'. - reserved 5; + License license=5; string version = 6; } @@ -90,6 +89,17 @@ message Contact { string email = 3; } +// `License` is a representation of OpenAPI v2 specification's License object. +// +// See: https://github.com/OAI/OpenAPI-Specification/blob/3.0.0/versions/2.0.md#licenseObject +// +message License { + // Required. The license name used for the API. + string name = 1; + // A URL to the license used for the API. + string url = 2; +} + // `ExternalDocumentation` is a representation of OpenAPI v2 specification's // ExternalDocumentation object. //