| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
package spec |
| 16 |
|
| 17 |
import ( |
| 18 |
"encoding/json" |
| 19 |
"net/http" |
| 20 |
"os" |
| 21 |
"path/filepath" |
| 22 |
|
| 23 |
"github.com/go-openapi/jsonreference" |
| 24 |
|
| 25 |
"k8s.io/kube-openapi/pkg/internal" |
| 26 |
) |
| 27 |
|
| 28 |
|
| 29 |
type Refable struct { |
| 30 |
Ref Ref |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
func (r Refable) MarshalJSON() ([]byte, error) { |
| 35 |
return r.Ref.MarshalJSON() |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
func (r *Refable) UnmarshalJSON(d []byte) error { |
| 40 |
return json.Unmarshal(d, &r.Ref) |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
type Ref struct { |
| 45 |
jsonreference.Ref |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
func (r *Ref) RemoteURI() string { |
| 50 |
if r.String() == "" { |
| 51 |
return r.String() |
| 52 |
} |
| 53 |
|
| 54 |
u := *r.GetURL() |
| 55 |
u.Fragment = "" |
| 56 |
return u.String() |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
func (r *Ref) IsValidURI(basepaths ...string) bool { |
| 61 |
if r.String() == "" { |
| 62 |
return true |
| 63 |
} |
| 64 |
|
| 65 |
v := r.RemoteURI() |
| 66 |
if v == "" { |
| 67 |
return true |
| 68 |
} |
| 69 |
|
| 70 |
if r.HasFullURL { |
| 71 |
rr, err := http.Get(v) |
| 72 |
if err != nil { |
| 73 |
return false |
| 74 |
} |
| 75 |
|
| 76 |
return rr.StatusCode/100 == 2 |
| 77 |
} |
| 78 |
|
| 79 |
if !(r.HasFileScheme || r.HasFullFilePath || r.HasURLPathOnly) { |
| 80 |
return false |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
pth := v |
| 85 |
if r.HasURLPathOnly { |
| 86 |
base := "." |
| 87 |
if len(basepaths) > 0 { |
| 88 |
base = filepath.Dir(filepath.Join(basepaths...)) |
| 89 |
} |
| 90 |
p, e := filepath.Abs(filepath.ToSlash(filepath.Join(base, pth))) |
| 91 |
if e != nil { |
| 92 |
return false |
| 93 |
} |
| 94 |
pth = p |
| 95 |
} |
| 96 |
|
| 97 |
fi, err := os.Stat(filepath.ToSlash(pth)) |
| 98 |
if err != nil { |
| 99 |
return false |
| 100 |
} |
| 101 |
|
| 102 |
return !fi.IsDir() |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
func (r *Ref) Inherits(child Ref) (*Ref, error) { |
| 108 |
ref, err := r.Ref.Inherits(child.Ref) |
| 109 |
if err != nil { |
| 110 |
return nil, err |
| 111 |
} |
| 112 |
return &Ref{Ref: *ref}, nil |
| 113 |
} |
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
func NewRef(refURI string) (Ref, error) { |
| 118 |
ref, err := jsonreference.New(refURI) |
| 119 |
if err != nil { |
| 120 |
return Ref{}, err |
| 121 |
} |
| 122 |
return Ref{Ref: ref}, nil |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
func MustCreateRef(refURI string) Ref { |
| 128 |
return Ref{Ref: jsonreference.MustCreateRef(refURI)} |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (r Ref) MarshalJSON() ([]byte, error) { |
| 133 |
str := r.String() |
| 134 |
if str == "" { |
| 135 |
if r.IsRoot() { |
| 136 |
return []byte(`{"$ref":""}`), nil |
| 137 |
} |
| 138 |
return []byte("{}"), nil |
| 139 |
} |
| 140 |
v := map[string]interface{}{"$ref": str} |
| 141 |
return json.Marshal(v) |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
func (r *Ref) UnmarshalJSON(d []byte) error { |
| 146 |
var v map[string]interface{} |
| 147 |
if err := json.Unmarshal(d, &v); err != nil { |
| 148 |
return err |
| 149 |
} |
| 150 |
return r.fromMap(v) |
| 151 |
} |
| 152 |
|
| 153 |
func (r *Ref) fromMap(v map[string]interface{}) error { |
| 154 |
return internal.JSONRefFromMap(&r.Ref, v) |
| 155 |
} |