ref.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 spec
  15. import (
  16. "bytes"
  17. "encoding/gob"
  18. "encoding/json"
  19. "net/http"
  20. "os"
  21. "path/filepath"
  22. "github.com/go-openapi/jsonreference"
  23. )
  24. // Refable is a struct for things that accept a $ref property
  25. type Refable struct {
  26. Ref Ref
  27. }
  28. // MarshalJSON marshals the ref to json
  29. func (r Refable) MarshalJSON() ([]byte, error) {
  30. return r.Ref.MarshalJSON()
  31. }
  32. // UnmarshalJSON unmarshalss the ref from json
  33. func (r *Refable) UnmarshalJSON(d []byte) error {
  34. return json.Unmarshal(d, &r.Ref)
  35. }
  36. // Ref represents a json reference that is potentially resolved
  37. type Ref struct {
  38. jsonreference.Ref
  39. }
  40. // RemoteURI gets the remote uri part of the ref
  41. func (r *Ref) RemoteURI() string {
  42. if r.String() == "" {
  43. return r.String()
  44. }
  45. u := *r.GetURL()
  46. u.Fragment = ""
  47. return u.String()
  48. }
  49. // IsValidURI returns true when the url the ref points to can be found
  50. func (r *Ref) IsValidURI(basepaths ...string) bool {
  51. if r.String() == "" {
  52. return true
  53. }
  54. v := r.RemoteURI()
  55. if v == "" {
  56. return true
  57. }
  58. if r.HasFullURL {
  59. //#nosec
  60. rr, err := http.Get(v)
  61. if err != nil {
  62. return false
  63. }
  64. defer rr.Body.Close()
  65. return rr.StatusCode/100 == 2
  66. }
  67. if !(r.HasFileScheme || r.HasFullFilePath || r.HasURLPathOnly) {
  68. return false
  69. }
  70. // check for local file
  71. pth := v
  72. if r.HasURLPathOnly {
  73. base := "."
  74. if len(basepaths) > 0 {
  75. base = filepath.Dir(filepath.Join(basepaths...))
  76. }
  77. p, e := filepath.Abs(filepath.ToSlash(filepath.Join(base, pth)))
  78. if e != nil {
  79. return false
  80. }
  81. pth = p
  82. }
  83. fi, err := os.Stat(filepath.ToSlash(pth))
  84. if err != nil {
  85. return false
  86. }
  87. return !fi.IsDir()
  88. }
  89. // Inherits creates a new reference from a parent and a child
  90. // If the child cannot inherit from the parent, an error is returned
  91. func (r *Ref) Inherits(child Ref) (*Ref, error) {
  92. ref, err := r.Ref.Inherits(child.Ref)
  93. if err != nil {
  94. return nil, err
  95. }
  96. return &Ref{Ref: *ref}, nil
  97. }
  98. // NewRef creates a new instance of a ref object
  99. // returns an error when the reference uri is an invalid uri
  100. func NewRef(refURI string) (Ref, error) {
  101. ref, err := jsonreference.New(refURI)
  102. if err != nil {
  103. return Ref{}, err
  104. }
  105. return Ref{Ref: ref}, nil
  106. }
  107. // MustCreateRef creates a ref object but panics when refURI is invalid.
  108. // Use the NewRef method for a version that returns an error.
  109. func MustCreateRef(refURI string) Ref {
  110. return Ref{Ref: jsonreference.MustCreateRef(refURI)}
  111. }
  112. // MarshalJSON marshals this ref into a JSON object
  113. func (r Ref) MarshalJSON() ([]byte, error) {
  114. str := r.String()
  115. if str == "" {
  116. if r.IsRoot() {
  117. return []byte(`{"$ref":""}`), nil
  118. }
  119. return []byte("{}"), nil
  120. }
  121. v := map[string]interface{}{"$ref": str}
  122. return json.Marshal(v)
  123. }
  124. // UnmarshalJSON unmarshals this ref from a JSON object
  125. func (r *Ref) UnmarshalJSON(d []byte) error {
  126. var v map[string]interface{}
  127. if err := json.Unmarshal(d, &v); err != nil {
  128. return err
  129. }
  130. return r.fromMap(v)
  131. }
  132. // GobEncode provides a safe gob encoder for Ref
  133. func (r Ref) GobEncode() ([]byte, error) {
  134. var b bytes.Buffer
  135. raw, err := r.MarshalJSON()
  136. if err != nil {
  137. return nil, err
  138. }
  139. err = gob.NewEncoder(&b).Encode(raw)
  140. return b.Bytes(), err
  141. }
  142. // GobDecode provides a safe gob decoder for Ref
  143. func (r *Ref) GobDecode(b []byte) error {
  144. var raw []byte
  145. buf := bytes.NewBuffer(b)
  146. err := gob.NewDecoder(buf).Decode(&raw)
  147. if err != nil {
  148. return err
  149. }
  150. return json.Unmarshal(raw, r)
  151. }
  152. func (r *Ref) fromMap(v map[string]interface{}) error {
  153. if v == nil {
  154. return nil
  155. }
  156. if vv, ok := v["$ref"]; ok {
  157. if str, ok := vv.(string); ok {
  158. ref, err := jsonreference.New(str)
  159. if err != nil {
  160. return err
  161. }
  162. *r = Ref{Ref: ref}
  163. }
  164. }
  165. return nil
  166. }