Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add example to string parameter in request body #2064

Open
eric-lee-ltk opened this issue Sep 19, 2019 · 4 comments
Open

add example to string parameter in request body #2064

eric-lee-ltk opened this issue Sep 19, 2019 · 4 comments
Labels
generate spec Related to spec generation from code question

Comments

@eric-lee-ltk
Copy link

Problem statement

When I generate spec from go code, the example and default tag of string parameter in request body is missing

Swagger specification

expected:

{
            "description": "desc",
            "name": "Body",
            "in": "body",
            "required": true,
            "schema": {
              "type": "string",
              "example": "example"
            }
}

actual:

{
            "description": "desc",
            "name": "Body",
            "in": "body",
            "required": true,
            "schema": {
              "type": "string"
            }
}

Steps to reproduce

original code:

// swagger:parameters Service
type Req struct {
    // desc
    // in: body
    // required: true
    // example: example
    Body string
}

Environment

swagger version: v0.20.1
go version: 1.12.7
OS: CentOS 6

@fredbi fredbi added the generate spec Related to spec generation from code label Oct 1, 2019
@ilyakaznacheev
Copy link
Contributor

@eric-lee-ltk it works for me.

Go code

package main

// swagger:route GET /foobar foobar-tag Service

// swagger:parameters Service
type Req struct {
	// desc
	// in: body
	// required: true
	// example: example
	Body string
}

generated json:

{
  "swagger": "2.0",
  "paths": {
    "/foobar": {
      "get": {
        "tags": [
          "foobar-tag"
        ],
        "operationId": "Service",
        "parameters": [
          {
            "example": "example",
            "description": "desc",
            "name": "Body",
            "in": "body",
            "required": true,
            "schema": {
              "type": "string"
            }
          }
        ]
      }
    }
  }
}

What am I missing?

@eric-lee-ltk
Copy link
Author

eric-lee-ltk commented Oct 8, 2019

@ilyakaznacheev I just copy and paste your generated json to swagger ui .

There has a structural error: should NOT have additional properties additionalProperty: example and the value in example dialog is still string instead of example.
image

It seems that the example section should be part of schema section instead of parameters section. Following is a working result:
image

@fredbi fredbi added the question label Oct 8, 2019
@rashmi-tondare
Copy link

@eric-lee-ltk were you able to successfully add an example to a request body field? I'm still facing the same issue where the "example" field is wrongly getting added under parameters instead of schema. Or is there another way to set an example field?

@starsz
Copy link

starsz commented Dec 23, 2021

Hi, @eric-lee-ltk maybe you can try this.

package main

type Body struct {
    // example: example
    Fielld string `json:"field"`
}

// swagger:route GET /foobar foobar-tag Service

// swagger:parameters Service
type Req struct {
    // desc
    // in: body
    // required: true
    Body Body `json:",inline"`
}

It's look good to me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
generate spec Related to spec generation from code question
Projects
None yet
Development

No branches or pull requests

5 participants