-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Support null
Package and Data-Types
#985
Comments
Try instead of import "gopkg.in/guregu/null.v4"
type Request struct {
Email null.String
IsAdmin null.Bool
} this one import null "gopkg.in/guregu/null.v4"
type Request struct {
Email null.String
IsAdmin null.Bool
} |
@naftulikay, have you figured it out? |
@SmartPhoneJava why would changing the imported package name work? Is there some logic within swaggo that will understand it if I force the package name? The code above does compile. @ubogdan I haven't tested this yet because I don't see how it would work differently. I'll attempt it at some point. Can you link to code or documentation that covers how this works? |
@naftulikay If the suggested workaround works it means there is an issue in the swag parser related to Please test and let us know. |
@naftulikay swaggo has different strategies to parse named and unnamed imports for detecting imported types. You can see it in https://github.com/swaggo/swag/blob/master/packages.go#L245 I have an assumption that in the case of an unnamed import, swaggo uses the part after the slash as the package name. For Check whether the import naming will work and everything will become clear :) |
I tried it out, and no dice: pkg/routes/login/root.go: package login
import (
// ...
null "gopkg.in/guregu/null.v4"
)
// LoginHandler godoc
// @Summary Login
// @Tags auth
// @Description Log a user in using email and password.
// @Accept json
// @Param login body views.LoginRequest true "Login Credentials"
// @Produce json
// @Success 200 {object} views.Response{data=views.LoginResponse}
// @Failure 400 {object} views.Response
// @Failure 403 {object} views.Response
// @Router /login [post]
func LoginHandler(w http.ResponsWriter, r *http.Request) {
// ...
resp := views.LoginRequest {
// ...
}
if err := json.NewEncoder(w).Encode(resp); err != nil {
// ...
}
} pkg/views/root.go: package views
import (
// ...
null "gopkg.in/guregu/null.v4"
)
type Response struct {
StatusCode uint16 `json:"status_code"`
Error null.String `json:"error"`
Data interface{} `json:"data"`
Links ResponseLinks `json:"_links"`
}
type ResponseLinks struct {
Self string `json:"_self"`
Prev null.String `json:"_prev,omitempty"`
Next null.String `json:"_next,omitempty"`
} If I add @ubogdan @SmartPhoneJava is there anything else I should try? |
@naftulikay Thanks for letting us know about the issue. A quick dirty workaround is to use "swaggertype" to override the value with a primitive as you described above. |
@ubogdan Is there a swagger struct field tag that I can use to mark a struct field as optional? I'm using |
@ubogdan ping on this: is there a tag that I can use to let Swagger know the field can be null? |
You can use |
Is there a |
Hi, I am getting those structs from https://github.com/volatiletech/sqlboiler, so I cannot really add those tags or the way it imports null. Is there a solution to this? |
Hello, I also meet a similar problem, and here is my code: // api comment
// @Param data body api.Problem true "comment" import "mime/multipart"
type Problem struct {
Input *multipart.FileHeader `form:"input"`
Output *multipart.FileHeader `form:"output"`
} And when I run ParseComment error in file :cannot find type definition: multipart.FileHeader So the only solution now is to use |
Describe the bug
swag init
fails when compiling models which contain structs from thegopkg.in/guregu/null.v4
package.To Reproduce
I have a
PATCH
endpoint where every field is optional, so my struct looks like this:Having marked up my endpoint like so:
When I run
swag init
, it chokes on items in thegopkg.in/guregu/null.v4
package:Expected behavior
I expected for
swag
to understand thatnull.*
are simply nullable types.Screenshots
If applicable, add screenshots to help explain your problem.
Your swag version
1.7.1
Your go version
Desktop (please complete the following information):
Additional context
If I shouldn't be using the
null.*
times for optional values in models, what should I be using?The text was updated successfully, but these errors were encountered: