swaggo / swag

Automatically generate RESTful API documentation with Swagger 2.0 for Go.
MIT License
10.45k stars 1.19k forks source link

.swaggo not replacing null.Int #1208

Open smatric opened 2 years ago

smatric commented 2 years ago

I am unable to override null.Int in .swaggo file.

handler.go:

import "gopkg.in/guregu/null.v4"

type params struct {
    FStyleId       null.Int   `json:"f-styleId" form:"f-styleId" validate:"gte=0"`

I tried running swag init with any of the lines below in .swaggo and none of them worked (I also tried "integer" instead of "int"):

replace gopkg.in/guregu/null.v4@v4.0.0/null.Int int
replace gopkg.in/guregu/null.v4@v4.0.0.null.Int int
replace gopkg.in/guregu/null.v4/null.Int int
replace gopkg.in/guregu/null.v4.null.Int int
replace null.Int int

output:

swag init -g api.go --outputTypes "go,json" --overridesFile .swaggo
2022/05/17 13:55:00 Using overrides from .swaggo
2022/05/17 13:55:00 Generate swagger docs....
...
2022/05/17 13:55:00 ParseComment error in file .../api/handlers/wheels/handler.go :cannot find type definition: null.Int
make: *** [swag] Error 1

Swag version: 1.8.1 Go version: go1.17.5 darwin/arm64 OS: macOS 12.3.1

victorien-a commented 2 years ago

Exactly the same issue for me. There is no way to specify a rule

ubogdan commented 2 years ago

We release https://github.com/swaggo/swag/pull/1209 in 1.8.4

C0Nd3Mnd commented 2 years ago

Not sure if I'm doing something wrong, but it still doesn't work for me with 1.8.4. Swagger still shows null.String like this in the example value:

{
  "title": {
    "string": "string",
    "valid": true
  },
}

What it should look like:

{
  "title": "string"
}

I tried all variations listed above in the .swaggo file:

replace null.String string
replace gopkg.in/guregu/null.v4/null.String string
replace gopkg.in/guregu/null.v4@v4.0.0/null.String string
replace gopkg.in/guregu/null.v4.null.String string
replace gopkg.in/guregu/null.v4@v4.0.0.null.String string

The struct looks like this (other fields omitted):

type Employee struct {
    Title          null.String `db:"title" json:"title"`
}
smatric commented 2 years ago

I updated the module to 1.8.4 and running the command didn't work either. I had to recompile the executable:

$ go get -u github.com/swaggo/swag/cmd/swag
$ go install github.com/swaggo/swag/cmd/swag@latest

Then running the following command worked for me:

swag init -g api.go --outputTypes "go,json" --overridesFile docs/.swaggo

My .swaggo file:

replace null.String string

(The first line in yours looks fine.)

My struct:

type params struct {
    Test          null.String `json:"test" form:"test" validate:"gte=-999,lte=999"`
    // other fields
}
C0Nd3Mnd commented 2 years ago

Interesting. I'll try this tomorrow! Thanks for sharing.

C0Nd3Mnd commented 2 years ago

Works just fine, thanks again.

Makes sense you have to update the CLI as well, oversight on my part.