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

Automatically generate path params definition #750

Closed
2 tasks done
ivan-tymoshenko opened this issue Aug 23, 2023 · 13 comments · Fixed by #761
Closed
2 tasks done

Automatically generate path params definition #750

ivan-tymoshenko opened this issue Aug 23, 2023 · 13 comments · Fixed by #761
Labels
feature request New feature to be added help wanted Help the community by contributing to this issue

Comments

@ivan-tymoshenko
Copy link
Member

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the issue has not already been raised

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
Copy link
Contributor

Uzlopak commented Aug 23, 2023

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
Copy link
Member Author

Sorry, I don't undersand what do you mean

@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 23, 2023

fastify.get('/example/{param}', {schema: { params: {} /* what? */ } }, async (request, reply) => {})

@ivan-tymoshenko
Copy link
Member Author

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

@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 23, 2023

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

@mcollina
Copy link
Member

@Uzlopak I don't understand

@Uzlopak
Copy link
Contributor

Uzlopak commented Aug 23, 2023

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
Copy link
Member Author

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
Copy link
Contributor

Uzlopak commented Aug 24, 2023

I agree.

@mcollina mcollina added help wanted Help the community by contributing to this issue feature request New feature to be added labels Aug 26, 2023
@mcollina
Copy link
Member

This would be amazing to have

@msebastianb
Copy link
Contributor

I'd like to pick up this.

@mcollina
Copy link
Member

mcollina commented Oct 5, 2023

go for it!

@msebastianb
Copy link
Contributor

I think this #761 could be a good way. Let me know otherwise. I'm open to suggestions.

@Eomm Eomm closed this as completed in #761 Oct 15, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature to be added help wanted Help the community by contributing to this issue
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants