node.jsfastifyfastify-swagger

fastify-openapi-glue FastifyError: ... strict mode: unknown keyword: "example"


I have upgraded from Fastify v3 to v4, which I use to serve mock apis based on OpenAPI/swagger by using the plugin fastify-openapi-glue.

Since the upgrade, I receive the error FastifyError [Error]: Failed building the validation schema for POST: /contract, due to error strict mode: unknown keyword: "example" which is, because there is no example in the schema and openapi-glue documentation suggests to disable ajv strict mode (also disabling strictSchema should be sufficient in that case).

With the switch from Fastify 3 to 4 these options need to go into the Fastify options, which I did. Additionally added them to our own plugin, which is another place where it can be defined. Still it seems the options are being ignored, and I do not know why.

I created a reproduction with stackblitz (awesome btw :D, nodejs in the browser) stackblitz:fastify4-upgrade-openapi-glue-issue?file=plugins%2Fswagger.js%3AL28

Install dependencies with npm install and then run it with npm start


Solution

  • You must provide the --options flag to fastify-cli or it will not load the config.

    So change the package.json

    {
      "start": "fastify start --options app.js",
    }
    

    And the ajv.customOptions stuff goes on app.js only.