go - How to configure swagger UI so it doesn't need the URL? - Stack Overflow
How to handle generic type by gin-swagger
Go + gRPC + GORM + SQLite + GIN + Swagger
Go swaggerfiles
Videos
Hi Friends
I am using gin-swagger to write comments in Go code and automatically generate Open API/Swagger documentation for the REST service exposed externally through these comments.
gin-swagger is just a top-level shell, and the actual work is done by another framework called swag, https://github.com/swaggo/swag/tree/master
A type is defined as follows:
```go
package middleware
type Response struct {
...other fields omitted...
data any
}
```
Add the following comments for the HTTP method in the Controller:
```go
// @Success 200 {object} middleware.Response
// @Success 500 {object} middleware.Response
```
After executing the `swag init`, the OpenAPI was correctly generated, although the result was not what I expected.
To get the expected result, I added a generic type argument to the Response struct:
```go
type Response[T any] struct {
...other fields omitted...
data T
}
```
Then I modified the comments in the HTTP function of the Controller:
```go
// @Success 200 {object} middleware.Response[int]
// @Success 500 {object} middleware.Response[string]
```
After executing the command again to regenerating the OpenAPI, an error occurred: "cannot find type definition".
The swag documentation clearly states that it supports Go generics, and the documentation for this feature can be found [here](https://github.com/swaggo/swag/tree/master?tab=readme-ov-file#how-to-use-generics)
As you can see, the framework explicitly states that it supports this feature, and the usage shown in its documentation is completely consistent with mine.
Finally, when searching for this issue, several issues occurred during the period when Go added generics, and the problems were caused by the users using versions of the frameworks that were too old. However, my version is the latest v1.16.3, which is far beyond the required version for this feature, yet the problem still persists.
Has anyone encountered this issue before?