[Go] fix: add testify dependency to go.mod and go.sum unconditionally
Fixes #25270.
The go generator's go.mod.mustache placed the testify dependency inside the {{#hasOAuthMethods}} conditional (introduced in v7.15.0), even though the generated test files import testify unconditionally. As a result:
- specs without OAuth2 got a
go.modwithouttestify, sogo test ./...failed out of the box; go.sumnever contained thetestifychecksums, so even specs with OAuth2 failed.
What changed
go.mod.mustache:github.com/stretchr/testifyis now an unconditional direct dependency;- added the
// indirectrequire block (go-spew,go-difflib,yaml.v3) needed by Go 1.17+ module graph pruning so the generated tests compile without runninggo mod tidy; - bumped the
godirective from1.23to1.23.0—golang.org/x/oauth2 v0.27.0declares a patch-stylegodirective, and with the plaingo 1.23directive any read-only module load (go build/go testwithout-mod=mod) fails with "updates to go.mod needed".
go.sum.mustache: added checksums fortestifyand its transitive graph (go-spew,go-difflib,yaml.v3,check.v1), plusgo-cmp v0.5.9(required by theoauth2 v0.27.0module graph, kept inside the{{#hasOAuthMethods}}block). All checksums verified againstsum.golang.org.- Added a regression test in
GoClientCodegenTestthat generates a client fromecho_api.yaml(a spec without OAuth2) and assertstestifyis present in bothgo.modandgo.sum. - Regenerated all samples produced by the
gogenerator configs.
Verification
With Go 1.23 (docker, default read-only module mode — no -mod=mod, no go mod tidy):
samples/client/petstore/go/go-petstore(OAuth2 spec):go build ./...OK, test compilation OK;samples/client/echo_api/go(no OAuth2):go build ./...OK;go mod tidyon the regenerated output results in nogo.modchanges (it only suggests adding the optionaltoolchainline, which is environment-specific and intentionally omitted from the template).
Note: go test compilation of the echo_api sample's test/ package fails with undefined: StringEnumRef. This is a pre-existing, unrelated generator bug — a $ref'd enum path parameter is emitted without the package qualifier in api_test.mustache (it fails the same way on master with -mod=mod and is unaffected by this change, which only touches go.mod/go.sum).
PR checklist
- Read the contribution guidelines.
- Ran
./mvnw clean packageand regenerated samples via./bin/generate-samples.sh(the individualgogenerator configs); no files deleted. - Pull request is created against the
masterbranch. - @wing328 (Go technical committee) for visibility.
Summary by cubic
Fixes #25270. The Go generator no longer produces clients that fail go test out of the box: testify is now added to go.mod and go.sum unconditionally. Previously the dependency was only included inside the {{#hasOAuthMethods}} conditional, so specs without OAuth2 got a go.mod missing the dependency their generated tests import, and go.sum never contained the testify checksums.
Bug Fixes
- Moves
testifyout of the OAuth2 conditional and adds the// indirectrequires needed by Go 1.17+ module graph pruning. - Bumps the
godirective to1.23.0becausegolang.org/x/oauth2v0.27.0 declares a patch-style directive, which breaks read-only module loads with plaingo 1.23. - Adds the missing
go.sumchecksums fortestifyand its transitive graph (go-spew,go-difflib,yaml.v3,check.v1, andgo-cmpfor the OAuth2 path). - Adds a regression test that generates a client from a spec without OAuth2 and asserts
testifyis present in bothgo.modandgo.sum. - Regenerates all
gogenerator samples, including the previously missedoneof-not-enumsample.