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

use swaggertype tag to supported custom type #201

Closed
lfaoro opened this issue Sep 3, 2018 · 4 comments
Closed

use swaggertype tag to supported custom type #201

lfaoro opened this issue Sep 3, 2018 · 4 comments
Labels

Comments

@lfaoro
Copy link

lfaoro commented Sep 3, 2018

Describe the bug
Sometimes swag init panics out of the blue, other times generates without error.

To Reproduce

$ make doc
cd srv && swag init -g main.go && cd ..
2018/09/03 15:36:00 Generate swagger docs....
2018/09/03 15:36:00 Generate general API Info
panic: assignment to entry in nil map

goroutine 1 [running]:
github.com/swaggo/swag.(*Operation).ParseParamComment(0xc42045a120, 0xc42002a34a, 0x31, 0xc4204e2b00, 0x7, 0x39)
        /Users/lfaoro/go/src/github.com/swaggo/swag/operation.go:150 +0x83e
github.com/swaggo/swag.(*Operation).ParseComment(0xc42045a120, 0xc42002a340, 0x3b, 0xc4204e2b00, 0x0, 0x0)
        /Users/lfaoro/go/src/github.com/swaggo/swag/operation.go:69 +0x3b8
github.com/swaggo/swag.(*Parser).ParseRouterAPIInfo(0xc4204cc5c0, 0xc4204e2b00)
        /Users/lfaoro/go/src/github.com/swaggo/swag/parser.go:297 +0x17b
github.com/swaggo/swag.(*Parser).ParseAPI(0xc4204cc5c0, 0x14b1da9, 0x2, 0x7ffeefbff7bd, 0x7)
        /Users/lfaoro/go/src/github.com/swaggo/swag/parser.go:89 +0x218
github.com/swaggo/swag/gen.(*Gen).Build(0xc420177988, 0x14b1da9, 0x2, 0x7ffeefbff7bd, 0x7, 0x14b52cc, 0xe, 0x14b395b, 0x9, 0x0, ...)
        /Users/lfaoro/go/src/github.com/swaggo/swag/gen/gen.go:29 +0x2cc
main.main.func1(0xc4200da2c0, 0xc4204e2100, 0xc4200da2c0)
        /Users/lfaoro/go/src/github.com/swaggo/swag/cmd/swag/main.go:33 +0x190
github.com/urfave/cli.HandleAction(0x1419020, 0x14cdec0, 0xc4200da2c0, 0x0, 0xc420084a20)
        /Users/lfaoro/go/src/github.com/urfave/cli/app.go:501 +0xc8
github.com/urfave/cli.Command.Run(0x14b23b6, 0x4, 0x0, 0x0, 0xc4203bc0f0, 0x1, 0x1, 0x14b534a, 0xe, 0x0, ...)
        /Users/lfaoro/go/src/github.com/urfave/cli/command.go:165 +0x47d
github.com/urfave/cli.(*App).Run(0xc42020a000, 0xc42001e180, 0x4, 0x4, 0x0, 0x0)
        /Users/lfaoro/go/src/github.com/urfave/cli/app.go:259 +0x6e8
main.main()
        /Users/lfaoro/go/src/github.com/swaggo/swag/cmd/swag/main.go:61 +0x4e2
make: *** [doc] Error 2

# lfaoro @ unidentifiedw in ~/go/src/gitlab.com/lfaoro/datacrib on git:master x [15:36:02] C:2
$ make doc
cd srv && swag init -g main.go && cd ..
2018/09/03 15:36:03 Generate swagger docs....
2018/09/03 15:36:03 Generate general API Info
2018/09/03 15:36:07 Generating mdl.Healthz
2018/09/03 15:36:07 Generating mdl.AccountPayload
2018/09/03 15:36:07 Generating mdl.Account
2018/09/03 15:36:07 create docs.go at  docs/docs.go

Expected behavior
Shouldn't panic but display an error

Your swag version
swag version v1.3.2

Additional context
Started doing this behavior after adding this line:
// @Success 201 {object} mdl.Account

type Account struct {
	ID            uuid.UUID  `json:"id" gorm:"primary_key,type:uuid"`
	CreatedAt     time.Time  `json:"created_at,omitempty"`
	UpdatedAt     time.Time  `json:"updated_at,omitempty"`
	DeletedAt     *time.Time `json:"-" sql:"index"`
	IPAddress     string     `json:"ip_address" valid:"ip"` // this validates ipv4 & ipv6
	Status        string     `json:"status" valid:"in(active|inactive)"`
	PaymentStatus string     `json:"payment_status,omitempty" valid:"in(paid|unpaid)"`
	CreditCard    CreditCard `json:"creditcard" valid:""`
	Tokens        int64      `json:"tokens"`
	Balance       int64      `json:"balance"`
	Mac           string     `json:"mac,omitempty"`
	AccountPayload
}
@elvizlai
Copy link
Contributor

same issue on v1.4.0

@psrebniak
Copy link

Problem exists when serialized struct has field with time.Time or strfmt.DateTime type.

Minimal code with reproduced error:

package test

import (
	"net/http"
	"time"
)

type DateTime time.Time

type CustomStructure struct {
	Begin DateTime `json:"begin,omitempty"`
}

// @title TEST API

// @Summary Test Endpoint
// @Success 200 {object} test.CustomStructure
// @Router /test [get]
func GetCustomStructure(w http.ResponseWriter, request *http.Request) {}

@rodrigodmd
Copy link

@psrebniak I had the same issue and was able to make it work using the "swaggertype" tag:

type DateTime time.Time

type CustomStructure struct {
	Begin DateTime `json:"begin,omitempty"    swaggertype:"primitive,string"`
}

That should help you to compile it as a string. Take a look at https://github.com/swaggo/swag/#override-swagger-type-of-a-struct-field

@easonlin404
Copy link
Member

@rodrigodmd ping. Thanks for your comments. Now have to use swaggertype tag to supported custom type declarations like:

type DateTime time.Time

we will add this feat if we have free time, and swag team welcome PR to enhance all of it.

@easonlin404 easonlin404 changed the title sporadic panic on swag init use swaggertype tag to supported custom type Mar 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants