Search code examples
gogo-swagger

Customize go-swagger generate spec


I am using go-swagger to generate spec with command swagger generate spec. Everything is working fine, however I want to mitigate possible mismatch between specs and actual implementation.

For example, the below model is having required : true in comment (for spec generation), as well as value:"required" for actual validation (e.g with govalidator)

    // Current model
    // swagger:parameters myAPI
    type Post struct {
        // the title
        // required : true
        Title   string `json:"Title" valid:"required"`

        // the message
        Message string `json:"Message"`
    }

Is there any way to customize go-swagger to parse valid:"required" and automatically add required:true in generated specs? I know that currently swagger generate spec is already checking json:"Tittle" to populate json field name Title. Any pointer will be appreciated. Thanks.


Solution

  • I checked go-swagger source code, and found out that in current implemented design, we can't do this. Basically, the scanning logic just checks the comment and JSON tag only.