fastify-swagger: Example not working with TS project because of swagger-schema-official types bug

๐Ÿ› Bug Report

The example provided in the README.md doesnโ€™t work with typescript because Schema type doesnโ€™t have .

This is a swagger-schema-official types bug and i am writing it here just for reference.

To Reproduce

Just copy-paste the example in a TS project and you will get:

No overload matches this call.
  Overload 2 of 3, '(plugin: FastifyPluginAsync<SwaggerOptions, Server>, opts?: (RegisterOptions & FastifyStaticSwaggerOptions) | (RegisterOptions & FastifyDynamicSwaggerOptions) | (() => (RegisterOptions & FastifyStaticSwaggerOptions) | (RegisterOptions & FastifyDynamicSwaggerOptions)) | undefined): FastifyInstance<...> & PromiseLike<...>', gave the following error.
    Argument of type 'FastifyPlugin<SwaggerOptions>' is not assignable to parameter of type 'FastifyPluginAsync<SwaggerOptions, Server>'.
      Type 'FastifyPluginCallback<SwaggerOptions, Server>' is not assignable to type 'FastifyPluginAsync<SwaggerOptions, Server>'.

Digging more into this i found that leaving just one of the nullable fields displays the correct error. Object literal may only specify known properties, and 'nullable' does not exist in type 'Schema'

this is the Schema definition:

export type BaseSchema = {
  type?: ParameterType;
  format?: string;
  title?: string;
  description?: string;
  default?: any;
  multipleOf?: number;
  maximum?: number;
  exclusiveMaximum?: boolean;
  minimum?: number;
  exclusiveMinimum?: boolean;
  maxLength?: number;
  minLength?: number;
  pattern?: string;
  maxItems?: number;
  minItems?: number;
  uniqueItems?: boolean;
  maxProperties?: number;
  minProperties?: number;
  enum?: any[];
  items?: Schema | Schema[];
};

export interface Schema extends BaseSchema {
  $ref?: string;
  allOf?: Schema[];
  additionalProperties?: Schema | boolean;
  properties?: { [propertyName: string]: Schema };
  discriminator?: string;
  readOnly?: boolean;
  xml?: XML;
  externalDocs?: ExternalDocs;
  example?: any;
  required?: string[];
}
fastify.register(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', nullable: true },
          lastName: { type: 'string', nullable: true },
          email: { type: 'string', format: 'email' },
        },
      },
    },
    securityDefinitions: {
      apiKey: {
        type: 'apiKey',
        name: 'apiKey',
        in: 'header',
      },
    },
  },
  exposeRoute: true,
})

Expected behavior

No error at all.

fastify.register(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',
      },
    },
  },
  exposeRoute: true,
})

Your Environment

  • node version: 12
  • fastify version: >=3.0.0
  • os: macOS

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (15 by maintainers)

Most upvoted comments

@mcollina since nullable: true is only v3 spec and this module, as of today, implements only v2, I think it would be better to remove this from the example, since it makes the example itself worng.