Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2: Segmentation Error When using Slice as a type for generic (Wrapper[[]T]) in -v3.1 #1935

Open
OnFireByte opened this issue Dec 4, 2024 · 0 comments

Comments

@OnFireByte
Copy link

OnFireByte commented Dec 4, 2024

Describe the bug
In v3.1 mode, if the return object is in generic form with slice as a generic argument (ex. Wrapper[[]Data]), it will cause the program to crash by segmentation error.

This problem seem to not occur in swagger-v2 mode, and -v3.1 mode in rc3.

To Reproduce

  1. Create a new route with generic type as a return type
type WithData[T any] struct {
	Data T `json:"data"`
}

type Foo struct {
	Bar string `json:"bar"`
	Baz int    `json:"baz"`
}

// @Summary		Get Foo
// @Router			/foo [get]
// @Success		200			{object}	WithData[[]Foo]
func GetFoo() {}
  1. Run swag init -v3.1 -g ./main.go
  2. See error

Expected behavior
To run successfully

Screenshots

❯ swag init -v3.1 -g ./cmd/api/main.go 
Swag version:  v2.0.0
2024/12/04 22:20:54 Generate swagger docs....
2024/12/04 22:20:54 Generate general API Info, search dir:./
get pkg name for directory: ./
2024/12/04 22:20:54 warning: failed to get package name in dir: ./, error: execute go list command, exit status 1, stdout:, stderr:no Go files in /Users/byte/playground/go/swag
@host is deprecated use servers instead
@basepath is deprecated use servers instead
2024/12/04 22:20:54 Generating main.WithData-array_main_Foo
2024/12/04 22:20:54 Generating array_main.Foo
2024/12/04 22:20:54 Generating main.Foo
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x104a73848]

goroutine 1 [running]:
github.com/swaggo/swag/v2.fillDefinitionDescriptionV3(0x1400021adc0, 0x14000200f08, 0x104d5d9e0?, 0x14000135260)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/parserv3.go:768 +0x28
github.com/swaggo/swag/v2.(*Parser).ParseDefinitionV3(0x1400021adc0, 0x14000135260)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/parserv3.go:725 +0x3c8
github.com/swaggo/swag/v2.(*Parser).getTypeSchemaV3(0x1400021adc0, {0x14000243d90, 0xe}, 0x1400020fa40, 0x1)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/parserv3.go:669 +0xa88
github.com/swaggo/swag/v2.(*Parser).parseStructFieldV3(0x1400021adc0, 0x1400020fa40, 0x1400022fc40)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/parserv3.go:973 +0x324
github.com/swaggo/swag/v2.(*Parser).parseStructV3(0x1400021adc0, 0x1400020fa40, 0x14000223a70)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/parserv3.go:885 +0xac
github.com/swaggo/swag/v2.(*Parser).parseTypeExprV3(0x1400021adc0, 0x104b43ad8?, {0x104d5daa0?, 0x14000125c98?}, 0xe0?)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/parserv3.go:808 +0x8c8
github.com/swaggo/swag/v2.(*Parser).ParseDefinitionV3(0x1400021adc0, 0x140001352c0)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/parserv3.go:718 +0x3a4
github.com/swaggo/swag/v2.(*Parser).getTypeSchemaV3(0x1400021adc0, {0x1400014476c, 0xf}, 0x1400020fa40, 0x1)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/parserv3.go:669 +0xa88
github.com/swaggo/swag/v2.parseObjectSchemaV3(0x1400021adc0, {0x1400014476c, 0xf}, 0x1400020fa40)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/operationv3.go:851 +0x1068
github.com/swaggo/swag/v2.(*OperationV3).parseObjectSchema(...)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/operationv3.go:791
github.com/swaggo/swag/v2.(*OperationV3).parseAPIObjectSchema(0x140001c0600, {0x1400014475d, 0x1e}, {0x14000144764, 0x6}, {0x1400014476c, 0xf}, 0x1400020fa40)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/operationv3.go:705 +0x3bc
github.com/swaggo/swag/v2.(*OperationV3).ParseResponseComment(0x140001c0600, {0x1400014475d, 0x1e}, 0x1400020fa40)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/operationv3.go:939 +0x110
github.com/swaggo/swag/v2.(*OperationV3).ParseComment(0x140001c0600, {0x14000144750?, 0x1?}, 0x1400020fa40)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/operationv3.go:88 +0x374
github.com/swaggo/swag/v2.(*Parser).ParseRouterAPIInfoV3(0x1400021adc0, 0x1400022f380)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/parserv3.go:514 +0x194
github.com/swaggo/swag/v2.(*PackagesDefinitions).RangeFiles(0x140002225d0, 0x140001a14d0)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/packages.go:107 +0x2cc
github.com/swaggo/swag/v2.(*Parser).ParseAPIMultiSearchDir(0x1400021adc0, {0x14000240ab0, 0x1, 0x0?}, {0x16b81707b, 0x11}, 0x64)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/parser.go:499 +0x4ac
github.com/swaggo/swag/v2/gen.(*Gen).Build(0x140002222d0, 0x14000246000)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/gen/gen.go:233 +0x6a4
main.initAction(0x1400022e080)
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/cmd/swag/main.go:249 +0x7f8
github.com/urfave/cli/v2.(*Command).Run(0x140001d4420, 0x1400022e080, {0x1400022e0c0, 0x4, 0x4})
	/Users/byte/go/pkg/mod/github.com/urfave/cli/[email protected]/command.go:274 +0x6fc
github.com/urfave/cli/v2.(*Command).Run(0x140001d4840, 0x1400015de80, {0x14000140000, 0x5, 0x5})
	/Users/byte/go/pkg/mod/github.com/urfave/cli/[email protected]/command.go:267 +0x8f8
github.com/urfave/cli/v2.(*App).RunContext(0x14000224000, {0x104d5ed18, 0x1051b9140}, {0x14000140000, 0x5, 0x5})
	/Users/byte/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:332 +0x4f8
github.com/urfave/cli/v2.(*App).Run(...)
	/Users/byte/go/pkg/mod/github.com/urfave/cli/[email protected]/app.go:309
main.main()
	/Users/byte/go/pkg/mod/github.com/swaggo/swag/[email protected]/cmd/swag/main.go:341 +0x594

Your swag version
v2.0.0-rc4

Your go version
e.g. 1.22.5

Desktop (please complete the following information):

  • OS: MacOS 14.2.1
  • Version: 14.2.1

Additional context
Add any other context about the problem here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant