Skip to content

Panic: runtime error invalid memory address or nil pointer dereference when using generic types #1995

@okliuxing

Description

@okliuxing

Describe the bug
I encountered a runtime error while running swag init with generic types in Go. Here are the details:

Error Log:

PS C:\Project> swag init --v3.1  -g handler/handler.go --parseDependency --parseInternal
Swag version:  v2.0.0
...
2025/03/25 15:38:40 Generating UserInfo
panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x18 pc=0x11d6eaa]

goroutine 1 [running]:
github.com/swaggo/swag/v2.fillDefinitionDescriptionV3(0xc0000a2000, 0xc019702908, 0x154af50?, 0xc019635740)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:768 +0x2a
github.com/swaggo/swag/v2.(*Parser).ParseDefinitionV3(0xc0000a2000, 0xc019635740)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:725 +0x43a
github.com/swaggo/swag/v2.(*Parser).getTypeSchemaV3(0xc0000a2000, {0xc0189ddc40, 0x1d}, 0xc000c1c640, 0x1)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:669 +0xa06
github.com/swaggo/swag/v2.(*Parser).parseStructFieldV3(0xc0000a2000, 0xc000c1c640, 0xc0196b4180)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:973 +0x391
github.com/swaggo/swag/v2.(*Parser).parseStructV3(0xc0000a2000, 0xc000c1c640, 0xc01bdf43c0)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:885 +0xca
github.com/swaggo/swag/v2.(*Parser).parseTypeExprV3(0xc0000a2000, 0x145d195?, {0x154b010?, 0xc01b5507e0?}, 0xe0?)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:808 +0x447
github.com/swaggo/swag/v2.(*Parser).ParseDefinitionV3(0xc0000a2000, 0xc0196357a0)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:718 +0x405
github.com/swaggo/swag/v2.(*Parser).getTypeSchemaV3(0xc0000a2000, {0xc00051a889, 0x28}, 0xc0001c7860, 0x1)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parserv3.go:669 +0xa06
github.com/swaggo/swag/v2.parseObjectSchemaV3(0xc0000a2000, {0xc00051a889, 0x28}, 0xc0001c7860)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/operationv3.go:851 +0xf9d
github.com/swaggo/swag/v2.(*OperationV3).parseObjectSchema(...)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/operationv3.go:791
github.com/swaggo/swag/v2.(*OperationV3).parseAPIObjectSchema(0xc01973b000, {0xc00051a87c, 0x35}, {0xc00051a881, 0x6}, {0xc00051a889, 0x28}, 0xc0001c7860)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/operationv3.go:705 +0x446
github.com/swaggo/swag/v2.(*OperationV3).ParseResponseComment(0xc01973b000, {0xc00051a87c, 0x35}, 0xc0001c7860)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/operationv3.go:939 +0x13d
github.com/swaggo/swag/v2.(*OperationV3).ParseComment(0xc01973b000, {0xc00051a870?, 0x1?}, 0xc0001c7860)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/operationv3.go:88 +0x329
github.com/swaggo/swag/v2.(*Parser).ParseRouterAPIInfoV3(0xc0000a2000, 0xc0007cedc0)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/packages.go:107 +0x33c
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/parser.go:499 +0x63a
github.com/swaggo/swag/v2/gen.(*Gen).Build(0xc0001d5d40, 0xc000128300)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/gen/gen.go:233 +0x7aa
main.initAction(0xc0001ea380)
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/cmd/swag/main.go:249 +0xa08
github.com/urfave/cli/v2.(*Command).Run(0xc0001c4420, 0xc0001ea380, {0xc000076a20, 0x6, 0x6})
        C:/Users/null/go/pkg/mod/github.com/urfave/cli/v2@v2.25.1/command.go:274 +0x77b
github.com/urfave/cli/v2.(*Command).Run(0xc0001c4840, 0xc0001ea240, {0xc000110000, 0x7, 0x8})
        C:/Users/null/go/pkg/mod/github.com/urfave/cli/v2@v2.25.1/command.go:267 +0x9fc
github.com/urfave/cli/v2.(*App).RunContext(0xc0001ee000, {0x154c0a8, 0x1a31a20}, {0xc000110000, 0x7, 0x8})
        C:/Users/null/go/pkg/mod/github.com/urfave/cli/v2@v2.25.1/app.go:332 +0x566
github.com/urfave/cli/v2.(*App).Run(...)
        C:/Users/null/go/pkg/mod/github.com/urfave/cli/v2@v2.25.1/app.go:309
main.main()
        C:/Users/null/go/pkg/mod/github.com/swaggo/swag/v2@v2.0.0-rc4/cmd/swag/main.go:341 +0x5d8

Code Sample:

type Result[T any] struct {
	Code    Code   `json:"code"`
	Data    T      `json:"data"`
	Message string `json:"message"`
}

type UserInfo struct {
        Id      int    `json:"id"`
}

// List godoc
//
//	@Summary	get list info
//	@Schemes
//	@Description
//	@Tags		test
//	@Accept		json
//	@Produce	json
//	@Success	200	{object}	Result[[]UserInfo]
//	@Router		/list [get]
func List(c *gin.Context) {
        // ...
}

Environment:

  • swag version: v2.0.0
  • go version: go1.24.0 windows/amd64

Steps to Reproduce:

  1. Define a generic type in Go.
  2. Annotate a handler function with Swag comments.
  3. Run swag init --v3.1 -g handler/handler.go --parseDependency --parseInternal.
    Expected Behavior: Swag should generate the Swagger documentation without errors.

Actual Behavior: Swag panics with a runtime error.

Additional Context: It seems that Swag is unable to correctly parse the generic type. Any guidance or fix for this issue would be appreciated.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions