Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 13 additions & 18 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
linters-settings:
govet:
check-shadowing: true
golint:
min-confidence: 0
gocyclo:
min-complexity: 45
maligned:
suggest-new: true
dupl:
threshold: 200
goconst:
Expand All @@ -16,7 +10,7 @@ linters-settings:
linters:
enable-all: true
disable:
- maligned
- recvcheck
- unparam
- lll
- gochecknoinits
Expand All @@ -29,17 +23,13 @@ linters:
- wrapcheck
- testpackage
- nlreturn
- gomnd
- exhaustivestruct
- goerr113
- errorlint
- nestif
- godot
- gofumpt
- paralleltest
- tparallel
- thelper
- ifshort
- exhaustruct
- varnamelen
- gci
Expand All @@ -52,10 +42,15 @@ linters:
- forcetypeassert
- cyclop
# deprecated linters
- deadcode
- interfacer
- scopelint
- varcheck
- structcheck
- golint
- nosnakecase
#- deadcode
#- interfacer
#- scopelint
#- varcheck
#- structcheck
#- golint
#- nosnakecase
#- maligned
#- goerr113
#- ifshort
#- gomnd
#- exhaustivestruct
2 changes: 1 addition & 1 deletion contact_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ var contactInfo = ContactInfo{ContactInfoProps: ContactInfoProps{
func TestIntegrationContactInfo(t *testing.T) {
b, err := json.MarshalIndent(contactInfo, "", "\t")
require.NoError(t, err)
assert.Equal(t, contactInfoJSON, string(b))
assert.JSONEq(t, contactInfoJSON, string(b))

actual := ContactInfo{}
err = json.Unmarshal([]byte(contactInfoJSON), &actual)
Expand Down
3 changes: 2 additions & 1 deletion debug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ var (
)

func TestDebug(t *testing.T) {
tmpFile, _ := os.CreateTemp("", "debug-test")
// usetesting linter disabled until https://github.com/golang/go/issues/71544 is fixed for windows
tmpFile, _ := os.CreateTemp("", "debug-test") //nolint:usetesting
tmpName := tmpFile.Name()
defer func() {
Debug = false
Expand Down
3 changes: 3 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@ var (

// 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")

// ErrSpec is an error raised by the spec package
ErrSpec = errors.New("spec error")
)
10 changes: 6 additions & 4 deletions expander.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import (
"fmt"
)

const smallPrealloc = 10

// ExpandOptions provides options for the spec expander.
//
// RelativeBase is the path to the root document. This can be a remote URL or a path to a local file.
Expand Down Expand Up @@ -56,7 +58,7 @@ func ExpandSpec(spec *Swagger, options *ExpandOptions) error {

if !options.SkipSchemas {
for key, definition := range spec.Definitions {
parentRefs := make([]string, 0, 10)
parentRefs := make([]string, 0, smallPrealloc)
parentRefs = append(parentRefs, "#/definitions/"+key)

def, err := expandSchema(definition, parentRefs, resolver, specBasePath)
Expand Down Expand Up @@ -160,7 +162,7 @@ func ExpandSchemaWithBasePath(schema *Schema, cache ResolutionCache, opts *Expan

resolver := defaultSchemaLoader(nil, opts, cache, nil)

parentRefs := make([]string, 0, 10)
parentRefs := make([]string, 0, smallPrealloc)
s, err := expandSchema(*schema, parentRefs, resolver, opts.RelativeBase)
if err != nil {
return err
Expand Down Expand Up @@ -386,7 +388,7 @@ func expandPathItem(pathItem *PathItem, resolver *schemaLoader, basePath string)
return nil
}

parentRefs := make([]string, 0, 10)
parentRefs := make([]string, 0, smallPrealloc)
if err := resolver.deref(pathItem, parentRefs, basePath); resolver.shouldStopOnError(err) {
return err
}
Expand Down Expand Up @@ -546,7 +548,7 @@ func expandParameterOrResponse(input interface{}, resolver *schemaLoader, basePa
return nil
}

parentRefs := make([]string, 0, 10)
parentRefs := make([]string, 0, smallPrealloc)
if ref != nil {
// dereference this $ref
if err = resolver.deref(input, parentRefs, basePath); resolver.shouldStopOnError(err) {
Expand Down
16 changes: 8 additions & 8 deletions header.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,14 @@ func (h *Header) WithDefault(defaultValue interface{}) *Header {
}

// WithMaxLength sets a max length value
func (h *Header) WithMaxLength(max int64) *Header {
h.MaxLength = &max
func (h *Header) WithMaxLength(maximum int64) *Header {
h.MaxLength = &maximum
return h
}

// WithMinLength sets a min length value
func (h *Header) WithMinLength(min int64) *Header {
h.MinLength = &min
func (h *Header) WithMinLength(minimum int64) *Header {
h.MinLength = &minimum
return h
}

Expand All @@ -98,15 +98,15 @@ func (h *Header) WithMultipleOf(number float64) *Header {
}

// WithMaximum sets a maximum number value
func (h *Header) WithMaximum(max float64, exclusive bool) *Header {
h.Maximum = &max
func (h *Header) WithMaximum(maximum float64, exclusive bool) *Header {
h.Maximum = &maximum
h.ExclusiveMaximum = exclusive
return h
}

// WithMinimum sets a minimum number value
func (h *Header) WithMinimum(min float64, exclusive bool) *Header {
h.Minimum = &min
func (h *Header) WithMinimum(minimum float64, exclusive bool) *Header {
h.Minimum = &minimum
h.ExclusiveMinimum = exclusive
return h
}
Expand Down
8 changes: 4 additions & 4 deletions header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,15 +118,15 @@ func TestJSONLookupHeader(t *testing.T) {
require.Error(t, err)
require.Nil(t, res)

var max *float64
var maximum *float64
res, err = header.JSONLookup("maximum")
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, max, res)
require.IsType(t, maximum, res)

max, ok = res.(*float64)
maximum, ok = res.(*float64)
require.True(t, ok)
assert.InDelta(t, float64(100), *max, epsilon)
assert.InDelta(t, float64(100), *maximum, epsilon)
}

func TestResponseHeaueder(t *testing.T) {
Expand Down
34 changes: 18 additions & 16 deletions info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const infoJSON = `{
"x-framework": "go-swagger"
}`

var info = Info{
var testInfo = Info{
InfoProps: InfoProps{
Version: "1.0.9-abcd",
Title: "Swagger Sample API",
Expand All @@ -56,21 +56,23 @@ var info = Info{
VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-framework": "go-swagger"}},
}

func TestIntegrationInfo_Serialize(t *testing.T) {
b, err := json.MarshalIndent(info, "", "\t")
require.NoError(t, err)
assert.Equal(t, infoJSON, string(b))
}
func TestInfo(t *testing.T) {
t.Run("should marshal Info", func(t *testing.T) {
b, err := json.MarshalIndent(testInfo, "", "\t")
require.NoError(t, err)
assert.JSONEq(t, infoJSON, string(b))
})

func TestIntegrationInfo_Deserialize(t *testing.T) {
actual := Info{}
require.NoError(t, json.Unmarshal([]byte(infoJSON), &actual))
assert.EqualValues(t, info, actual)
}
t.Run("should unmarshal Info", func(t *testing.T) {
actual := Info{}
require.NoError(t, json.Unmarshal([]byte(infoJSON), &actual))
assert.EqualValues(t, testInfo, actual)
})

func TestInfoGobEncoding(t *testing.T) {
var src, dst Info
require.NoError(t, json.Unmarshal([]byte(infoJSON), &src))
assert.EqualValues(t, src, info)
doTestAnyGobEncoding(t, &src, &dst)
t.Run("should GobEncode Info", func(t *testing.T) {
var src, dst Info
require.NoError(t, json.Unmarshal([]byte(infoJSON), &src))
assert.EqualValues(t, src, testInfo)
doTestAnyGobEncoding(t, &src, &dst)
})
}
16 changes: 8 additions & 8 deletions items.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,14 +97,14 @@ func (i *Items) WithDefault(defaultValue interface{}) *Items {
}

// WithMaxLength sets a max length value
func (i *Items) WithMaxLength(max int64) *Items {
i.MaxLength = &max
func (i *Items) WithMaxLength(maximum int64) *Items {
i.MaxLength = &maximum
return i
}

// WithMinLength sets a min length value
func (i *Items) WithMinLength(min int64) *Items {
i.MinLength = &min
func (i *Items) WithMinLength(minimum int64) *Items {
i.MinLength = &minimum
return i
}

Expand All @@ -121,15 +121,15 @@ func (i *Items) WithMultipleOf(number float64) *Items {
}

// WithMaximum sets a maximum number value
func (i *Items) WithMaximum(max float64, exclusive bool) *Items {
i.Maximum = &max
func (i *Items) WithMaximum(maximum float64, exclusive bool) *Items {
i.Maximum = &maximum
i.ExclusiveMaximum = exclusive
return i
}

// WithMinimum sets a minimum number value
func (i *Items) WithMinimum(min float64, exclusive bool) *Items {
i.Minimum = &min
func (i *Items) WithMinimum(minimum float64, exclusive bool) *Items {
i.Minimum = &minimum
i.ExclusiveMinimum = exclusive
return i
}
Expand Down
84 changes: 46 additions & 38 deletions items_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
"github.com/stretchr/testify/require"
)

var items = Items{
var testItems = Items{
Refable: Refable{Ref: MustCreateRef("Dog")},
CommonValidations: CommonValidations{
Maximum: float64Ptr(100),
Expand Down Expand Up @@ -76,17 +76,17 @@ const itemsJSON = `{
func TestIntegrationItems(t *testing.T) {
var actual Items
require.NoError(t, json.Unmarshal([]byte(itemsJSON), &actual))
assert.EqualValues(t, actual, items)
assert.EqualValues(t, actual, testItems)

assertParsesJSON(t, itemsJSON, items)
assertParsesJSON(t, itemsJSON, testItems)
}

func TestTypeNameItems(t *testing.T) {
var nilItems Items
assert.Equal(t, "", nilItems.TypeName())

assert.Equal(t, "date", items.TypeName())
assert.Equal(t, "", items.ItemsTypeName())
assert.Equal(t, "date", testItems.TypeName())
assert.Equal(t, "", testItems.ItemsTypeName())

nested := Items{
SimpleSchema: SimpleSchema{
Expand Down Expand Up @@ -151,39 +151,47 @@ func TestItemsBuilder(t *testing.T) {
}

func TestJSONLookupItems(t *testing.T) {
res, err := items.JSONLookup("$ref")
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, &Ref{}, res)

var ok bool
ref, ok := res.(*Ref)
require.True(t, ok)
assert.EqualValues(t, MustCreateRef("Dog"), *ref)

var max *float64
res, err = items.JSONLookup("maximum")
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, max, res)

max, ok = res.(*float64)
require.True(t, ok)
assert.InDelta(t, float64(100), *max, epsilon)

var f string
res, err = items.JSONLookup("collectionFormat")
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, f, res)

f, ok = res.(string)
require.True(t, ok)
assert.Equal(t, "csv", f)

res, err = items.JSONLookup("unknown")
require.Error(t, err)
require.Nil(t, res)
t.Run(`lookup should find "$ref"`, func(t *testing.T) {
res, err := testItems.JSONLookup("$ref")
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, &Ref{}, res)

ref, ok := res.(*Ref)
require.True(t, ok)
assert.EqualValues(t, MustCreateRef("Dog"), *ref)
})

t.Run(`lookup should find "maximum"`, func(t *testing.T) {
var maximum *float64
res, err := testItems.JSONLookup("maximum")
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, maximum, res)

var ok bool
maximum, ok = res.(*float64)
require.True(t, ok)
assert.InDelta(t, float64(100), *maximum, epsilon)
})

t.Run(`lookup should find "collectionFormat"`, func(t *testing.T) {
var f string
res, err := testItems.JSONLookup("collectionFormat")
require.NoError(t, err)
require.NotNil(t, res)
require.IsType(t, f, res)

f, ok := res.(string)
require.True(t, ok)
assert.Equal(t, "csv", f)
})

t.Run(`lookup should fail on "unknown"`, func(t *testing.T) {
res, err := testItems.JSONLookup("unknown")
require.Error(t, err)
require.Nil(t, res)
})
}

func TestItemsWithValidation(t *testing.T) {
Expand Down
Loading
Loading