1 // Copyright 2015 go-swagger maintainers
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
25 "github.com/go-openapi/jsonreference"
28 // Refable is a struct for things that accept a $ref property
33 // MarshalJSON marshals the ref to json
34 func (r Refable) MarshalJSON() ([]byte, error) {
35 return r.Ref.MarshalJSON()
38 // UnmarshalJSON unmarshalss the ref from json
39 func (r *Refable) UnmarshalJSON(d []byte) error {
40 return json.Unmarshal(d, &r.Ref)
43 // Ref represents a json reference that is potentially resolved
48 // RemoteURI gets the remote uri part of the ref
49 func (r *Ref) RemoteURI() string {
59 // IsValidURI returns true when the url the ref points to can be found
60 func (r *Ref) IsValidURI(basepaths ...string) bool {
71 rr, err := http.Get(v)
76 return rr.StatusCode/100 == 2
79 if !(r.HasFileScheme || r.HasFullFilePath || r.HasURLPathOnly) {
83 // check for local file
87 if len(basepaths) > 0 {
88 base = filepath.Dir(filepath.Join(basepaths...))
90 p, e := filepath.Abs(filepath.ToSlash(filepath.Join(base, pth)))
97 fi, err := os.Stat(filepath.ToSlash(pth))
105 // Inherits creates a new reference from a parent and a child
106 // If the child cannot inherit from the parent, an error is returned
107 func (r *Ref) Inherits(child Ref) (*Ref, error) {
108 ref, err := r.Ref.Inherits(child.Ref)
112 return &Ref{Ref: *ref}, nil
115 // NewRef creates a new instance of a ref object
116 // returns an error when the reference uri is an invalid uri
117 func NewRef(refURI string) (Ref, error) {
118 ref, err := jsonreference.New(refURI)
122 return Ref{Ref: ref}, nil
125 // MustCreateRef creates a ref object but panics when refURI is invalid.
126 // Use the NewRef method for a version that returns an error.
127 func MustCreateRef(refURI string) Ref {
128 return Ref{Ref: jsonreference.MustCreateRef(refURI)}
131 // MarshalJSON marshals this ref into a JSON object
132 func (r Ref) MarshalJSON() ([]byte, error) {
136 return []byte(`{"$ref":""}`), nil
138 return []byte("{}"), nil
140 v := map[string]interface{}{"$ref": str}
141 return json.Marshal(v)
144 // UnmarshalJSON unmarshals this ref from a JSON object
145 func (r *Ref) UnmarshalJSON(d []byte) error {
146 var v map[string]interface{}
147 if err := json.Unmarshal(d, &v); err != nil {
153 // GobEncode provides a safe gob encoder for Ref
154 func (r Ref) GobEncode() ([]byte, error) {
156 raw, err := r.MarshalJSON()
160 err = gob.NewEncoder(&b).Encode(raw)
161 return b.Bytes(), err
164 // GobDecode provides a safe gob decoder for Ref
165 func (r *Ref) GobDecode(b []byte) error {
167 buf := bytes.NewBuffer(b)
168 err := gob.NewDecoder(buf).Decode(&raw)
172 return json.Unmarshal(raw, r)
175 func (r *Ref) fromMap(v map[string]interface{}) error {
180 if vv, ok := v["$ref"]; ok {
181 if str, ok := vv.(string); ok {
182 ref, err := jsonreference.New(str)