Skip to content

Releases: samchungy/fastify-zod-openapi

v3.0.4

16 Nov 00:49
094e65c
Compare
Choose a tag to compare

What's Changed

Other Changes

Full Changelog: v3.0.3...v3.0.4

v3.0.3

10 Nov 06:02
8e3d8ed
Compare
Choose a tag to compare
  • Allow zod-openapi 4.0.0 in peer dependencies

Full Changelog: v3.0.2...v3.0.3

v3.0.2

04 Nov 12:10
a1a0100
Compare
Choose a tag to compare

What's Changed

Other Changes

New Contributors

Full Changelog: v3.0.1...v3.0.2

v3.0.1

31 Oct 13:36
177b8bd
Compare
Choose a tag to compare

What's Changed

Other Changes

Full Changelog: v3.0.0...v3.0.1

v3.0.0

30 Oct 10:59
5456d6b
Compare
Choose a tag to compare

What's Changed

Breaking Changes 🛠

This is a fairly big release, and hopefully should be the last breaking change in a little while. My aim with this library is to keep the functionality as stable as possible with as little change as possible. This release brings us to a place which is more closely aligned to the rest of the Fastify ecosystem, which should mean less breaking changes in the future.

Peer Dependencies

  • The @fastify/swagger peer dependency range has been restricted to <9.2.0 due to a bug in that version which can cause Fastify to crash. This restriction will be lifted once the issue is patched.
  • zod-openapi peer dependency requirement has been bumped to ^3.1.1. This enables us to use any CreateDocumentOptions the downstream library may introduce in the future.

Features

  • Add Fast Json Stringify by @samchungy in #179

    This introduces fast-json-stringify which is used natively within Fastify and replaces the default JSON.stringify() serialization. This should result in a performance uplift in your server. If you have any benchmarks, feel free to share them back here.

    This should hopefully just "work" for most users, unless you are manually specifying components within the plugin options, however, I strongly recommend testing it in your own applications in case there are any edge cases in serialization. If you run into any issues, please create an issue

    Migration

    If you only use the ref auto-registering feature you can ignore this section.

    The migration is fairly simple, just pass the components you pass into the plugin also into the new createCustomSerializerCompiler function and pass that result to app.setSerializerCompiler.

    const components: ZodOpenApiComponentsObject = { schemas: { mySchema } };
    await app.register(fastifyZodOpenApiPlugin, {
      components,
    });
    // Create a custom serializer
    const customSerializerCompiler = createSerializerCompiler({
      components,
    });
    app.setSerializerCompiler(customSerializerCompiler);

    You can also return to the pre-existing JSON.stringify() serialization by passing it into the createCustomSerializerCompiler function.

    const customSerializerCompiler = createSerializerCompiler({ stringify: JSON.stringify });
  • Add Fastify Error Handling by @samchungy in #175

    This blatantly copies the Error Implementation from fastify-type-provider-zod but adds its own twist.

    Migration
    Request Errors

    Previously, we exported a ValidationError class which was used for both request and response serialization errors. This worked fairly differently to the native Fastify implementation.

    We now export a ValidationRequestError and utilise Fastify's native error formatting to output errors. Instead of logging the entire ZodError in the message, we now emit errors request errors in the following format:

    {
      "code": "FST_ERR_VALIDATION",
      "error": "Bad Request",
      "message": "params/jobId Expected number, received string",
      "statusCode": 400
    }
    Response Errors

    For errors, we now emit a ResponseSerializationError. Previously, when a response serialization error occurred, we would previously leak the implementation details by emitting the ZodError results in the message of the 500 error. This has been changed and by default we will emit the following error:

    {
      "code": "FST_ERR_RESPONSE_SERIALIZATION",
      "error": "Internal Server Error",
      "message": "Response does not match the schema",
      "statusCode": 500
    }

    You can view the documentation on how you can handle and customize these errors.

Fixes

  • Remove the need to double pass the openapi version.

    Previously, if you wanted to use an alternate openapi version, you needed to pass the openapi version twice into different plugins.

    const openapi: ZodOpenApiVersion = '3.0.3';
    await app.register(fastifyZodOpenApiPlugin, { openapi });
    await app.register(fastifySwagger, {
      openapi: {
        info: {
          title: 'hello world',
          version: '1.0.0',
        },
        openapi,
      },
    }

    We have changed the implementation to only require you to pass it into the fastify-swagger plugin.

    await app.register(fastifyZodOpenApiPlugin);
    await app.register(fastifySwagger, {
      openapi: {
        info: {
          title: 'hello world',
          version: '1.0.0',
        },
        openapi: '3.0.3' satisfies ZodOpenApiVersion,
      },
    }

New Contributors

Full Changelog: v2.0.0...v3.0.0

v2.0.0

20 Sep 03:57
97aadda
Compare
Choose a tag to compare

What's Changed

Breaking Changes 🛠

  • Support Fastify 5 by @dmuharemagic in #155

  • Bump zod-openapi requirement to 3.0.1 by @samchungy in #156

    This release sets the required Node version to 20 and bumps all the underlying peerDependency version requirements to the following:

    "@fastify/swagger": "^9.0.0",
    "@fastify/swagger-ui": "^5.0.1",
    "fastify": "5",
    "zod-openapi": "^3.0.1"

    The Fastify update required a few type signature changes but there should be hopefully minimal updates you need to apply to your code.

New Contributors

Full Changelog: v1.3.0...v2.0.0

v1.3.0

02 Sep 06:28
03e5d1f
Compare
Choose a tag to compare

What's Changed

  • Fix types to work with @fastify/swagger 8.15.0
  • Fix types for compatibility with node16 and bundler tsconfig options

Full Changelog: v1.2.1...v1.3.0

v1.2.1

10 Jun 23:38
d607f48
Compare
Choose a tag to compare

What's Changed

Other Changes

Full Changelog: v1.2.0...v1.2.1

v1.2.0

07 Mar 10:38
fee2624
Compare
Choose a tag to compare

What's Changed

New Features 🎉

  • Add Fastify Plugin Signatures by @samchungy, @Puppo in #99

    This allows you to declare your routes with more modularity eg.

    const plugin: FastifyPluginAsyncZodOpenApi = async (fastify, _opts) => {
      fastify.route({
        method: 'POST',
        url: '/',
        // Define your schema
        schema: {
          body: z.object({
            jobId: z.string().openapi({
              description: 'Job ID',
              example: '60002023',
            }),
          }),
          response: {
            201: z.object({
              jobId: z.string().openapi({
                description: 'Job ID',
                example: '60002023',
              }),
            }),
          },
        } satisfies FastifyZodOpenApiSchema,
        handler: async (req, res) => {
          await res.send({ jobId: req.body.jobId });
        },
      });
    };
    app.register(plugin);

Full Changelog: v1.1.0...v1.2.0

v1.1.0

20 Feb 02:22
574aea8
Compare
Choose a tag to compare

What's Changed

New Features 🎉

Other Changes

Full Changelog: v1.0.2...v1.1.0