spec.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. // Copyright 2015 go-swagger maintainers
  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. package loads
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "fmt"
  19. "net/url"
  20. "github.com/go-openapi/analysis"
  21. "github.com/go-openapi/spec"
  22. "github.com/go-openapi/swag"
  23. )
  24. // JSONDoc loads a json document from either a file or a remote url
  25. func JSONDoc(path string) (json.RawMessage, error) {
  26. data, err := swag.LoadFromFileOrHTTP(path)
  27. if err != nil {
  28. return nil, err
  29. }
  30. return json.RawMessage(data), nil
  31. }
  32. // DocLoader represents a doc loader type
  33. type DocLoader func(string) (json.RawMessage, error)
  34. // DocMatcher represents a predicate to check if a loader matches
  35. type DocMatcher func(string) bool
  36. var (
  37. loaders *loader
  38. defaultLoader *loader
  39. )
  40. func init() {
  41. defaultLoader = &loader{Match: func(_ string) bool { return true }, Fn: JSONDoc}
  42. loaders = defaultLoader
  43. spec.PathLoader = loaders.Fn
  44. AddLoader(swag.YAMLMatcher, swag.YAMLDoc)
  45. }
  46. // AddLoader for a document
  47. func AddLoader(predicate DocMatcher, load DocLoader) {
  48. prev := loaders
  49. loaders = &loader{
  50. Match: predicate,
  51. Fn: load,
  52. Next: prev,
  53. }
  54. spec.PathLoader = loaders.Fn
  55. }
  56. type loader struct {
  57. Fn DocLoader
  58. Match DocMatcher
  59. Next *loader
  60. }
  61. // JSONSpec loads a spec from a json document
  62. func JSONSpec(path string) (*Document, error) {
  63. data, err := JSONDoc(path)
  64. if err != nil {
  65. return nil, err
  66. }
  67. // convert to json
  68. return Analyzed(json.RawMessage(data), "")
  69. }
  70. // Document represents a swagger spec document
  71. type Document struct {
  72. // specAnalyzer
  73. Analyzer *analysis.Spec
  74. spec *spec.Swagger
  75. specFilePath string
  76. origSpec *spec.Swagger
  77. schema *spec.Schema
  78. raw json.RawMessage
  79. }
  80. // Embedded returns a Document based on embedded specs. No analysis is required
  81. func Embedded(orig, flat json.RawMessage) (*Document, error) {
  82. var origSpec, flatSpec spec.Swagger
  83. if err := json.Unmarshal(orig, &origSpec); err != nil {
  84. return nil, err
  85. }
  86. if err := json.Unmarshal(flat, &flatSpec); err != nil {
  87. return nil, err
  88. }
  89. return &Document{
  90. raw: orig,
  91. origSpec: &origSpec,
  92. spec: &flatSpec,
  93. }, nil
  94. }
  95. // Spec loads a new spec document
  96. func Spec(path string) (*Document, error) {
  97. specURL, err := url.Parse(path)
  98. if err != nil {
  99. return nil, err
  100. }
  101. var lastErr error
  102. for l := loaders.Next; l != nil; l = l.Next {
  103. if loaders.Match(specURL.Path) {
  104. b, err2 := loaders.Fn(path)
  105. if err2 != nil {
  106. lastErr = err2
  107. continue
  108. }
  109. doc, err := Analyzed(b, "")
  110. if err != nil {
  111. return nil, err
  112. }
  113. if doc != nil {
  114. doc.specFilePath = path
  115. }
  116. return doc, nil
  117. }
  118. }
  119. if lastErr != nil {
  120. return nil, lastErr
  121. }
  122. b, err := defaultLoader.Fn(path)
  123. if err != nil {
  124. return nil, err
  125. }
  126. document, err := Analyzed(b, "")
  127. if document != nil {
  128. document.specFilePath = path
  129. }
  130. return document, err
  131. }
  132. // Analyzed creates a new analyzed spec document
  133. func Analyzed(data json.RawMessage, version string) (*Document, error) {
  134. if version == "" {
  135. version = "2.0"
  136. }
  137. if version != "2.0" {
  138. return nil, fmt.Errorf("spec version %q is not supported", version)
  139. }
  140. raw := data
  141. trimmed := bytes.TrimSpace(data)
  142. if len(trimmed) > 0 {
  143. if trimmed[0] != '{' && trimmed[0] != '[' {
  144. yml, err := swag.BytesToYAMLDoc(trimmed)
  145. if err != nil {
  146. return nil, fmt.Errorf("analyzed: %v", err)
  147. }
  148. d, err := swag.YAMLToJSON(yml)
  149. if err != nil {
  150. return nil, fmt.Errorf("analyzed: %v", err)
  151. }
  152. raw = d
  153. }
  154. }
  155. swspec := new(spec.Swagger)
  156. if err := json.Unmarshal(raw, swspec); err != nil {
  157. return nil, err
  158. }
  159. origsqspec := new(spec.Swagger)
  160. if err := json.Unmarshal(raw, origsqspec); err != nil {
  161. return nil, err
  162. }
  163. d := &Document{
  164. Analyzer: analysis.New(swspec),
  165. schema: spec.MustLoadSwagger20Schema(),
  166. spec: swspec,
  167. raw: raw,
  168. origSpec: origsqspec,
  169. }
  170. return d, nil
  171. }
  172. // Expanded expands the ref fields in the spec document and returns a new spec document
  173. func (d *Document) Expanded(options ...*spec.ExpandOptions) (*Document, error) {
  174. swspec := new(spec.Swagger)
  175. if err := json.Unmarshal(d.raw, swspec); err != nil {
  176. return nil, err
  177. }
  178. var expandOptions *spec.ExpandOptions
  179. if len(options) > 0 {
  180. expandOptions = options[0]
  181. } else {
  182. expandOptions = &spec.ExpandOptions{
  183. RelativeBase: d.specFilePath,
  184. }
  185. }
  186. if err := spec.ExpandSpec(swspec, expandOptions); err != nil {
  187. return nil, err
  188. }
  189. dd := &Document{
  190. Analyzer: analysis.New(swspec),
  191. spec: swspec,
  192. specFilePath: d.specFilePath,
  193. schema: spec.MustLoadSwagger20Schema(),
  194. raw: d.raw,
  195. origSpec: d.origSpec,
  196. }
  197. return dd, nil
  198. }
  199. // BasePath the base path for this spec
  200. func (d *Document) BasePath() string {
  201. return d.spec.BasePath
  202. }
  203. // Version returns the version of this spec
  204. func (d *Document) Version() string {
  205. return d.spec.Swagger
  206. }
  207. // Schema returns the swagger 2.0 schema
  208. func (d *Document) Schema() *spec.Schema {
  209. return d.schema
  210. }
  211. // Spec returns the swagger spec object model
  212. func (d *Document) Spec() *spec.Swagger {
  213. return d.spec
  214. }
  215. // Host returns the host for the API
  216. func (d *Document) Host() string {
  217. return d.spec.Host
  218. }
  219. // Raw returns the raw swagger spec as json bytes
  220. func (d *Document) Raw() json.RawMessage {
  221. return d.raw
  222. }
  223. func (d *Document) OrigSpec() *spec.Swagger {
  224. return d.origSpec
  225. }
  226. // ResetDefinitions gives a shallow copy with the models reset
  227. func (d *Document) ResetDefinitions() *Document {
  228. defs := make(map[string]spec.Schema, len(d.origSpec.Definitions))
  229. for k, v := range d.origSpec.Definitions {
  230. defs[k] = v
  231. }
  232. d.spec.Definitions = defs
  233. return d
  234. }
  235. // Pristine creates a new pristine document instance based on the input data
  236. func (d *Document) Pristine() *Document {
  237. dd, _ := Analyzed(d.Raw(), d.Version())
  238. return dd
  239. }
  240. // SpecFilePath returns the file path of the spec if one is defined
  241. func (d *Document) SpecFilePath() string {
  242. return d.specFilePath
  243. }