go-swagger / go-swagger

Swagger 2.0 implementation for go
https://goswagger.io
Apache License 2.0
9.46k stars 1.25k forks source link

Unable to connect a go-swagger parameter to a route #2218

Open cbarlock opened 4 years ago

cbarlock commented 4 years ago

Problem statement

I have the following go-swagger annotation:

// swagger:parameters callbackReg
type registrationParms struct {
    // Description of callback.
    // in: query
    // required: true
    callback string
}

// OK indicates that the HTTP request was successful.
//
// swagger:response
type OK struct {
    responseCode int
}

// BadRequest indicates that there was an error in
// the HTTP request.
//
// swagger:response
type BadRequest struct {
    responseCode int
}

// swagger:route POST /eprouter/1.0/registration callbackReg
//
// Callback registration API.
//
// Registers a callback URL for unsolicited messages.  A callback is registered for a given
// combination of message type (alarm, metric) and message encoding (Protobuf, XML).
//
// Schemes: http, https
//
// Responses:
//   200: OK
//   400: BadRequest

I generate the swagger YAML with swagger generate spec -o docs/swagger.yaml -w eprouter. The resulting YAML does not include the query parameter. My understanding is that the identifier callbackReg should tie the parameter structure to the route. What am I doing wrong?

The generated YAML:

info: {}
paths:
  /eprouter/1.0/registration:
    post:
      description: |-
        Registers a callback URL for unsolicited messages.  A callback is registered for a given
        combination of message type (alarm, metric) and message encoding (Protobuf, XML).
      operationId: callbackReg
      responses:
        "200":
          $ref: '#/responses/OK'
        "400":
          $ref: '#/responses/BadRequest'
      schemes:
      - http
      - https
      summary: Callback registration API.
responses:
  BadRequest:
    description: |-
      BadRequest indicates that there was an error in
      the HTTP request.
    headers:
      responseCode:
        format: int64
        type: integer
  OK:
    description: OK indicates that the HTTP request was successful.
    headers:
      responseCode:
        format: int64
        type: integer
swagger: "2.0"

Environment

swagger version version: v0.22.0 commit: 5773cbe63c3f459b23ed73ad8b482389ddf46cb4

go version: 1.13.6 OS: MacOS

cbarlock commented 4 years ago

Looks like I stumbled on the answer. The values in the parameter structures have to be exported (Capitalized). Why is this? The parameter structures are defined in the same package as the routes.

atulkc commented 4 years ago

This seems restrictive. How do I include query parameters that do not start with capital letter in generated swagger spec? As per URI RFC other than scheme and host other components of URI are case sensitive.

https://tools.ietf.org/html/rfc3986#page-11

atulkc commented 4 years ago

Also, this seems to be a regression. Version v0.20.1 seems to be able to generate query parameters that are not exported/capitalized.

casualjim commented 4 years ago

This only refers to your go code. The json:"differentNameOrCapitalization" struct tag is used for naming purposes in the generated spec

atulkc commented 4 years ago

@casualjim Thanks! I somehow missed that it honors json tags as well when generating specs. Adding json tags solved the issue.