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

Plugins must be imported at the top to propagate the types #4122

Closed
2 tasks done
mikicho opened this issue Jul 8, 2022 · 8 comments · Fixed by #4126
Closed
2 tasks done

Plugins must be imported at the top to propagate the types #4122

mikicho opened this issue Jul 8, 2022 · 8 comments · Fixed by #4126
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers

Comments

@mikicho
Copy link
Contributor

mikicho commented Jul 8, 2022

Prerequisites

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

Fastify version

3.29.1

Plugin version

6.1.0

Node.js version

16.15.0

Operating system

Linux

Operating system version (i.e. 20.04, 11.3, 10)

Debian 11.3

Description

typescript throw error when trying to add description or summary to endpoints:

fastify.post<{ Body: MyBody}>(
    '/generate',
    {
        schema: {
            description:  'my beautiful description', // <-- type error
            body: myBody,
        },
    },
    handler,
);

image

This is supported according to @fastify/swagger:
image

Also, if I'm ignore the error with // @ts-ignore it's working correctly and the swagger is valid

Steps to Reproduce

Almost identical to @fastify/swagger example.

  1. save the below code to a file (a.ts)
  2. run npx tsc --noEmit a.ts
import fastify from 'fastify';
const server = fastify();
(async () => {

await server.register(require('@fastify/swagger'), {
  routePrefix: '/documentation',
  swagger: {
    info: {
      title: 'Test swagger',
      description: 'Testing the Fastify swagger API',
      version: '0.1.0'
    },
    externalDocs: {
      url: 'https://swagger.io',
      description: 'Find more info here'
    },
    host: 'localhost',
    schemes: ['http'],
    consumes: ['application/json'],
    produces: ['application/json'],
    tags: [
      { name: 'user', description: 'User related end-points' },
      { name: 'code', description: 'Code related end-points' }
    ],
    definitions: {
      User: {
        type: 'object',
        required: ['id', 'email'],
        properties: {
          id: { type: 'string', format: 'uuid' },
          firstName: { type: 'string' },
          lastName: { type: 'string' },
          email: {type: 'string', format: 'email' }
        }
      }
    },
    securityDefinitions: {
      apiKey: {
        type: 'apiKey',
        name: 'apiKey',
        in: 'header'
      }
    }
  },
  uiConfig: {
    docExpansion: 'full',
    deepLinking: false
  },
  uiHooks: {
    onRequest: function (request, reply, next) { next() },
    preHandler: function (request, reply, next) { next() }
  },
  staticCSP: true,
  transformStaticCSP: (header) => header,
  exposeRoute: true
})

server.put('/some-route/:id', {
  schema: {
    description: 'post some data',
    tags: ['user', 'code'],
    summary: 'qwerty',
    params: {
      type: 'object',
      properties: {
        id: {
          type: 'string',
          description: 'user id'
        }
      }
    },
    body: {
      type: 'object',
      properties: {
        hello: { type: 'string' },
        obj: {
          type: 'object',
          properties: {
            some: { type: 'string' }
          }
        }
      }
    },
    response: {
      201: {
        description: 'Successful response',
        type: 'object',
        properties: {
          hello: { type: 'string' }
        }
      },
      default: {
        description: 'Default response',
        type: 'object',
        properties: {
          foo: { type: 'string' }
        }
      }
    },
    security: [
      {
        "apiKey": []
      }
    ]
  }
}, (req, reply) => {})

await server.ready()
server.swagger()

})();

Expected Behavior

no type error

@climba03003
Copy link
Member

climba03003 commented Jul 8, 2022

It is the problem of TypeScript itself.
Doing server.register(require('@fastify/swagger')) will not import the types augmentation properly.

Please import the package on top.

import FastifySwagger from '@fastify/swagger'

server.register(FastifySwagger)

@mcollina
Copy link
Member

mcollina commented Jul 9, 2022

@climba03003 have we documented that on our docs?

@mikicho
Copy link
Contributor Author

mikicho commented Jul 9, 2022

I can't find it even now. When I know what I'm looking for

@mcollina mcollina reopened this Jul 9, 2022
@mcollina mcollina added documentation Improvements or additions to documentation good first issue Good for newcomers labels Jul 9, 2022
@mcollina
Copy link
Member

mcollina commented Jul 9, 2022

This would be a great addition to our docs! Would you like to send a PR?

@mikicho
Copy link
Contributor Author

mikicho commented Jul 9, 2022

@mcollina sure. Do you think it should be in this repo or in fastify/swagger repo?

@mcollina
Copy link
Member

mcollina commented Jul 9, 2022

this repo, in the typescript docs

@mcollina mcollina changed the title Wrong type definitions for FastifySchema Plugins must be imported at the top to propagate the types Jul 9, 2022
@climba03003
Copy link
Member

Yes, we do not have document anything on it.
And think it is not even in TypeScript. But, it is how TypeScript works currently and the behavior will be vary on config or even after certain updates.

Are we necessary to document how TypeScript behave?

@mikicho mikicho mentioned this issue Jul 9, 2022
4 tasks
@mikicho
Copy link
Contributor Author

mikicho commented Jul 9, 2022

@climba03003 I agree, but the docs should tackle also pitfalls. we of course don't need to elaborate on the matters but may at least put a reference or link to the relevant information.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants