main.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. // Copyright 2017 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. // report is a demo application that displays information about an
  15. // OpenAPI description.
  16. package main
  17. import (
  18. "flag"
  19. "fmt"
  20. "io/ioutil"
  21. "os"
  22. "github.com/golang/protobuf/proto"
  23. "github.com/googleapis/gnostic/printer"
  24. pb "github.com/googleapis/gnostic/OpenAPIv2"
  25. )
  26. func readDocumentFromFileWithName(filename string) *pb.Document {
  27. data, err := ioutil.ReadFile(filename)
  28. if err != nil {
  29. fmt.Printf("File error: %v\n", err)
  30. os.Exit(1)
  31. }
  32. document := &pb.Document{}
  33. err = proto.Unmarshal(data, document)
  34. if err != nil {
  35. panic(err)
  36. }
  37. return document
  38. }
  39. func printDocument(code *printer.Code, document *pb.Document) {
  40. code.Print("BasePath: %+v", document.BasePath)
  41. code.Print("Consumes: %+v", document.Consumes)
  42. code.Print("Definitions:")
  43. code.Indent()
  44. if document.Definitions != nil && document.Definitions.AdditionalProperties != nil {
  45. for _, pair := range document.Definitions.AdditionalProperties {
  46. code.Print("%s", pair.Name)
  47. code.Indent()
  48. printSchema(code, pair.Value)
  49. code.Outdent()
  50. }
  51. }
  52. code.Outdent()
  53. code.Print("ExternalDocs: %+v", document.ExternalDocs)
  54. code.Print("Host: %+v", document.Host)
  55. if document.Info != nil {
  56. code.Print("Info:")
  57. code.Indent()
  58. code.Print("Title: %s", document.Info.Title)
  59. code.Print("Description: %s", document.Info.Description)
  60. code.Print("Version: %s", document.Info.Version)
  61. code.Print("TermsOfService: %s", document.Info.TermsOfService)
  62. if document.Info.Contact != nil {
  63. code.Print("Contact Email: %s", document.Info.Contact.Email)
  64. }
  65. if document.Info.License != nil {
  66. code.Print("License Name: %s", document.Info.License.Name)
  67. code.Print("License URL: %s", document.Info.License.Url)
  68. }
  69. code.Outdent()
  70. }
  71. code.Print("Parameters: %+v", document.Parameters)
  72. code.Print("Paths:")
  73. code.Indent()
  74. for _, pair := range document.Paths.Path {
  75. code.Print("%+v", pair.Name)
  76. code.Indent()
  77. v := pair.Value
  78. if v.Get != nil {
  79. code.Print("GET")
  80. code.Indent()
  81. printOperation(code, v.Get)
  82. code.Outdent()
  83. }
  84. if v.Post != nil {
  85. code.Print("POST")
  86. code.Indent()
  87. printOperation(code, v.Post)
  88. code.Outdent()
  89. }
  90. code.Outdent()
  91. }
  92. code.Outdent()
  93. code.Print("Produces: %+v", document.Produces)
  94. code.Print("Responses: %+v", document.Responses)
  95. code.Print("Schemes: %+v", document.Schemes)
  96. code.Print("Security: %+v", document.Security)
  97. if document.SecurityDefinitions != nil {
  98. code.Print("SecurityDefinitions:")
  99. code.Indent()
  100. for _, pair := range document.SecurityDefinitions.AdditionalProperties {
  101. code.Print("%s", pair.Name)
  102. code.Indent()
  103. v := pair.Value
  104. switch t := v.Oneof.(type) {
  105. default:
  106. code.Print("unexpected type %T", t) // %T prints whatever type t has
  107. case *pb.SecurityDefinitionsItem_ApiKeySecurity:
  108. code.Print("ApiKeySecurity: %+v", t)
  109. case *pb.SecurityDefinitionsItem_BasicAuthenticationSecurity:
  110. code.Print("BasicAuthenticationSecurity: %+v", t)
  111. case *pb.SecurityDefinitionsItem_Oauth2AccessCodeSecurity:
  112. code.Print("Oauth2AccessCodeSecurity: %+v", t)
  113. case *pb.SecurityDefinitionsItem_Oauth2ApplicationSecurity:
  114. code.Print("Oauth2ApplicationSecurity: %+v", t)
  115. case *pb.SecurityDefinitionsItem_Oauth2ImplicitSecurity:
  116. code.Print("Oauth2ImplicitSecurity")
  117. code.Indent()
  118. code.Print("AuthorizationUrl: %+v", t.Oauth2ImplicitSecurity.AuthorizationUrl)
  119. code.Print("Flow: %+v", t.Oauth2ImplicitSecurity.Flow)
  120. code.Print("Scopes:")
  121. code.Indent()
  122. for _, pair := range t.Oauth2ImplicitSecurity.Scopes.AdditionalProperties {
  123. code.Print("%s -> %s", pair.Name, pair.Value)
  124. }
  125. code.Outdent()
  126. code.Outdent()
  127. case *pb.SecurityDefinitionsItem_Oauth2PasswordSecurity:
  128. code.Print("Oauth2PasswordSecurity: %+v", t)
  129. }
  130. code.Outdent()
  131. }
  132. code.Outdent()
  133. }
  134. code.Print("Swagger: %+v", document.Swagger)
  135. code.Print("Tags:")
  136. code.Indent()
  137. for _, tag := range document.Tags {
  138. code.Print("Tag:")
  139. code.Indent()
  140. code.Print("Name: %s", tag.Name)
  141. code.Print("Description: %s", tag.Description)
  142. code.Print("ExternalDocs: %s", tag.ExternalDocs)
  143. printVendorExtension(code, tag.VendorExtension)
  144. code.Outdent()
  145. }
  146. code.Outdent()
  147. }
  148. func printOperation(code *printer.Code, operation *pb.Operation) {
  149. code.Print("Consumes: %+v", operation.Consumes)
  150. code.Print("Deprecated: %+v", operation.Deprecated)
  151. code.Print("Description: %+v", operation.Description)
  152. code.Print("ExternalDocs: %+v", operation.ExternalDocs)
  153. code.Print("OperationId: %+v", operation.OperationId)
  154. code.Print("Parameters:")
  155. code.Indent()
  156. for _, item := range operation.Parameters {
  157. switch t := item.Oneof.(type) {
  158. default:
  159. code.Print("unexpected type %T", t) // %T prints whatever type t has
  160. case *pb.ParametersItem_JsonReference:
  161. code.Print("JsonReference: %+v", t)
  162. case *pb.ParametersItem_Parameter:
  163. code.Print("Parameter: %+v", t)
  164. }
  165. }
  166. code.Outdent()
  167. code.Print("Produces: %+v", operation.Produces)
  168. code.Print("Responses:")
  169. code.Indent()
  170. code.Print("ResponseCode:")
  171. code.Indent()
  172. for _, pair := range operation.Responses.ResponseCode {
  173. code.Print("%s %s", pair.Name, pair.Value)
  174. }
  175. code.Outdent()
  176. printVendorExtension(code, operation.Responses.VendorExtension)
  177. code.Outdent()
  178. code.Print("Schemes: %+v", operation.Schemes)
  179. code.Print("Security: %+v", operation.Security)
  180. code.Print("Summary: %+v", operation.Summary)
  181. code.Print("Tags: %+v", operation.Tags)
  182. printVendorExtension(code, operation.VendorExtension)
  183. }
  184. func printSchema(code *printer.Code, schema *pb.Schema) {
  185. //code.Print("%+v", schema)
  186. if schema.Format != "" {
  187. code.Print("Format: %+v", schema.Format)
  188. }
  189. if schema.Properties != nil {
  190. code.Print("Properties")
  191. code.Indent()
  192. for _, pair := range schema.Properties.AdditionalProperties {
  193. code.Print("%s", pair.Name)
  194. code.Indent()
  195. printSchema(code, pair.Value)
  196. code.Outdent()
  197. }
  198. code.Outdent()
  199. }
  200. if schema.Type != nil {
  201. code.Print("Type: %+v", schema.Type)
  202. }
  203. if schema.Xml != nil {
  204. code.Print("Xml: %+v", schema.Xml)
  205. }
  206. printVendorExtension(code, schema.VendorExtension)
  207. }
  208. func printVendorExtension(code *printer.Code, vendorExtension []*pb.NamedAny) {
  209. if len(vendorExtension) > 0 {
  210. code.Print("VendorExtension: %+v", vendorExtension)
  211. }
  212. }
  213. func main() {
  214. flag.Parse()
  215. args := flag.Args()
  216. if len(args) != 1 {
  217. fmt.Printf("Usage: report <file.pb>\n")
  218. return
  219. }
  220. document := readDocumentFromFileWithName(args[0])
  221. code := &printer.Code{}
  222. code.Print("API REPORT")
  223. code.Print("----------")
  224. printDocument(code, document)
  225. fmt.Printf("%s", code)
  226. }