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

Don't working properly with generic instantiation of slice #1346

Closed
0x9ef opened this issue Oct 13, 2022 · 1 comment
Closed

Don't working properly with generic instantiation of slice #1346

0x9ef opened this issue Oct 13, 2022 · 1 comment
Labels

Comments

@0x9ef
Copy link

0x9ef commented Oct 13, 2022

Describe the bug
Don't working properly with parametric generics
panic: interface conversion: ast.Expr is *ast.ArrayType, not *ast.StructType

To Reproduce
Steps to reproduce the behavior:

  1. Create main.go with the next content (listed below)
  2. Run "swag init"
  3. See error

Content:
pgjson.go

package pgjson

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
	"fmt"
)

func Scan(dest interface{}, value interface{}) error {
	switch t := value.(type) {
	case string:
		return json.Unmarshal([]byte(t), dest)
	case []byte:
		return json.Unmarshal(t, dest)
	default:
		return errors.New(fmt.Sprint("failed to unmarshal JSON value:", value))
	}
}

func Value(src interface{}) (driver.Value, error) {
	value, err := json.Marshal(src)
	return string(json.RawMessage(value)), err
}

type Slice[T any] []T

func (j *Slice[any]) Scan(value interface{}) error {
	return Scan(j, value)
}

func (j Slice[any]) Value() (driver.Value, error) {
	if len(j) == 0 {
		return "[]", nil
	}
	return Value(j)
}

main.go

package main

import "main/pgjson"

type controller struct{}

type Destination struct {
	Images pgjson.Slice[string] `json:"images,omitempty"`
} // @name Destination

type doSomethingBody struct {
}

type doSomethingResponse struct {
	*Destination
}

// @ID           DoSomething.
// @Summary      Do something.
// @Accept       application/json
// @Produce      application/json
// @Param        fields body doSomethingBody true "data"
// @Success      200 {object} doSomethingResponse
// @Failure      422,500 {object} Error
// @Router       /doSomething [POST]
func (c *controller) doSomething() {}

func main() {
	c := &controller{}
	c.doSomething()
}

Expected behavior
Should generate docs correctly without any errors

Screenshots
image

Your swag version
latest (1.8.6)

Your go version
go version go1.18 windows/amd64

Desktop (please complete the following information):

  • OS: Windows 10
  • Windows Version: 10.0.19044 Build 19044
@0x9ef
Copy link
Author

0x9ef commented Oct 14, 2022

It can be fixed by adding swaggertype:"array,string" as a tag to structure field. But is there a way to implement it automatically by parsing generic instantiation type parameter?

@sdghchj sdghchj added the bug label Oct 14, 2022
sdghchj pushed a commit that referenced this issue Oct 14, 2022
Signed-off-by: sdghchj <chengjin@cmiot.chinamobile.com>
sdghchj added a commit that referenced this issue Oct 15, 2022
ubogdan pushed a commit that referenced this issue Oct 15, 2022
* fix issue #1346 about generics

Signed-off-by: sdghchj <chengjin@cmiot.chinamobile.com>

* fix issue #1346 about generics

Signed-off-by: sdghchj <chengjin@cmiot.chinamobile.com>
Co-authored-by: sdghchj <chengjin@cmiot.chinamobile.com>
@sdghchj sdghchj closed this as completed Dec 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants