fastify / fastify-swagger

Swagger documentation generator for Fastify
MIT License
910 stars 200 forks source link

Automatically generate path params definition #750

Closed ivan-tymoshenko closed 10 months ago

ivan-tymoshenko commented 1 year ago

Prerequisites

Issue

It would be nice if @fastify/swagger would generate a default schema for path params.

  fastify.get('/example/*', async (request, reply) => {})

Something like this:

"/{param}": {
  "get": {
    "params": {
      "param": {
        "type": "string"
      }
    },
    "responses": {
      "200": {
        "description": "Default Response"
      }
    }
  }
}

Instead of this:

"/{param}": {
  "get": {
    "responses": {
      "200": {
        "description": "Default Response"
      }
    }
  }
}
Uzlopak commented 1 year ago

I am not sure.

If we do this, the swagger could be correct. But on the other hand, path params schema is not effected by this.

ivan-tymoshenko commented 1 year ago

Sorry, I don't undersand what do you mean

Uzlopak commented 1 year ago
fastify.get('/example/{param}', {schema: { params: {} /* what? */ } }, async (request, reply) => {})
ivan-tymoshenko commented 1 year ago

Oh, I see. Can we resolve it on a fastify level?

Uzlopak commented 1 year ago

idk. I would personally expect that schema gets validated against the path.

mcollina commented 1 year ago

@Uzlopak I don't understand

Uzlopak commented 1 year ago
const paramsJsonSchema = {
  type: 'object',
  required: [
    'par1', 'par2'
  ],
  properties: {
    par1: { type: 'string' },
    par2: { type: 'number' }
  },
  additionalProperties: false
}

fastify.get('/example/{par1}/{par2}', {schema: paramsJsonSchema }, async (request, reply) => {}) // OK
fastify.get('/example/{par1}/{par2}/{invalid}', {schema: paramsJsonSchema }, async (request, reply) => {}) // ERROR: Did not found 'invalid' in schema
fastify.get('/example/{par1}/{par3}', {schema: paramsJsonSchema }, async (request, reply) => {}) // ERROR: Missing parameter 'par2'
ivan-tymoshenko commented 1 year ago

I would say, let's generate default param schema only if user doesn't specify a schema for param at all. If there is something, even with missing params like in your example, we should not modify it.

Uzlopak commented 1 year ago

I agree.

mcollina commented 1 year ago

This would be amazing to have

msebastianb commented 11 months ago

I'd like to pick up this.

mcollina commented 11 months ago

go for it!

msebastianb commented 11 months ago

I think this https://github.com/fastify/fastify-swagger/pull/761 could be a good way. Let me know otherwise. I'm open to suggestions.