Skip to content

Commit

Permalink
refactored expander and ref resolver for readability (go-openapi#135)
Browse files Browse the repository at this point in the history
[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 <[email protected]>
  • Loading branch information
fredbi authored Dec 29, 2020
1 parent 10b968f commit 9dac817
Show file tree
Hide file tree
Showing 12 changed files with 1,243 additions and 1,116 deletions.
31 changes: 31 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package spec

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestDefaultResolutionCache(t *testing.T) {
jsonSchema := MustLoadJSONSchemaDraft04()
swaggerSchema := MustLoadSwagger20Schema()

cache := defaultResolutionCache()

sch, ok := cache.Get("not there")
assert.False(t, ok)
assert.Nil(t, sch)

sch, ok = cache.Get("http://swagger.io/v2/schema.json")
assert.True(t, ok)
assert.Equal(t, swaggerSchema, sch)

sch, ok = cache.Get("http://json-schema.org/draft-04/schema")
assert.True(t, ok)
assert.Equal(t, jsonSchema, sch)

cache.Set("something", "here")
sch, ok = cache.Get("something")
assert.True(t, ok)
assert.Equal(t, "here", sch)
}
18 changes: 18 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package spec

import "errors"

var (
// ErrUnknownTypeForReference indicates that a resolved reference was found in an unsupported container type
ErrUnknownTypeForReference = errors.New("unknown type for the resolved reference")

// ErrResolveRefNeedsAPointer indicates that a $ref target must be a valid JSON pointer
ErrResolveRefNeedsAPointer = errors.New("resolve ref: target needs to be a pointer")

// ErrDerefUnsupportedType indicates that a resolved reference was found in an unsupported container type.
// At the moment, $ref are supported only inside: schemas, parameters, responses, path items
ErrDerefUnsupportedType = errors.New("deref: unsupported type")

// ErrExpandUnsupportedType indicates that $ref expansion is attempted on some invalid type
ErrExpandUnsupportedType = errors.New("expand: unsupported type. Input should be of type *Parameter or *Response")
)
Loading

0 comments on commit 9dac817

Please sign in to comment.