swaggo / gin-swagger

gin middleware to automatically generate RESTful API documentation with Swagger 2.0.
MIT License
3.66k stars 266 forks source link

Cannot find type definition: dto.CreateCityRequest #292

Closed amiralitaheri-it closed 4 months ago

amiralitaheri-it commented 4 months ago

Hi, I'm encountering this error when trying to run the swag init command, it happens after I make a generic function inside handlers, the create function that I sent here is one example, before that change, everything was working fine, this is the init command:

swag init -g cmd/main.go --parseInternal --parseDependency

this is the city handler:

// CreateCity godoc
// @Summary Create a City
// @Description Create a City
// @Schemes
// @Tags Cities
// @Accept json
// @produces json
// @Param Request body dto.CreateCityRequest true "Create a City"
// @Success 201 {object} helper.BaseHttpResponse{result=dto.CityResponse} "City response"
// @Failure 400 {object} helper.BaseHttpResponse "Bad request"
// @Router /v1/cities/ [post]
// @Security AuthBearer
func (h *CityHandler) Create(c *gin.Context) {
    Create(c, h.service.Create)
}

this is that generic create function:

func Create[Ti any, To any](c *gin.Context, caller func(ctx context.Context, req *Ti) (*To, error)) {
    req := new(Ti)
    err := c.ShouldBindJSON(&req)
    if err != nil {
        c.AbortWithStatusJSON(http.StatusBadRequest,
            helper.GenerateBaseResponseWithValidationError(nil, false, constants.ValidationError, err))
        return
    }

    res, err := caller(c, req)
    if err != nil {
        c.AbortWithStatusJSON(helper.TranslateErrorToStatusCode(err),
            helper.GenerateBaseResponseWithError(nil, false, constants.InternalError, err))
        return
    }
    c.JSON(http.StatusCreated, helper.GenerateBaseResponse(res, true, 0))
}

errors:

2024/02/15 19:41:16 Generate swagger docs....
2024/02/15 19:41:16 Generate general API Info, search dir:./
2024/02/15 19:41:16 warning: failed to get package name in dir: ./, error: execute go list command, exit status 1, stdout:, stderr:no Go files in C:\Users\ALMAS\Desktop\Go Learning\golang-clean-web-api-project
2024/02/15 19:41:18 warning: failed to evaluate const mProfCycleWrap at C:\Program Files\Go\src\runtime\mprof.go:168:7, reflect: call of reflect.Value.Len on zero Value
2024/02/15 19:41:18 ParseComment error in file C:\Users\ALMAS\Desktop\Go Learning\golang-clean-web-api-project\api\handlers\cities.go :cannot find type definition: dto.CreateCityRequest
amiralitaheri-it commented 4 months ago

there was an issue with importing external packages, it has been resolved.