Skip to content

Commit 9dac817

Browse files
authored
refactored expander and ref resolver for readability (#135)
[NO CHANGE IN FUNCTIONALITY] * refactored associated units tests for readability * reshuffled resolver funcs and associated tests to a dedicated file * removed panic() in ResolveRef (now return error) [used by go-swagger] Signed-off-by: Frederic BIDON <fredbi@yahoo.com>
1 parent 10b968f commit 9dac817

12 files changed

+1243
-1116
lines changed

cache_test.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package spec
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestDefaultResolutionCache(t *testing.T) {
10+
jsonSchema := MustLoadJSONSchemaDraft04()
11+
swaggerSchema := MustLoadSwagger20Schema()
12+
13+
cache := defaultResolutionCache()
14+
15+
sch, ok := cache.Get("not there")
16+
assert.False(t, ok)
17+
assert.Nil(t, sch)
18+
19+
sch, ok = cache.Get("http://swagger.io/v2/schema.json")
20+
assert.True(t, ok)
21+
assert.Equal(t, swaggerSchema, sch)
22+
23+
sch, ok = cache.Get("http://json-schema.org/draft-04/schema")
24+
assert.True(t, ok)
25+
assert.Equal(t, jsonSchema, sch)
26+
27+
cache.Set("something", "here")
28+
sch, ok = cache.Get("something")
29+
assert.True(t, ok)
30+
assert.Equal(t, "here", sch)
31+
}

errors.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package spec
2+
3+
import "errors"
4+
5+
var (
6+
// ErrUnknownTypeForReference indicates that a resolved reference was found in an unsupported container type
7+
ErrUnknownTypeForReference = errors.New("unknown type for the resolved reference")
8+
9+
// ErrResolveRefNeedsAPointer indicates that a $ref target must be a valid JSON pointer
10+
ErrResolveRefNeedsAPointer = errors.New("resolve ref: target needs to be a pointer")
11+
12+
// ErrDerefUnsupportedType indicates that a resolved reference was found in an unsupported container type.
13+
// At the moment, $ref are supported only inside: schemas, parameters, responses, path items
14+
ErrDerefUnsupportedType = errors.New("deref: unsupported type")
15+
16+
// ErrExpandUnsupportedType indicates that $ref expansion is attempted on some invalid type
17+
ErrExpandUnsupportedType = errors.New("expand: unsupported type. Input should be of type *Parameter or *Response")
18+
)

0 commit comments

Comments
 (0)