Skip to content

The default tag in a struct field doesn't support an empty string #2052

@milanvthakor

Description

@milanvthakor

Describe the bug
When attempting to set the default value of a string type field to an empty string using the tag default:"", the swag command ignores this property during Swagger specification generation. This occurs even though an empty string is a valid default value according to the Swagger specification documentation.

To Reproduce
Steps to reproduce the behavior:

  1. Here is the sample code having the status field with an empty string as a default value.
package main

import (
	"fmt"
	docs "gin-swagger-example/docs"
	"net/http"

	"github.com/gin-gonic/gin"
	swaggerfiles "github.com/swaggo/files"
	ginSwagger "github.com/swaggo/gin-swagger"
)

// Pet example
type Pet struct {
	ID        int      `json:"id"`
	Name      string   `json:"name"`
	PhotoUrls []string `json:"photoUrls"`
	Status    string   `json:"status" default:""`
}

// @BasePath /api/v1

// SavePet example
//
//	@Summary		Save a new pet to the store
//	@Description	Save a new pet to the store
//	@ID				save-pet
//	@Accept			json
//	@Produce		json
//	@Param			pet	body		Pet			true	"Pet"
//	@Success		200		{string}	string			"ok"
//	@Failure		400		{object}	gin.H	"We need ID!!"
//	@Router			/example/save-pet [post]
func SavePet(c *gin.Context) {
	var pet Pet
	if err := c.ShouldBindJSON(&pet); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	fmt.Println("Request Pet", pet)
	c.JSON(http.StatusOK, gin.H{"message": "Pet saved successfully"})
}

func main() {
	r := gin.Default()
	docs.SwaggerInfo.BasePath = "/api/v1"
	v1 := r.Group("/api/v1")
	{
		v1.POST("/save-pet", SavePet)
	}
	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerfiles.Handler))
	r.Run(":8080")

}
  1. Generate the swagger specification using the command: swag init --pd.
  2. Check the generated swagger YAML or JSON files. Here is the output for the above code.
basePath: /api/v1
definitions:
  gin.H:
    additionalProperties: {}
    type: object
  main.Pet:
    properties:
      id:
        type: integer
      name:
        type: string
      photoUrls:
        items:
          type: string
        type: array
      status:
        type: string
    type: object
info:
  contact: {}
paths:
  /example/save-pet:
    post:
      consumes:
      - application/json
      description: Save a new pet to the store
      operationId: save-pet
      parameters:
      - description: Pet
        in: body
        name: pet
        required: true
        schema:
          $ref: '#/definitions/main.Pet'
      produces:
      - application/json
      responses:
        "200":
          description: ok
          schema:
            type: string
        "400":
          description: We need ID!!
          schema:
            $ref: '#/definitions/gin.H'
      summary: Save a new pet to the store
swagger: "2.0"

As can be seen above, the default value for the status property in the main.Pet definitions doesn't have a default tag set.

Expected behavior
The tool should handle the empty string as a default value and set it in the generated swagger specification doc. Here is the valid generated doc.

basePath: /api/v1
definitions:
  gin.H:
    additionalProperties: {}
    type: object
  main.Pet:
    properties:
      id:
        type: integer
      name:
        type: string
      photoUrls:
        items:
          type: string
        type: array
      status:
        default: ""
        type: string
    type: object
info:
  contact: {}
paths:
  /example/save-pet:
    post:
      consumes:
      - application/json
      description: Save a new pet to the store
      operationId: save-pet
      parameters:
      - description: Pet
        in: body
        name: pet
        required: true
        schema:
          $ref: '#/definitions/main.Pet'
      produces:
      - application/json
      responses:
        "200":
          description: ok
          schema:
            type: string
        "400":
          description: We need ID!!
          schema:
            $ref: '#/definitions/gin.H'
      summary: Save a new pet to the store
swagger: "2.0"

Your swag version
e.g. 1.16.6

Your go version
e.g. 1.24.0

Desktop (please complete the following information):

  • OS: Ubuntu 24.4.2 LTS
  • Browser: Brave
  • Version: 1.80.125

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions