I'm using Swaggo to document my Go API endpoints. I have a handler function where I've defined a struct with example tags in the JSON struct fields, but these examples aren't appearing in the generated Swagger UI.
// @Summary Register a new user
// @Description Creates a new user account. In development environment, the user is activated immediately. Otherwise, an activation email is sent.
// @Tags users
// @Accept json
// @Produce json
// @Param request body main.application.registerUserHandler.input true "User registration details"
// @Success 202 {object} object{user=data.User}
// @Router /users [post]
func (app *application) registerUserHandler(w http.ResponseWriter, r *http.Request) {
var input struct {
Name string `json:"name" example:"John Doe"`
Email string `json:"email" example:"john.doe@example.com"`
Password string `json:"password" example:"SecurePass123!"`
}
// ... rest of the handler ...
}
The Swagger documentation generates correctly, but the example values from the example tags in the struct aren't showing up in the request body schema.
Is there a way to make Swaggo recognize the example tags in nested/inline structs?
Define a new type instead of an anonymous struct:
// @Summary Register a new user
// @Description Creates a new user account. In development environment, the user is activated immediately. Otherwise, an activation email is sent.
// @Tags users
// @Accept json
// @Produce json
// @Param request body main.application.registerUserHandler.input true "User registration details"
// @Success 202 {object} object{user=data.User}
// @Router /users [post]
func (app *application) registerUserHandler(w http.ResponseWriter, r *http.Request) {
type input struct {
Name string `json:"name" example:"John Doe"`
Email string `json:"email" example:"john.doe@example.com"`
Password string `json:"password" example:"SecurePass123!"`
}