go-swagger / go-swagger

Swagger 2.0 implementation for go
https://goswagger.io
Apache License 2.0
9.56k stars 1.25k forks source link

Go v1.23.0 `panic: WARNING: can't determine refined type any (*types.Alias)` #3131

Open tsmethurst opened 2 months ago

tsmethurst commented 2 months ago

Problem statement

Hello hello, I can't seem to get go swagger generating a yaml output from code annotations on Go v1.23.0, where it was working fine in Go v1.22.x.

When running the following command in the GoToSocial repo with go bumped to v1.23.0:

go run github.com/go-swagger/go-swagger/cmd/swagger generate spec --scan-models --exclude-deps --output docs/api/swagger.yaml

The following panic occurs:

panic: WARNING: can't determine refined type any (*types.Alias)

goroutine 1 [running]:
github.com/go-swagger/go-swagger/codescan.(*schemaBuilder).buildFromType(0xc018e6faf8, {0x14ca240, 0xc0000b8dc0}, {0x14d30f8, 0xc052567b20})
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/schema.go:471 +0xa5e
github.com/go-swagger/go-swagger/codescan.(*schemaBuilder).buildFromType(0xc018e6faf8, {0x14c90c0, 0xc021eec720}, {0x14d30f8, 0xc052567940})
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/schema.go:315 +0x8a7
github.com/go-swagger/go-swagger/codescan.(*schemaBuilder).buildFromType(0xc018e6faf8, {0x14c8fa8, 0xc021eec6e0}, {0x14d30f8, 0xc052567940})
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/schema.go:309 +0x7ff
github.com/go-swagger/go-swagger/codescan.(*schemaBuilder).buildFromStruct(0xc018e6faf8, 0xc0548d5ac0, 0xc012ad8720, 0xc053212008, 0xc018e6f4d0)
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/schema.go:832 +0xd25
github.com/go-swagger/go-swagger/codescan.(*schemaBuilder).buildFromType(0xc018e6faf8, {0x14c8ff8, 0xc012ad8720}, {0x14d30f8, 0xc052566c30})
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/schema.go:311 +0x978
github.com/go-swagger/go-swagger/codescan.(*schemaBuilder).buildFromDecl(0xc018e6faf8, 0xc05808b470?, 0xc053212008)
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/schema.go:220 +0xccc
github.com/go-swagger/go-swagger/codescan.(*schemaBuilder).Build(0xc018e6faf8, 0xc05808b470)
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/schema.go:159 +0xc7
github.com/go-swagger/go-swagger/codescan.(*specBuilder).buildDiscoveredSchema(0xc04ef6b310, 0xc05808b3e0?)
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/spec.go:114 +0x8d
github.com/go-swagger/go-swagger/codescan.(*specBuilder).buildModels(0xc04ef6b310)
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/spec.go:200 +0x98
github.com/go-swagger/go-swagger/codescan.(*specBuilder).Build(0xc04ef6b310)
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/spec.go:49 +0x1c
github.com/go-swagger/go-swagger/codescan.Run(0xc00050bc70)
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/codescan/application.go:77 +0x45
github.com/go-swagger/go-swagger/cmd/swagger/commands/generate.(*SpecFile).Execute(0xc0000e06e0, {0xc0000bac00?, 0x7ffcf67fdf01?, 0x6?})
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/cmd/swagger/commands/generate/spec.go:68 +0x1c5
github.com/jessevdk/go-flags.(*Parser).ParseArgs(0xc00024e070, {0xc000036080, 0x6, 0x6})
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/jessevdk/go-flags/parser.go:335 +0xacc
github.com/jessevdk/go-flags.(*Parser).Parse(...)
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/jessevdk/go-flags/parser.go:191
main.main()
    /home/tobi/go/src/github.com/superseriousbusiness/gotosocial/vendor/github.com/go-swagger/go-swagger/cmd/swagger/swagger.go:140 +0x959
exit status 2

Thanks for reading! ^_^

Environment

swagger version: 0.31.0
go version: 1.23.0
OS: Linux amd64 (Ubuntu 22.04)

aeneasr commented 1 month ago

It appears that

https://github.com/go-swagger/go-swagger/blob/bb4bd9ac1bae0e8bdb30903ba54618caeb1e3643/codescan/schema.go#L305-L312

lacks support for type.Alias. I've experimented with various handlers but none resulted in success (the panic was gone, but swagger annotations on alias types are now broken):


    case *types.Alias:
        fmt.Printf("alias: %v -> %v\n", s.decl.Ident.Name, titpe.Rhs().String())
        ret := s.buildFromType(titpe.Rhs(), tgt)
        fmt.Printf("returning from alias: %v -> %v\n", s.decl.Ident.Name, ret) // always gives me nil
        return ret