Skip to content

Commit

Permalink
* simplified return values from load()
Browse files Browse the repository at this point in the history
Signed-off-by: Frederic BIDON <[email protected]>
  • Loading branch information
fredbi committed Jan 4, 2021
1 parent 5404d3e commit 55dd5ff
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 18 deletions.
4 changes: 3 additions & 1 deletion circular_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,8 @@ func TestExpandCircular_RemoteCircularID(t *testing.T) {
})

t.Run("withID", func(t *testing.T) {
Debug = true
t.SkipNow() // TODO(fredbi)
const fixturePath = "fixtures/more_circulars/with-id.json"
jazon := expandThisOrDieTrying(t, fixturePath)
t.Log(jazon)
Expand Down Expand Up @@ -299,5 +301,5 @@ func TestDocRef(t *testing.T) {
require.NoError(t, ExpandSchema(ref, &schema, nil))
jazon, err = json.MarshalIndent(ref, "", " ")
require.NoError(t, err)
t.Log(string(jazon))
assertRefInJSONRegexp(t, string(jazon), `(^#$)|(^$)`)
}
5 changes: 3 additions & 2 deletions expander_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -802,8 +802,9 @@ func TestExpandRemoteRef_WithNestedResolutionContext(t *testing.T) {
}

/*
This next test will have to wait until we do full $ID analysis for every subschema on every file that is referenced
// For now, TestExpandRemoteRef_WithNestedResolutionContext replaces this next test
This next test will have to wait until we do full $ID analysis for every subschema on every file that is referenced
For now, TestExpandRemoteRef_WithNestedResolutionContext replaces this next test
func TestExpandRemoteRef_WithNestedResolutionContext_WithParentID(t *testing.T) {
server := resolutionContextServer()
defer server.Close()
Expand Down
28 changes: 13 additions & 15 deletions schema_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ type resolverContext struct {
rootID string
// ids holds all inherited $id for the current schema.
// This is used to try alternate paths to resolve $ref.
//ids map[string]bool
// ids map[string]bool
}

func newResolverContext(expandOptions *ExpandOptions) *resolverContext {
Expand Down Expand Up @@ -185,14 +185,12 @@ func (r *schemaLoader) resolveRef(ref *Ref, target interface{}, basePath, pointe
if (ref.IsRoot() || ref.HasFragmentOnly) && root == nil {
if basePath != "" {
debugLog("fetch from basePath: %s", basePath)
if baseRef, erb := NewRef(basePath); erb == nil {
root, _, _, _ = r.load(baseRef.GetURL())
}
baseRef := MustCreateRef(basePath)
root, _ = r.load(baseRef.GetURL())
} else {
debugLog("fetch from root in RelativeBase: %s", r.options.RelativeBase)
if baseRef, erb := NewRef(r.options.RelativeBase); erb == nil {
root, _, _, _ = r.load(baseRef.GetURL())
}
baseRef := MustCreateRef(r.options.RelativeBase)
root, _ = r.load(baseRef.GetURL())
}
}

Expand All @@ -202,7 +200,7 @@ func (r *schemaLoader) resolveRef(ref *Ref, target interface{}, basePath, pointe
if (ref.IsRoot() || ref.HasFragmentOnly) && root != nil {
data = root
} else {
data, _, _, err = r.load(baseRef.GetURL())
data, err = r.load(baseRef.GetURL())
if err != nil {
return err
}
Expand Down Expand Up @@ -244,7 +242,7 @@ func (r *schemaLoader) resolveRef(ref *Ref, target interface{}, basePath, pointe
return swag.DynamicJSONToStruct(res, target)
}

func (r *schemaLoader) load(refURL *url.URL) (interface{}, url.URL, bool, error) { // TODO: simplify return values
func (r *schemaLoader) load(refURL *url.URL) (interface{}, error) {
debugLog("loading schema from url: %s", refURL)
toFetch := *refURL
toFetch.Fragment = ""
Expand All @@ -254,7 +252,7 @@ func (r *schemaLoader) load(refURL *url.URL) (interface{}, url.URL, bool, error)
if pth == rootBase {
pth, err = absPath(rootBase)
if err != nil {
return nil, url.URL{}, false, err
return nil, err
}
}
normalized := normalizeAbsPath(pth)
Expand All @@ -263,20 +261,20 @@ func (r *schemaLoader) load(refURL *url.URL) (interface{}, url.URL, bool, error)
if !fromCache {
b, err := r.context.loadDoc(normalized)
if err != nil {
return nil, url.URL{}, false, fmt.Errorf("%s [%s]: %w", pth, normalized, err)
return nil, fmt.Errorf("%s [normalized: %s]: %w", pth, normalized, err)
}

var doc interface{}
if err := json.Unmarshal(b, &doc); err != nil {
return nil, url.URL{}, false, err
return nil, err
}

r.cache.Set(normalized, doc)

return doc, toFetch, fromCache, nil
return doc, nil
}

return data, toFetch, fromCache, nil
return data, nil
}

// isCircular detects cycles in sequences of $ref.
Expand Down Expand Up @@ -408,7 +406,7 @@ func (r *schemaLoader) setSchemaID(target interface{}, id, basePath, pointer str
}

// remembers the schema id's encountered
//r.context.ids[id] = true
// r.context.ids[id] = true

// updates the current base path
// * important: ID can be a relative path
Expand Down

0 comments on commit 55dd5ff

Please sign in to comment.