types.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. package genopenapi
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "git.ikuban.com/server/swagger-api/protoc-gen-openapiv2/internal/descriptor"
  7. )
  8. type param struct {
  9. *descriptor.File
  10. reg *descriptor.Registry
  11. }
  12. // http://swagger.io/specification/#infoObject
  13. type openapiInfoObject struct {
  14. Title string `json:"title"`
  15. Description string `json:"description,omitempty"`
  16. TermsOfService string `json:"termsOfService,omitempty"`
  17. Version string `json:"version"`
  18. Contact *openapiContactObject `json:"contact,omitempty"`
  19. License *openapiLicenseObject `json:"license,omitempty"`
  20. extensions []extension
  21. }
  22. // https://swagger.io/specification/#tagObject
  23. type openapiTagObject struct {
  24. Name string `json:"name"`
  25. Description string `json:"description,omitempty"`
  26. ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty"`
  27. }
  28. // http://swagger.io/specification/#contactObject
  29. type openapiContactObject struct {
  30. Name string `json:"name,omitempty"`
  31. URL string `json:"url,omitempty"`
  32. Email string `json:"email,omitempty"`
  33. }
  34. // http://swagger.io/specification/#licenseObject
  35. type openapiLicenseObject struct {
  36. Name string `json:"name,omitempty"`
  37. URL string `json:"url,omitempty"`
  38. }
  39. // http://swagger.io/specification/#externalDocumentationObject
  40. type openapiExternalDocumentationObject struct {
  41. Description string `json:"description,omitempty"`
  42. URL string `json:"url,omitempty"`
  43. }
  44. type extension struct {
  45. key string
  46. value json.RawMessage
  47. }
  48. // http://swagger.io/specification/#swaggerObject
  49. type openapiSwaggerObject struct {
  50. Swagger string `json:"swagger"`
  51. Info openapiInfoObject `json:"info"`
  52. Tags []openapiTagObject `json:"tags,omitempty"`
  53. Host string `json:"host,omitempty"`
  54. BasePath string `json:"basePath,omitempty"`
  55. Schemes []string `json:"schemes,omitempty"`
  56. Consumes []string `json:"consumes"`
  57. Produces []string `json:"produces"`
  58. Paths openapiPathsObject `json:"paths"`
  59. Definitions openapiDefinitionsObject `json:"definitions"`
  60. SecurityDefinitions openapiSecurityDefinitionsObject `json:"securityDefinitions,omitempty"`
  61. Security []openapiSecurityRequirementObject `json:"security,omitempty"`
  62. ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty"`
  63. extensions []extension
  64. }
  65. // http://swagger.io/specification/#securityDefinitionsObject
  66. type openapiSecurityDefinitionsObject map[string]openapiSecuritySchemeObject
  67. // http://swagger.io/specification/#securitySchemeObject
  68. type openapiSecuritySchemeObject struct {
  69. Type string `json:"type"`
  70. Description string `json:"description,omitempty"`
  71. Name string `json:"name,omitempty"`
  72. In string `json:"in,omitempty"`
  73. Flow string `json:"flow,omitempty"`
  74. AuthorizationURL string `json:"authorizationUrl,omitempty"`
  75. TokenURL string `json:"tokenUrl,omitempty"`
  76. Scopes openapiScopesObject `json:"scopes,omitempty"`
  77. extensions []extension
  78. }
  79. // http://swagger.io/specification/#scopesObject
  80. type openapiScopesObject map[string]string
  81. // http://swagger.io/specification/#securityRequirementObject
  82. type openapiSecurityRequirementObject map[string][]string
  83. // http://swagger.io/specification/#pathsObject
  84. type openapiPathsObject map[string]openapiPathItemObject
  85. // http://swagger.io/specification/#pathItemObject
  86. type openapiPathItemObject struct {
  87. Get *openapiOperationObject `json:"get,omitempty"`
  88. Delete *openapiOperationObject `json:"delete,omitempty"`
  89. Post *openapiOperationObject `json:"post,omitempty"`
  90. Put *openapiOperationObject `json:"put,omitempty"`
  91. Patch *openapiOperationObject `json:"patch,omitempty"`
  92. }
  93. // http://swagger.io/specification/#operationObject
  94. type openapiOperationObject struct {
  95. Summary string `json:"summary,omitempty"`
  96. Description string `json:"description,omitempty"`
  97. OperationID string `json:"operationId"`
  98. Responses openapiResponsesObject `json:"responses"`
  99. Parameters openapiParametersObject `json:"parameters,omitempty"`
  100. Tags []string `json:"tags,omitempty"`
  101. Deprecated bool `json:"deprecated,omitempty"`
  102. Produces []string `json:"produces,omitempty"`
  103. Security *[]openapiSecurityRequirementObject `json:"security,omitempty"`
  104. ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty"`
  105. extensions []extension
  106. }
  107. type openapiParametersObject []openapiParameterObject
  108. // http://swagger.io/specification/#parameterObject
  109. type openapiParameterObject struct {
  110. Name string `json:"name"`
  111. Description string `json:"description,omitempty"`
  112. In string `json:"in,omitempty"`
  113. Required bool `json:"required"`
  114. Type string `json:"type,omitempty"`
  115. Format string `json:"format,omitempty"`
  116. Items *openapiItemsObject `json:"items,omitempty"`
  117. Enum []string `json:"enum,omitempty"`
  118. CollectionFormat string `json:"collectionFormat,omitempty"`
  119. Default string `json:"default,omitempty"`
  120. MinItems *int `json:"minItems,omitempty"`
  121. // Or you can explicitly refer to another type. If this is defined all
  122. // other fields should be empty
  123. Schema *openapiSchemaObject `json:"schema,omitempty"`
  124. }
  125. // core part of schema, which is common to itemsObject and schemaObject.
  126. // http://swagger.io/specification/v2/#itemsObject
  127. // The OAS3 spec (https://swagger.io/specification/#schemaObject) defines the
  128. // `nullable` field as part of a Schema Object. This behavior has been
  129. // "back-ported" to OAS2 as the Specification Extension `x-nullable`, and is
  130. // supported by generation tools such as swagger-codegen and go-swagger.
  131. // For protoc-gen-openapiv3, we'd want to add `nullable` instead.
  132. type schemaCore struct {
  133. Type string `json:"type,omitempty"`
  134. Format string `json:"format,omitempty"`
  135. Ref string `json:"$ref,omitempty"`
  136. XNullable bool `json:"x-nullable,omitempty"`
  137. Example json.RawMessage `json:"example,omitempty"`
  138. Items *openapiItemsObject `json:"items,omitempty"`
  139. // If the item is an enumeration include a list of all the *NAMES* of the
  140. // enum values. I'm not sure how well this will work but assuming all enums
  141. // start from 0 index it will be great. I don't think that is a good assumption.
  142. Enum []string `json:"enum,omitempty"`
  143. Default string `json:"default,omitempty"`
  144. }
  145. func (s *schemaCore) setRefFromFQN(ref string, reg *descriptor.Registry) error {
  146. name, ok := fullyQualifiedNameToOpenAPIName(ref, reg)
  147. if !ok {
  148. return fmt.Errorf("setRefFromFQN: can't resolve OpenAPI name from '%v'", ref)
  149. }
  150. s.Ref = fmt.Sprintf("#/definitions/%s", name)
  151. return nil
  152. }
  153. type openapiItemsObject schemaCore
  154. // http://swagger.io/specification/#responsesObject
  155. type openapiResponsesObject map[string]openapiResponseObject
  156. // http://swagger.io/specification/#responseObject
  157. type openapiResponseObject struct {
  158. Description string `json:"description"`
  159. Schema openapiSchemaObject `json:"schema"`
  160. Examples map[string]interface{} `json:"examples,omitempty"`
  161. Headers openapiHeadersObject `json:"headers,omitempty"`
  162. extensions []extension
  163. }
  164. type openapiHeadersObject map[string]openapiHeaderObject
  165. // http://swagger.io/specification/#headerObject
  166. type openapiHeaderObject struct {
  167. Description string `json:"description,omitempty"`
  168. Type string `json:"type,omitempty"`
  169. Format string `json:"format,omitempty"`
  170. Default json.RawMessage `json:"default,omitempty"`
  171. Pattern string `json:"pattern,omitempty"`
  172. }
  173. type keyVal struct {
  174. Key string
  175. Value interface{}
  176. }
  177. type openapiSchemaObjectProperties []keyVal
  178. func (op openapiSchemaObjectProperties) MarshalJSON() ([]byte, error) {
  179. var buf bytes.Buffer
  180. buf.WriteString("{")
  181. for i, kv := range op {
  182. if i != 0 {
  183. buf.WriteString(",")
  184. }
  185. key, err := json.Marshal(kv.Key)
  186. if err != nil {
  187. return nil, err
  188. }
  189. buf.Write(key)
  190. buf.WriteString(":")
  191. val, err := json.Marshal(kv.Value)
  192. if err != nil {
  193. return nil, err
  194. }
  195. buf.Write(val)
  196. }
  197. buf.WriteString("}")
  198. return buf.Bytes(), nil
  199. }
  200. // http://swagger.io/specification/#schemaObject
  201. type openapiSchemaObject struct {
  202. schemaCore
  203. // Properties can be recursively defined
  204. Properties *openapiSchemaObjectProperties `json:"properties,omitempty"`
  205. AdditionalProperties *openapiSchemaObject `json:"additionalProperties,omitempty"`
  206. Description string `json:"description,omitempty"`
  207. Title string `json:"title,omitempty"`
  208. ExternalDocs *openapiExternalDocumentationObject `json:"externalDocs,omitempty"`
  209. ReadOnly bool `json:"readOnly,omitempty"`
  210. MultipleOf float64 `json:"multipleOf,omitempty"`
  211. Maximum float64 `json:"maximum,omitempty"`
  212. ExclusiveMaximum bool `json:"exclusiveMaximum,omitempty"`
  213. Minimum float64 `json:"minimum,omitempty"`
  214. ExclusiveMinimum bool `json:"exclusiveMinimum,omitempty"`
  215. MaxLength uint64 `json:"maxLength,omitempty"`
  216. MinLength uint64 `json:"minLength,omitempty"`
  217. Pattern string `json:"pattern,omitempty"`
  218. MaxItems uint64 `json:"maxItems,omitempty"`
  219. MinItems uint64 `json:"minItems,omitempty"`
  220. UniqueItems bool `json:"uniqueItems,omitempty"`
  221. MaxProperties uint64 `json:"maxProperties,omitempty"`
  222. MinProperties uint64 `json:"minProperties,omitempty"`
  223. Required []string `json:"required,omitempty"`
  224. }
  225. // http://swagger.io/specification/#definitionsObject
  226. type openapiDefinitionsObject map[string]openapiSchemaObject
  227. // Internal type mapping from FQMN to descriptor.Message. Used as a set by the
  228. // findServiceMessages function.
  229. type messageMap map[string]*descriptor.Message
  230. // Internal type mapping from FQEN to descriptor.Enum. Used as a set by the
  231. // findServiceMessages function.
  232. type enumMap map[string]*descriptor.Enum
  233. // Internal type to store used references.
  234. type refMap map[string]struct{}