Skip to content

use swaggertype tag to supported custom type #201

Closed

Description

@lfaoro

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
}

Activity

  1. elvizlai commented on Sep 20, 2018

    @elvizlai
    Contributor

    same issue on v1.4.0

  2. psrebniak commented on Feb 6, 2019

    @psrebniak

    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) {}
    
  3. rodrigodmd commented on Mar 22, 2019

    @rodrigodmd

    @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

  4. easonlin404 commented on Mar 22, 2019

    @easonlin404
    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.

  5. changed the title [-]sporadic panic on `swag init`[/-] [+] use swaggertype tag to supported custom type[/+] on Mar 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址