0

Below is my code:

// swagger:route GET /user_list get_user
//
// Get a user profile
// Parameters:
//  user_id: userParam
// Consumes:
// - application/json
// Produces:
// - application/json
//
// Responses:
//      200: UserResponse

In my model for request I have added param like below:

// swagger:parameters userParam
type UserRequest struct {
    // aaaa
    // in: query
    UserId string `json:"user_id"`
}

I am getting below error when I generate swagger json.

panic: assignment to entry in nil map

goroutine 1 [running]:
github.com/go-swagger/go-swagger/scan.(*setOpParams).Parse(0xc428986a20, 0xc426439f30, 0x1, 0x1, 0x0, 0x0)
    /home/sotsys-056/go_10/src/github.com/go-swagger/go-swagger/scan/route_params.go:137 +0x635
github.com/go-swagger/go-swagger/scan.(*tagParser).Parse(0xc426e873d0, 0xc426439f30, 0x1, 0x1, 0x0, 0xc426439f30)
    /home/sotsys-056/go_10/src/github.com/go-swagger/go-swagger/scan/scanner.go:526 +0x52

When I remove parameters section it works fine I am not generate swagger json but with parameters I am not able to generate it.

I have also tried these ways too.

// Parameters:
//      - user_id: [in:query required:true type:string] Description goes her

parameters:
        - name: user_id
          in: query
          description: this argument is a string
          schema:
            type: string
Sapna Mishra
  • 187
  • 1
  • 2
  • 15

1 Answers1

2

Your example is incomplete. However, using what you have, your code should look something along the lines of this in its simplest form:

// swagger:parameters GetUser
type GetUser struct {
    // UserId that identifies a user.
    //
    // in: query
    UserId string `json:"user_id"`
}

// Returns a user.
// swagger:response UserResponse
type UserResponse struct { 
    // in: body
    Body struct {}
}

// swagger:route GET /user_list GetUser
//
// Get a user profile.
//
// Responses:
//      200: UserResponse
router.Get("/user_list", handler.GetUser)

Notice how we have three things:

  1. The request object
  2. The response object
  3. The route that uses both the request and response object
Lansana Camara
  • 9,497
  • 11
  • 47
  • 87
  • can you please explain how I can define file type request param in above GetUser Struct? – Sapna Mishra Aug 16 '18 at 13:44
  • Okay @Lansana . I have added param in GetUser struct like below: // UserFile that identifies a user. // // in: formData // type: file UserFile multipart.File `json:"user_file"` which generates swagger json code like this { "$ref": "#/definitions/File", "x-go-name": "UserFile", "description": "UserFile that identifies a user.", "name": "user_file", "in": "formData" } – Sapna Mishra Aug 18 '18 at 06:01
  • Please update your answer with this code in its entirety. – Lansana Camara Aug 20 '18 at 15:46