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/jsonpointer"
28 // Some currently unused functions and definitions that
29 // used to be part of the expander.
31 // Moved here for the record and possible future reuse
34 idPtr, _ = jsonpointer.New("/id")
35 refPtr, _ = jsonpointer.New("/$ref")
38 func idFromNode(node interface{}) (*Ref, error) {
39 if idValue, _, err := idPtr.Get(node); err == nil {
40 if refStr, ok := idValue.(string); ok && refStr != "" {
41 idRef, err := NewRef(refStr)
51 func nextRef(startingNode interface{}, startingRef *Ref, ptr *jsonpointer.Pointer) *Ref {
52 if startingRef == nil {
64 for _, tok := range ptr.DecodedTokens() {
65 node, _, _ = jsonpointer.GetForToken(node, tok)
70 idRef, _ = idFromNode(node)
72 nw, err := ret.Inherits(*idRef)
79 refRef, _, _ := refPtr.Get(node)
82 switch value := refRef.(type) {
86 nw, err := ret.Inherits(rf)
91 if nwURL.Scheme == "file" || (nwURL.Scheme == "" && nwURL.Host == "") {
92 nwpt := filepath.ToSlash(nwURL.Path)
93 if filepath.IsAbs(nwpt) {
94 _, err := os.Stat(nwpt)
96 nwURL.Path = filepath.Join(".", nwpt)
109 // basePathFromSchemaID returns a new basePath based on an existing basePath and a schema ID
110 func basePathFromSchemaID(oldBasePath, id string) string {
111 u, err := url.Parse(oldBasePath)
115 uid, err := url.Parse(id)
120 if path.IsAbs(uid.Path) {
123 u.Path = path.Join(path.Dir(u.Path), uid.Path)
128 // type ExtraSchemaProps map[string]interface{}
130 // // JSONSchema represents a structure that is a json schema draft 04
131 // type JSONSchema struct {
136 // // MarshalJSON marshal this to JSON
137 // func (s JSONSchema) MarshalJSON() ([]byte, error) {
138 // b1, err := json.Marshal(s.SchemaProps)
142 // b2, err := s.Ref.MarshalJSON()
146 // b3, err := s.Schema.MarshalJSON()
150 // b4, err := json.Marshal(s.ExtraSchemaProps)
154 // return swag.ConcatJSON(b1, b2, b3, b4), nil
157 // // UnmarshalJSON marshal this from JSON
158 // func (s *JSONSchema) UnmarshalJSON(data []byte) error {
159 // var sch JSONSchema
160 // if err := json.Unmarshal(data, &sch.SchemaProps); err != nil {
163 // if err := json.Unmarshal(data, &sch.Ref); err != nil {
166 // if err := json.Unmarshal(data, &sch.Schema); err != nil {
169 // if err := json.Unmarshal(data, &sch.ExtraSchemaProps); err != nil {