diff --git a/.golangci.yml b/.golangci.yml index ee64ed3b..dd04cc2f 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -47,10 +47,15 @@ linters-settings: files: $all allow: - $gostd + # depguard resolves imports by longest-prefix match, so the + # "go.yaml.in/..." entry below registers a "go" prefix that shadows + # the shorter $gostd-provided stdlib "go/..." packages (go/token, + # go/ast, ...). Re-allow them explicitly. + - go/ - github.com/Khan/genqlient - github.com/vektah/gqlparser/v2 - golang.org/x/tools - - gopkg.in/yaml.v2 + - go.yaml.in/yaml/v3 - github.com/alexflint/go-arg - github.com/bmatcuk/doublestar/v4 - github.com/google/uuid diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index a9bdbd06..b8835dd9 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -33,6 +33,7 @@ Note that genqlient now requires Go 1.23 or higher, and is tested through Go 1.2 ### Bug fixes: +- replaced the archived `gopkg.in/yaml.v2` dependency with the maintained `go.yaml.in/yaml/v3` (the YAML organization's successor to `gopkg.in/yaml.v3`) for config parsing. - fixed `pointer_omitempty` not being applied to list types when `use_struct_references` is enabled. List fields like `[String!]` now correctly get the `omitempty` JSON tag. - fixed minor typos and grammatical issues across the project diff --git a/generate/config.go b/generate/config.go index 8f02d566..daf16d9a 100644 --- a/generate/config.go +++ b/generate/config.go @@ -1,15 +1,18 @@ package generate import ( + "bytes" _ "embed" + "errors" "fmt" "go/token" + "io" "os" "path/filepath" "strings" + "go.yaml.in/yaml/v3" "golang.org/x/tools/go/packages" - "gopkg.in/yaml.v2" ) var cfgFilenames = []string{".genqlient.yml", ".genqlient.yaml", "genqlient.yml", "genqlient.yaml"} @@ -323,8 +326,10 @@ func ReadAndValidateConfig(filename string) (*Config, error) { } var config Config - err = yaml.UnmarshalStrict(text, &config) - if err != nil { + dec := yaml.NewDecoder(bytes.NewReader(text)) + dec.KnownFields(true) + err = dec.Decode(&config) + if err != nil && !errors.Is(err, io.EOF) { return nil, errorf(nil, "invalid config file %v: %v", filename, err) } diff --git a/generate/generate_test.go b/generate/generate_test.go index fec629f9..b1eec86d 100644 --- a/generate/generate_test.go +++ b/generate/generate_test.go @@ -1,6 +1,7 @@ package generate import ( + "bytes" "errors" "fmt" "os" @@ -9,7 +10,7 @@ import ( "strings" "testing" - "gopkg.in/yaml.v2" + "go.yaml.in/yaml/v3" "github.com/Khan/genqlient/internal/testutil" ) @@ -133,7 +134,9 @@ func getDefaultConfig(t *testing.T) *Config { t.Fatal(err) } - err = yaml.UnmarshalStrict(b, &config) + dec := yaml.NewDecoder(bytes.NewReader(b)) + dec.KnownFields(true) + err = dec.Decode(&config) if err != nil { t.Fatal(err) } diff --git a/generate/testdata/snapshots/TestValidConfigs-YAML11Booleans.yaml b/generate/testdata/snapshots/TestValidConfigs-YAML11Booleans.yaml new file mode 100644 index 00000000..43e882b9 --- /dev/null +++ b/generate/testdata/snapshots/TestValidConfigs-YAML11Booleans.yaml @@ -0,0 +1,27 @@ +(*generate.Config)({ + Schema: (generate.StringList) (len=1) { + (string) (len=35) "testdata/validConfig/schema.graphql" + }, + Operations: (generate.StringList) (len=1) { + (string) (len=39) "testdata/validConfig/operations.graphql" + }, + Generated: (string) (len=33) "testdata/validConfig/generated.go", + Package: (string) (len=11) "validConfig", + ExportOperations: (string) (len=25) "testdata/validConfig/0777", + ContextType: (string) (len=15) "context.Context", + ClientGetter: (string) "", + Bindings: (map[string]*generate.TypeBinding) , + PackageBindings: ([]*generate.PackageBinding) , + Casing: (generate.Casing) { + Default: (generate.CasingAlgorithm) "", + AllEnums: (generate.CasingAlgorithm) "", + Enums: (map[string]generate.CasingAlgorithm) + }, + Optional: (string) "", + OptionalGenericType: (string) "", + StructReferences: (bool) true, + Extensions: (bool) true, + Flatten: (bool) false, + baseDir: (string) (len=20) "testdata/validConfig", + pkgPath: (string) (len=55) "github.com/Khan/genqlient/generate/testdata/validConfig" +}) diff --git a/generate/testdata/validConfig/YAML11Booleans.yaml b/generate/testdata/validConfig/YAML11Booleans.yaml new file mode 100644 index 00000000..38f26961 --- /dev/null +++ b/generate/testdata/validConfig/YAML11Booleans.yaml @@ -0,0 +1,10 @@ +schema: schema.graphql +operations: operations.graphql + +# YAML 1.1 boolean spellings (yes/no/on/off) must still decode to bools, as +# they did under gopkg.in/yaml.v2, to avoid breaking existing config files. +use_struct_references: yes +use_extensions: on + +# Octal-looking strings must stay strings (e.g. "0777", not 511). +export_operations: 0777 diff --git a/go.mod b/go.mod index b07078e1..b0e62ca4 100644 --- a/go.mod +++ b/go.mod @@ -13,8 +13,8 @@ require ( github.com/gorilla/websocket v1.5.0 github.com/stretchr/testify v1.9.0 github.com/vektah/gqlparser/v2 v2.5.19 + go.yaml.in/yaml/v3 v3.0.4 golang.org/x/tools v0.36.0 - gopkg.in/yaml.v2 v2.4.0 ) require ( diff --git a/go.sum b/go.sum index 2facde28..7c4c2c4f 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/vektah/gqlparser/v2 v2.5.19 h1:bhCPCX1D4WWzCDvkPl4+TP1N8/kLrWnp43egplt7iSg= github.com/vektah/gqlparser/v2 v2.5.19/go.mod h1:y7kvl5bBlDeuWIvLtA9849ncyvx6/lj06RsMrEjVy3U= +go.yaml.in/yaml/v3 v3.0.4 h1:tfq32ie2Jv2UxXFdLJdh3jXuOzWiL1fo0bu/FbuKpbc= +go.yaml.in/yaml/v3 v3.0.4/go.mod h1:DhzuOOF2ATzADvBadXxruRBLzYTpT36CKvDb3+aBEFg= golang.org/x/mod v0.27.0 h1:kb+q2PyFnEADO2IEF935ehFUXlWiNjJWtRNgBLSfbxQ= golang.org/x/mod v0.27.0/go.mod h1:rWI627Fq0DEoudcK+MBkNkCe0EetEaDSwJJkCcjpazc= golang.org/x/sync v0.16.0 h1:ycBJEhp9p4vXvUZNszeOq0kGTPghopOL8q0fq3vstxw= @@ -51,8 +53,6 @@ golang.org/x/tools v0.36.0 h1:kWS0uv/zsvHEle1LbV5LE8QujrxB3wfQyxHfhOk0Qkg= golang.org/x/tools v0.36.0/go.mod h1:WBDiHKJK8YgLHlcQPYQzNCkUxUypCaa5ZegCVutKm+s= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= -gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY= -gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=