ajv-validator / ajv

The fastest JSON schema Validator. Supports JSON Schema draft-04/06/07/2019-09/2020-12 and JSON Type Definition (RFC8927)
https://ajv.js.org
MIT License
13.79k stars 875 forks source link

OneOf property is failing during OAS Schema Validation #2349

Open RPS044 opened 10 months ago

RPS044 commented 10 months ago

Hello Team,

Requesting you to please check this issue and help. Performing schema validation with oneOf, discriminator and mapping property in oas3.0.

Error: { "fault": { "faultstring": "OASValidation MAM-EXT-SpecValidation with resource "oas://openapi.yaml": failed with reason: "[ERROR - An error occurred during schema validation - com.google.common.util.concurrent.UncheckedExecutionException: java.lang.NullPointerException.: []]"", "detail": { "errorcode": "steps.oasvalidation.Failed" } } }

yaml file looks like: image

image

json payload looks like:

--data '{ "billableHeader": { "sourceSystem": "UPSTREAM_APP", "sourceTransactionType": "INVOICE", "eventType": "BATCH", "messageCreationDatetime": "20220811140203", "timeZoneCode": "GMT", "isBulkProcessing": true, "product": "Airlcl" },

mefellows commented 10 months ago

According to the docs, mapping is not supported: https://ajv.js.org/json-schema.html#discriminator.

It's unclear clear why that's the case. If the author is open to a PR perhaps you could add the feature?

mefellows commented 10 months ago

It looks like somebody has already attempted that: https://github.com/ajv-validator/ajv/pull/2262/files

RPS044 commented 10 months ago

@mefellows then only with oneOf with discriminator is possible?

mefellows commented 10 months ago

I don't think mapping is supported at all.

RPS044 commented 10 months ago

@mefellows alternated is then using extract policy validation?

mefellows commented 10 months ago

I'm not sure what that means, sorry, but to work around it you can not using the mapping. See https://swagger.io/specification/v3/#discriminator-object for how it works without mapping.

epoberezkin commented 8 months ago

OpenAPI defines discriminator as tooling support and no-op from validation point of view - actual validation happens as defined in oneOf. Mapping may lead to contradictory schema, so is not supported. You can add it as a custom keyword to be ignored and allowed in schemas. Also, you have to use discriminator option.

mefellows commented 8 months ago

Thanks for your response @epoberezkin!

I'm still a little confused as to why we wouldn't want to support this feature. I appreciate it's not currently supported and it's a limitation (as you had documented already).

OpenAPI defines discriminator as tooling support and no-op from validation point of view - actual validation happens as defined in oneOf

That's not how I read the spec: https://spec.openapis.org/oas/v3.0.3#discriminator-object. I'm not all that familiar with spec reading and interpretation, so perhaps I'm mistaken. Or should I be looking at the JSON Schema dialect for this?

Mapping may lead to contradictory schema, so is not supported.

Is this something ajv must consider in features? Isn't it already possible to create contradictory schemas without mapping already? For example the following is illogical:

{
  "allOf": [
    { "type": "string" },
    { "type": "number" }
  ]
}

You can add it as a custom keyword to be ignored and allowed in schemas. Also, you have to use discriminator option.

This will have the effect of ignoring the mapping, correct? I'm looking for a solution that would include incorporating the entire mapping and discriminator vocabulary into the parsing of the document. If you know of a workaround, I would be very grateful to hear of it.