seriousme / fastify-openapi-glue

A plugin for the Fastify webserver to autogenerate a Fastify configuration based on a OpenApi(v2/v3) specification.
MIT License
206 stars 33 forks source link

Discriminator keyword does not work ? #616

Closed JayPe69 closed 3 weeks ago

JayPe69 commented 3 weeks ago

Hello,

I'm trying to add a discriminator the my api schema. The swagger editor does not show me any error, but openapiglue does not seem to work with it :

{"type":"FastifyError","message":"Failed building the validation schema for POST: /api/v1/accounts/registration-from-web-app/:userUrn, due to error strict mode: unknown keyword: \"discriminator\""

requestBody sample :

        body1:
            required: true
            content:
                application/json:
                    schema:
                        oneOf:
                            - $ref: "#/components/schemas/schema1"
                            - $ref: "#/components/schemas/schema2"
                        discriminator:
                            propertyName: property1

Documentation : https://swagger.io/docs/specification/v3_0/data-models/inheritance-and-polymorphism/#discriminator

Any tips ? Thank you

seriousme commented 3 weeks ago

Hi,

thanks for asking. fastify-openapi-glue takes the OpenAPI schema, then tries to parse it en feeds the result to Fastify as configuration. Fastify then uses that configuration to validate any requests. Request validation in Fastify is handled by AJV (see https://github.com/fastify/fastify/blob/main/docs/Reference/Validation-and-Serialization.md). Digging a bit deeper I found that AJV does not support "discriminator" out of the box (see https://ajv.js.org/json-schema.html#discriminator).

You then have two options: a) remove the "discriminator" keyword from the schema b) add the "discriminator: true" to the AJV options (e.g. see https://fastify.dev/docs/latest/Reference/Server/#factory-ajv)

Hope this helps! Kind regards, Hans

JayPe69 commented 3 weeks ago

Hi @seriousme ,

Thanks a lot.

Well it seems discriminator will be deprecated in the version 4 of OpenAPI. With AJV, even by activating discriminator, mapping is not supported.

Thanks for your help and support