fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
915 stars 201 forks source link

how to add example values to request body properties? #696

Closed gusparr28 closed 1 year ago

gusparr28 commented 1 year ago

Prerequisites

Issue

No response

Eomm commented 1 year ago

This question is totally unclear to me.

Maybe Add examples to the schema?

gusparr28 commented 1 year ago

Yup, that's what I'm asking for. I tried that solution before opening the issue but didn't work for me. I have this body:

body: {
    type: 'object',
    required: ['username', 'password', 'trustId'],
    properties: {
      username: {
        description: 'Username for login',
        type: 'string',
      },
      password: {
        description: 'Password for login',
        type: 'string',
      },
      trustId: {
        description: 'Device trust id',
        type: 'string',
      },
    },
    examples: [
      {
        username: '123456785',
      },
      {
        password: 'Testtest1!',
      },
      {
        trustId: v4(),
      },
    ],
  }

and I can't see the example values in my Swagger UI

Eomm commented 1 year ago

Could you add a Minimal, Reproducible Example?

Without it, we are unable to help you.

Uzlopak commented 1 year ago

You should use example and not examples property. https://swagger.io/docs/specification/adding-examples/

gusparr28 commented 1 year ago

You should use example and not examples property. https://swagger.io/docs/specification/adding-examples/

it looks like example property does not exist

Just tried these both and I got compiling error:

body: {
    type: 'object',
    required: ['username', 'password', 'trustId'],
    properties: {
      username: {
        description: 'Username for login',
        type: 'string',
      },
      password: {
        description: 'Password for login',
        type: 'string',
      },
      trustId: {
        description: 'Device trust id',
        type: 'string',
      },
    },
    example: {
      username: '123456785',
      password: 'Testtest1!',
      trustId: v4(),
    },
  }
  body: {
    type: 'object',
    required: ['username', 'password', 'trustId'],
    properties: {
      username: {
        description: 'Username for login',
        type: 'string',
      },
      password: {
        description: 'Password for login',
        type: 'string',
      },
      trustId: {
        description: 'Device trust id',
        type: 'string',
      },
    },
    example: [
      {
        username: '123456785',
      },
      {
        password: 'Testtest1!',
      },
      {
        trustId: v4(),
      },
    ],
  }
    err: {
      "type": "FastifyError",
      "message": "Failed building the validation schema for POST:, due to error strict mode: unknown keyword: \"example\"",      
      "stack":
          FastifyError: Failed building the validation schema for POST, due to error strict mode: unknown keyword: "example"    
      "name": "FastifyError",
      "code": "FST_ERR_SCH_VALIDATION_BUILD",
      "statusCode": 500
    }
Uzlopak commented 1 year ago

You could also try to put the example into each property.

gusparr28 commented 1 year ago

You could also try to put the example into each property.

it was my first try but didn't work, still got compiling error. In response body works like a charm, but in request body I don't find a way of how to add example values. Already thinking to change to static mode

climba03003 commented 1 year ago

Just curious, are you using Swagger 2.0 or OpenAPI 3.0. AFAIK, if you are using Swagger 2.0. There are NO example for body, only description.

I am closing this issue because I think it is a correct behavior. If you are using OpenAPI 3.0, then please provide a minimal reproducible code that we can run directly.