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

How to show response examples? #181

Closed
qortex opened this issue Jan 13, 2019 · 7 comments
Closed

How to show response examples? #181

qortex opened this issue Jan 13, 2019 · 7 comments

Comments

@qortex
Copy link

qortex commented Jan 13, 2019

I can't find how to make the response samples appear in the generated output.

My Open API file is:

"v1/user_profiles/{id}": {
      "get": {
        "tags": [
          "User profile"
        ],
        "summary": "Get user profile",
        "description": "Gets all the user profile. The user needs to be authorized for this operation (own profile or admin).",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "path",
            "description": "",
            "required": true,
            "type": "integer"
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "description": "",
              "type": "object",
              "properties": {
              }
            },
            "headers": {
            },
            "examples": {
              "application/json": {
                "id": 1,
                "firstname": "Otis",
                "lastname": "Kertzmann",
                "user_id": 1
              }
            }
          }
        },
        "deprecated": false,
        "security": [

        ]
      }

So the example is in there. But when I build with spectacle, it does not show in the output. I understand only the schema shows up (in this case nothing, so can't see the returned output).

Thanks for the great work!

@michaelschofield
Copy link

Seconding! I'm not sure how to generate these.

@elyesbenabdelkader
Copy link
Contributor

Replace "examples" by "example", but your Open API will become invalid according to the specs.

@qortex
Copy link
Author

qortex commented Mar 27, 2019

Thanks I'll try.
Any chances to do a small update in the code to get the examples taken into account with "examples" ? (My spec is auto-generated from the unit tests of my API.)

@qortex
Copy link
Author

qortex commented Mar 27, 2019

Maybe just there, but I'm not so familiar with the code:

if (value.example) {

@auscaster
Copy link
Member

Hey guys, we now support the single example and multiple examples for rendering response examples.

@qortex
Copy link
Author

qortex commented Jul 20, 2019

Thanks @auscaster and @elyesbenabdelkader for the fix! Didn't have a chance to try again until now, and it is not fixed for me it seems :(

My spec is:

"examples": {
  "application/json": {
    "access_token": "c3f23c2f8ae08a4c1f6889d0b3ec0ddb9d0f9d493def645d6c550ce794c5437d",
    "token_type": "Bearer",
    "expires_in": 7200,
    "refresh_token": "0618ae5e5ce0389ffa9f3304ddea2c12bb6841580d6f6b953acea3e2e7a93bea",
    "created_at": 1563633247
  }
}

And the output is:
image

Is there a flag I am missing or something?

Note: in the merge referred above, you say you want to keep the example backward compatibility, but it seems to me it didn't make it to the final merge :)

@qortex
Copy link
Author

qortex commented Jul 20, 2019

To be more precise, the spec is as below, and the example strings are still taken from the schema instead of the examples field. Can't figure out why looking at the source code in the merge, not familiar enough with the codebase :(

"v1/oauth/token": {
      "post": {
        "tags": [
          "Tokens"
        ],
        "summary": "Login",
        "description": "",
        "consumes": [
          "application/json"
        ],
        "produces": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "username",
            "in": "formData",
            "description": "User email (login)\nEg, `asha@example.com`",
            "required": true,
            "type": "string"
          },
          {
            "name": "password",
            "in": "formData",
            "description": "Plain password\nEg, `$^#@$3Br62946`",
            "required": true,
            "type": "string"
          },
          {
            "name": "grant_type",
            "in": "formData",
            "description": "Always set to 'password'\nEg, `password`",
            "required": true,
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "Success",
            "schema": {
              "description": "",
              "type": "object",
              "properties": {
                "access_token": {
                  "type": "string",
                  "description": "Token to be used by user in each request to authentify"
                },
                "token_type": {
                  "type": "string",
                  "example": "Bearer",
                  "description": "Token type"
                },
                "expires_in": {
                  "type": "string",
                  "description": "Expiration time (in seconds)"
                },
                "refresh_token": {
                  "type": "string",
                  "description": "Token to be used to request a new token when this one is outdated"
                },
                "created_at": {
                  "type": "string",
                  "description": "Created at"
                }
              }
            },
            "headers": {
            },
            "examples": {
              "application/json": {
                "access_token": "3f58a3fa86101c641338e17257995449ca8ec478038442d096079d0072181c63",
                "token_type": "Bearer",
                "expires_in": 7200,
                "refresh_token": "339ab3db384532cef43dcf8b3b114250713441171592cca96673c9631724ab4f",
                "created_at": 1563635790
              }
            }
          },
          "401": {
            "description": "Fail / Wrong password",
            "schema": {
              "description": "",
              "type": "object",
              "properties": {
              }
            },
            "headers": {
              "WWW-Authenticate": {
                "description": "",
                "type": "string",
                "x-example-value": "Bearer realm=\"Doorkeeper\", error=\"invalid_grant\", error_description=\"The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client.\""
              }
            },
            "examples": {
              "application/json": {
                "error": "invalid_grant",
                "error_description": "The provided authorization grant is invalid, expired, revoked, does not match the redirection URI used in the authorization request, or was issued to another client."
              }
            }
          }
        },
        "deprecated": false,
        "security": [

        ]
      }
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants