swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.46k stars 1.19k forks source link

If I use generic array type instead of regular array it panics. #1763

Closed myyrakle closed 6 months ago

myyrakle commented 6 months ago

Describe the bug To reduce boilerplate, I defined a generic array type instead of using a named type of array.

To Reproduce First of all, I defined the following generic array wrapper:

package slices

type SliceWrapper[T any] []T

func (s SliceWrapper[T]) Append(v T) SliceWrapper[T] {
    return append(s, v)
}

func (s SliceWrapper[T]) IsEmpty() bool {
    return len(s) == 0
}

func (s SliceWrapper[T]) Len() int {
    return len(s)
}

func (s SliceWrapper[T]) First() T {
    if s.IsEmpty() {
        var t T
        return t
    }

    return s[0]
}

Previously, I defined and used the following response dto type.

...
type ShopsResponse []ShopResponse

type ListShopsResponse struct {
    Shops      ShopsResponse      `json:"shops"`      
    Pagination PaginationResponse `json:"pagination"` 
}

But if I apply a generic type like this:

...
type ShopsResponse slices.SliceWrapper[ShopResponse]

type ListShopsResponse struct {
    Shops      ShopsResponse      `json:"shops"`      
    Pagination PaginationResponse `json:"pagination"` 
}

It panics and fails.

2024/02/19 18:28:31 Generating domain.ShopsResponse
panic: interface conversion: ast.Expr is *ast.ArrayType, not *ast.StructType

goroutine 1 [running]:
github.com/swaggo/swag.(*PackagesDefinitions).parametrizeStruct(0x14003b16c18?, 0x140009c06c0, 0x14003b23290, {0x140041bd3e1, 0x21}, 0x30?)
        /Users/myyrakle/go/pkg/mod/github.com/swaggo/swag@v1.8.6/generics.go:145 +0xe60
github.com/swaggo/swag.(*PackagesDefinitions).findGenericTypeSpec(0x140000a06c0, {0x140041bd3e1, 0x21}, 0x140009c06c0?, 0x0?)
        /Users/myyrakle/go/pkg/mod/github.com/swaggo/swag@v1.8.6/packages.go:435 +0x12c
github.com/swaggo/swag.(*PackagesDefinitions).FindTypeSpec(0x140000a06c0, {0x140041bd3e1, 0x21}, 0x140009c06c0, 0x0)
        /Users/myyrakle/go/pkg/mod/github.com/swaggo/swag@v1.8.6/packages.go:392 +0x738
github.com/swaggo/swag.(*Parser).getTypeSchema(0x140001581c0, {0x140041bd3e1, 0x21}, 0x0?, 0x0)
        /Users/myyrakle/go/pkg/mod/github.com/swaggo/swag@v1.8.6/parser.go:891 +0x6c0
github.com/swaggo/swag.(*Parser).parseGenericTypeExpr(0x140001581c0, 0x14000166120?, {0x103563550?, 0x14001835920})
        /Users/myyrakle/go/pkg/mod/github.com/swaggo/swag@v1.8.6/generics.go:344 +0xf4
github.com/swaggo/swag.(*Parser).parseTypeExpr(0x140001660f0?, 0x1033b61c7?, {0x103563550?, 0x14001835920?}, 0x18?)
        /Users/myyrakle/go/pkg/mod/github.com/swaggo/swag@v1.8.6/parser.go:1192 +0x518

Expected behavior In my opinion, since generics are removed from preprocessing, they should behave the same as they would without generics. But it didn't work that way.

Of course, I know that the principle is different since this is generate before compilation. Is support for these features possible?

Screenshots image

Your swag version v1.8.6

Your go version go version go1.20.6 darwin/arm64

Desktop (please complete the following information):

Additional context

sdghchj commented 6 months ago

Try the latest version of swag.

myyrakle commented 6 months ago

Since I use the latest version, there is no panic, but it doesn't generate like an array.

I think this is probably intentional behavior from here on out. We will custom process the rest ourselves.

The issue is closed.