You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have searched existing issues to ensure the bug has not already been reported
Fastify version
4.26.2
Plugin version
8.14.0
Node.js version
20
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.3
Description
When using custom keywords with AJV, those keywords are added to the resulting swagger/openAPI definition which results in a "structural error" being reported in the swagger editor.
Results in open api spec giving this validation error
Normally I wouldn't care about an error like this but we have teams attempting to generate Java clients using swagger generator and it throws an error because of this structure error.
Steps to Reproduce
Example
importFastifyfrom'fastify'importfastifySwaggerfrom'@fastify/swagger'// Any custom keywordconstevenKeyword: FuncKeywordDefinition={keyword: 'even',type: 'number',schemaType: 'boolean',compile: (schema,parentSchema)=>{return(data)=>{returndata%2===0}},}// Add our custom keyword to fastifyconstfastify=Fastify({logger: true,ajv: {customOptions: {keywords: [evenKeyword]}}})// Register @fastify/swaggerawaitfastify.register(fastifySwagger,{openapi: { ... }})// Define our schema using our custom keywordconstschema={body: {type: 'object',properties: {value: {type: 'number',even: true},},},}// Declare our route using schemafastify.post('/',{ schema },function(request,reply){reply.send({hello: 'world'})})// Run the server!fastify.listen({port: 3000},function(err,address){...})
The resulting swagger json then contains the custom keyword:
I would expect @fastify/swagger to generate valid openAPI spec regardless of AJV keywords used, but I understand this may be a difficult problem considering AJV keywords might need to be added which add valid openAPI keywords (because of openAPIs "extended" JSON Schema spec I think this is a non-trivial issue)
I'm thinking about solving it this way using the "transform" function to remove any invalid keywords, but this seems fragile and inefficient. Not sure if there is something better
awaitfastify.register(fastifySwagger,{openapi: {transform(transform){if(transform.schema&&!transform.schema.hide){// Deep clone original schema (unsure if necessary)transform.schema=structuredClone(transform.schema)// TODO do this in a more flexible waydeletetransform.schema.body.properties.value.even}returntransform},},})
The text was updated successfully, but these errors were encountered:
Prerequisites
Fastify version
4.26.2
Plugin version
8.14.0
Node.js version
20
Operating system
macOS
Operating system version (i.e. 20.04, 11.3, 10)
14.3
Description
When using custom keywords with AJV, those keywords are added to the resulting swagger/openAPI definition which results in a "structural error" being reported in the swagger editor.
Results in open api spec giving this validation error
Normally I wouldn't care about an error like this but we have teams attempting to generate Java clients using swagger generator and it throws an error because of this structure error.
Steps to Reproduce
Example
The resulting swagger json then contains the custom keyword:
Expected Behavior
I would expect @fastify/swagger to generate valid openAPI spec regardless of AJV keywords used, but I understand this may be a difficult problem considering AJV keywords might need to be added which add valid openAPI keywords (because of openAPIs "extended" JSON Schema spec I think this is a non-trivial issue)
I'm thinking about solving it this way using the "transform" function to remove any invalid keywords, but this seems fragile and inefficient. Not sure if there is something better
The text was updated successfully, but these errors were encountered: