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
2 changes: 1 addition & 1 deletion .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ linters:
- godox
- gosmopolitan
- inamedparam
#- intrange # disabled while < go1.22
- intrange
- ireturn
- lll
- musttag
Expand Down
4 changes: 2 additions & 2 deletions analysis_test/helpers_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"testing"

"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)

var (
Expand Down
4 changes: 2 additions & 2 deletions analysis_test/spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import (
"github.com/go-openapi/loads"
"github.com/go-openapi/spec"
"github.com/go-openapi/swag/loading"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)

func skipNotify(t *testing.T) {
Expand Down
53 changes: 25 additions & 28 deletions analyzer.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package analysis

import (
"fmt"
"maps"
slashpath "path"
"strconv"
"strings"
Expand Down Expand Up @@ -100,33 +101,33 @@ func (p *patternAnalysis) addSchemaPattern(key, pattern string) {
}

type enumAnalysis struct {
parameters map[string][]interface{}
headers map[string][]interface{}
items map[string][]interface{}
schemas map[string][]interface{}
allEnums map[string][]interface{}
parameters map[string][]any
headers map[string][]any
items map[string][]any
schemas map[string][]any
allEnums map[string][]any
}

func (p *enumAnalysis) addEnum(key string, enum []interface{}) {
func (p *enumAnalysis) addEnum(key string, enum []any) {
p.allEnums["#"+key] = enum
}

func (p *enumAnalysis) addParameterEnum(key string, enum []interface{}) {
func (p *enumAnalysis) addParameterEnum(key string, enum []any) {
p.parameters["#"+key] = enum
p.addEnum(key, enum)
}

func (p *enumAnalysis) addHeaderEnum(key string, enum []interface{}) {
func (p *enumAnalysis) addHeaderEnum(key string, enum []any) {
p.headers["#"+key] = enum
p.addEnum(key, enum)
}

func (p *enumAnalysis) addItemsEnum(key string, enum []interface{}) {
func (p *enumAnalysis) addItemsEnum(key string, enum []any) {
p.items["#"+key] = enum
p.addEnum(key, enum)
}

func (p *enumAnalysis) addSchemaEnum(key string, enum []interface{}) {
func (p *enumAnalysis) addSchemaEnum(key string, enum []any) {
p.schemas["#"+key] = enum
p.addEnum(key, enum)
}
Expand Down Expand Up @@ -622,31 +623,31 @@ func (s *Spec) AllPatterns() map[string]string {

// ParameterEnums returns all the enums found in parameters
// the map is cloned to avoid accidental changes
func (s *Spec) ParameterEnums() map[string][]interface{} {
func (s *Spec) ParameterEnums() map[string][]any {
return cloneEnumMap(s.enums.parameters)
}

// HeaderEnums returns all the enums found in response headers
// the map is cloned to avoid accidental changes
func (s *Spec) HeaderEnums() map[string][]interface{} {
func (s *Spec) HeaderEnums() map[string][]any {
return cloneEnumMap(s.enums.headers)
}

// ItemsEnums returns all the enums found in simple array items
// the map is cloned to avoid accidental changes
func (s *Spec) ItemsEnums() map[string][]interface{} {
func (s *Spec) ItemsEnums() map[string][]any {
return cloneEnumMap(s.enums.items)
}

// SchemaEnums returns all the enums found in schemas
// the map is cloned to avoid accidental changes
func (s *Spec) SchemaEnums() map[string][]interface{} {
func (s *Spec) SchemaEnums() map[string][]any {
return cloneEnumMap(s.enums.schemas)
}

// AllEnums returns all the enums found in the spec
// the map is cloned to avoid accidental changes
func (s *Spec) AllEnums() map[string][]interface{} {
func (s *Spec) AllEnums() map[string][]any {
return cloneEnumMap(s.enums.allEnums)
}

Expand Down Expand Up @@ -722,11 +723,11 @@ func (s *Spec) reset() {
s.patterns.items = make(map[string]string, allocLargeMap)
s.patterns.schemas = make(map[string]string, allocLargeMap)
s.patterns.allPatterns = make(map[string]string, allocLargeMap)
s.enums.parameters = make(map[string][]interface{}, allocLargeMap)
s.enums.headers = make(map[string][]interface{}, allocLargeMap)
s.enums.items = make(map[string][]interface{}, allocLargeMap)
s.enums.schemas = make(map[string][]interface{}, allocLargeMap)
s.enums.allEnums = make(map[string][]interface{}, allocLargeMap)
s.enums.parameters = make(map[string][]any, allocLargeMap)
s.enums.headers = make(map[string][]any, allocLargeMap)
s.enums.items = make(map[string][]any, allocLargeMap)
s.enums.schemas = make(map[string][]any, allocLargeMap)
s.enums.allEnums = make(map[string][]any, allocLargeMap)
}

func (s *Spec) reload() {
Expand Down Expand Up @@ -1040,18 +1041,14 @@ func (s *Spec) analyzeSchema(name string, schema *spec.Schema, prefix string) {

func cloneStringMap(source map[string]string) map[string]string {
res := make(map[string]string, len(source))
for k, v := range source {
res[k] = v
}
maps.Copy(res, source)

return res
}

func cloneEnumMap(source map[string][]interface{}) map[string][]interface{} {
res := make(map[string][]interface{}, len(source))
for k, v := range source {
res[k] = v
}
func cloneEnumMap(source map[string][]any) map[string][]any {
res := make(map[string][]any, len(source))
maps.Copy(res, source)

return res
}
38 changes: 19 additions & 19 deletions analyzer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

"github.com/go-openapi/analysis/internal/antest"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)

const (
Expand All @@ -28,7 +28,7 @@ func TestAnalyzer_All(t *testing.T) {
formatParam := spec.QueryParam("format").Typed("string", "")

limitParam := spec.QueryParam("limit").Typed("integer", "int32")
limitParam.Extensions = spec.Extensions(map[string]interface{}{})
limitParam.Extensions = spec.Extensions(map[string]any{})
limitParam.Extensions.Add("go-name", "Limit")

skipParam := spec.QueryParam("skip").Typed("integer", "int32")
Expand Down Expand Up @@ -803,33 +803,33 @@ func TestAnalyzer_EnumAnalysis(t *testing.T) {
en := an.enums

// parameters
assertEnum(t, en.parameters, "#/parameters/idParam", []interface{}{"aA", "b9", "c3"})
assertEnum(t, en.parameters, "#/paths/~1some~1where~1{id}/parameters/1", []interface{}{"bA", "ba", "b9"})
assertEnum(t, en.parameters, "#/paths/~1some~1where~1{id}/get/parameters/0", []interface{}{"a0", "b1", "c2"})
assertEnum(t, en.parameters, "#/parameters/idParam", []any{"aA", "b9", "c3"})
assertEnum(t, en.parameters, "#/paths/~1some~1where~1{id}/parameters/1", []any{"bA", "ba", "b9"})
assertEnum(t, en.parameters, "#/paths/~1some~1where~1{id}/get/parameters/0", []any{"a0", "b1", "c2"})

// responses
assertEnum(t, en.headers, "#/responses/notFound/headers/ContentLength", []interface{}{"1234", "123"})
assertEnum(t, en.headers, "#/responses/notFound/headers/ContentLength", []any{"1234", "123"})
assertEnum(t, en.headers,
"#/paths/~1some~1where~1{id}/get/responses/200/headers/X-Request-Id", []interface{}{"dA", "d9"})
"#/paths/~1some~1where~1{id}/get/responses/200/headers/X-Request-Id", []any{"dA", "d9"})

// definitions
assertEnum(t, en.schemas,
"#/paths/~1other~1place/post/parameters/0/schema/properties/value", []interface{}{"eA", "e9"})
"#/paths/~1other~1place/post/parameters/0/schema/properties/value", []any{"eA", "e9"})
assertEnum(t, en.schemas, "#/paths/~1other~1place/post/responses/200/schema/properties/data",
[]interface{}{"123a", "123b", "123d"})
assertEnum(t, en.schemas, "#/definitions/named", []interface{}{"fA", "f9"})
assertEnum(t, en.schemas, "#/definitions/tag/properties/value", []interface{}{"gA", "ga", "g9"})
[]any{"123a", "123b", "123d"})
assertEnum(t, en.schemas, "#/definitions/named", []any{"fA", "f9"})
assertEnum(t, en.schemas, "#/definitions/tag/properties/value", []any{"gA", "ga", "g9"})
assertEnum(t, en.schemas, "#/definitions/record",
[]interface{}{`{"createdAt": "2018-08-31"}`, `{"createdAt": "2018-09-30"}`})
[]any{`{"createdAt": "2018-08-31"}`, `{"createdAt": "2018-09-30"}`})

// array enum
assertEnum(t, en.parameters, "#/paths/~1some~1where~1{id}/get/parameters/1",
[]interface{}{[]interface{}{"cA", "cz", "c9"}, []interface{}{"cA", "cz"}, []interface{}{"cz", "c9"}})
[]any{[]any{"cA", "cz", "c9"}, []any{"cA", "cz"}, []any{"cz", "c9"}})

// items
assertEnum(t, en.items, "#/paths/~1some~1where~1{id}/get/parameters/1/items", []interface{}{"cA", "cz", "c9"})
assertEnum(t, en.items, "#/paths/~1some~1where~1{id}/get/parameters/1/items", []any{"cA", "cz", "c9"})
assertEnum(t, en.items, "#/paths/~1other~1place/post/responses/default/headers/Via/items",
[]interface{}{"AA", "Ab"})
[]any{"AA", "Ab"})

res := an.AllEnums()
assert.Lenf(t, res, 14, "Expected 14 enums in this spec, but got %d", len(res))
Expand Down Expand Up @@ -885,7 +885,7 @@ func makeFixturepec(pi, pi2 spec.PathItem, formatParam *spec.Parameter) *spec.Sw
}
}

func assertEnum(t testing.TB, data map[string][]interface{}, key string, enum []interface{}) {
func assertEnum(t testing.TB, data map[string][]any, key string, enum []any) {
require.Contains(t, data, key)
assert.Equal(t, enum, data[key])
}
Expand Down Expand Up @@ -914,7 +914,7 @@ func prepareTestParamsAuth() *Spec {
formatParam := spec.QueryParam("format").Typed("string", "")

limitParam := spec.QueryParam("limit").Typed("integer", "int32")
limitParam.Extensions = spec.Extensions(map[string]interface{}{})
limitParam.Extensions = spec.Extensions(map[string]any{})
limitParam.Extensions.Add("go-name", "Limit")

skipParam := spec.QueryParam("skip").Typed("integer", "int32")
Expand Down Expand Up @@ -979,7 +979,7 @@ func prepareTestParamsValid() *Spec {
formatParam := spec.QueryParam("format").Typed("string", "")

limitParam := spec.QueryParam("limit").Typed("integer", "int32")
limitParam.Extensions = spec.Extensions(map[string]interface{}{})
limitParam.Extensions = spec.Extensions(map[string]any{})
limitParam.Extensions.Add("go-name", "Limit")

skipParam := spec.QueryParam("skip").Typed("integer", "int32")
Expand Down
4 changes: 2 additions & 2 deletions fixer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (

"github.com/go-openapi/analysis/internal/antest"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)

func TestFixer_EmptyResponseDescriptions(t *testing.T) {
Expand Down
10 changes: 2 additions & 8 deletions flatten.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package analysis
import (
"log"
"path"
"slices"
"sort"
"strings"

Expand Down Expand Up @@ -530,14 +531,7 @@ func updateRefParents(allRefs map[string]spec.Ref, r *newRef) {
continue
}

found := false
for _, p := range r.parents {
if p == k {
found = true

break
}
}
found := slices.Contains(r.parents, k)
if !found {
r.parents = append(r.parents, k)
}
Expand Down
4 changes: 2 additions & 2 deletions flatten_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
"github.com/go-openapi/analysis/internal/flatten/sortref"
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)

func TestName_FromRef(t *testing.T) {
Expand Down
6 changes: 3 additions & 3 deletions flatten_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ import (
"github.com/go-openapi/analysis/internal/flatten/operations"
"github.com/go-openapi/jsonpointer"
"github.com/go-openapi/spec"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)

var (
Expand All @@ -33,7 +33,7 @@ type refFixture struct {
Key string
Ref spec.Ref
Location string
Expected interface{}
Expected any
}

func makeRefFixtures() []refFixture {
Expand Down
20 changes: 8 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,17 @@ module github.com/go-openapi/analysis

require (
github.com/go-openapi/jsonpointer v0.22.1
github.com/go-openapi/spec v0.22.0
github.com/go-openapi/strfmt v0.24.0
github.com/go-openapi/spec v0.22.1
github.com/go-openapi/strfmt v0.25.0
github.com/go-openapi/swag/jsonutils v0.25.1
github.com/go-openapi/swag/loading v0.25.1
github.com/go-openapi/swag/mangling v0.25.1
github.com/stretchr/testify v1.11.1
github.com/go-openapi/testify/v2 v2.0.2
)

require (
github.com/asaskevich/govalidator v0.0.0-20230301143203-a9d515a09cc2 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/go-openapi/errors v0.22.3 // indirect
github.com/go-openapi/jsonreference v0.21.2 // indirect
github.com/go-openapi/errors v0.22.4 // indirect
github.com/go-openapi/jsonreference v0.21.3 // indirect
github.com/go-openapi/swag/conv v0.25.1 // indirect
github.com/go-openapi/swag/jsonname v0.25.1 // indirect
github.com/go-openapi/swag/stringutils v0.25.1 // indirect
Expand All @@ -23,12 +21,10 @@ require (
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/oklog/ulid v1.3.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.mongodb.org/mongo-driver v1.17.4 // indirect
go.mongodb.org/mongo-driver v1.17.6 // indirect
go.yaml.in/yaml/v3 v3.0.4 // indirect
golang.org/x/net v0.44.0 // indirect
golang.org/x/text v0.29.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
golang.org/x/net v0.46.0 // indirect
golang.org/x/text v0.30.0 // indirect
)

go 1.24.0
Loading
Loading