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

go generics cannot find common package object type definition #1237

Closed
Coderpipi opened this issue Jun 18, 2022 · 4 comments
Closed

go generics cannot find common package object type definition #1237

Coderpipi opened this issue Jun 18, 2022 · 4 comments

Comments

@Coderpipi
Copy link

Coderpipi commented Jun 18, 2022

Describe the bug

An error occurred while parsing a data structure that contains generics and the generic object field is another object and both objects are in the same package

ParseComment error in file problem.go :cannot find type definition: ProblemCategory

This is my response.go:

type R[T any] struct {
	Code int    `json:"code"`
	Msg  string `json:"msg"`
	Data T      `json:"data"`
}

This is my problem.go,it contains a field of type ProblemCategory Array:

package models

type Problem struct {
	gorm.Model
	Identity          string             `gorm:"column:identity;type:varchar(36);" json:"identity"`        
	Title             string             `gorm:"column:title;type:varchar(255);" json:"title" form:"title"`
	ProblemCategories []*ProblemCategory `gorm:"foreignKey:problem_id;references:id"`                      
	Content           string             `gorm:"column:content;type:text;" json:"content"`                 
	MaxRunTime        string             `gorm:"column:max_runtime;type:int(11);" json:"max_runtime"`      
	MaxMem            string             `gorm:"column:max_mem;type:int(11)" json:"max_mem"`               
	TestCases         []*TestCase        `gorm:"foreignKey:problem_identity;references:identity"`          
}

This is my problem_category.go:

package models

type ProblemCategory struct {
	gorm.Model
	ProblemId  uint      `gorm:"column:problem_id;type:int(11);" json:"problem_id"`  
	CategoryId uint      `gorm:"column:category_id;type:int(11);" json:"category_id"`
	Category   *Category `gorm:"foreignKey:id;references:category_id"`               
}

This is my comment:

// xx
// @Tags     xxx
// @Summary  xxx
// @Param    xxx
// @Success  200       {object}  response.R[models.Problem]
// @Router   /api/v1/xxx [get]
func Route() {
  
}

Your swag version
1.8.3

Your go version
1.18

Desktop (please complete the following information):

  • OS: macOS 11.6.3
@Coderpipi Coderpipi changed the title go generics cannot right parse go generics cannot find common package object type definition Jun 18, 2022
@Coderpipi Coderpipi reopened this Jun 18, 2022
@ubogdan
Copy link
Contributor

ubogdan commented Jun 18, 2022

@Coderpipi FYI ProblemCategories it's a slice in go terminology.

1.8.3 is the first version supporting basic generics parsing. That being said, it's acceptable to have this kind of issue.

@Coderpipi
Copy link
Author

@ubogdan but after my testing, it also gives errors like this, even though it's not a slice

@wiidz
Copy link

wiidz commented Jun 20, 2022

i have had the same puzzle , it really really confused me

@drewsilcock
Copy link

I've also been experiencing this issue and have been using this as a workaround:

type problemR response.R[models.Problem]

// xx
// @Tags     xxx
// @Summary  xxx
// @Param    xxx
// @Success  200       {object}  problemR
// @Router   /api/v1/xxx [get]
func Route() {
  
}

Swag doesn't properly parse this but instead gives the warning Type definition of type '*ast.IndexExpr' is not supported yet. Using 'object' instead.. Essentially it seems to fall back to thinking that problemR is an object which can contain anything. This allows you to at least generate the OpenAPI spec until the generics support is more stable in this package.

FabianMartin added a commit to FabianMartin/swag that referenced this issue Aug 4, 2022
- use ast.SelectorExpr instead if ast.Ident to embed types
- tests reworked

fixes swaggo#1237
FabianMartin added a commit to FabianMartin/swag that referenced this issue Aug 6, 2022
- Generate full type name, if only one generic field exists
- Generate refs to generic properties
- Tests extended to test override behavior

refs swaggo#1237
fixes swaggo#1253
@ubogdan ubogdan closed this as completed in af1c525 Aug 9, 2022
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

4 participants