OpenAPI-Specification icon indicating copy to clipboard operation
OpenAPI-Specification copied to clipboard

OpenAPI 3.1 examples not validating through AJV using OpenAPI 3.1 spec

Open essential-randomness opened this issue 4 years ago • 14 comments

Hello, there is a lot of JavaScript tooling that uses AJV to validate OpenAPI specs. None of it seems to support 3.1, which is really unfortunate. I was trying to remedy the situation, but failed miserably.

As far as I can tell (see https://github.com/ajv-validator/ajv/issues/1745) even the examples in your OpenAPI3.1 folder do not pass validation. The AJV folks (understandably) won't help unless I create a minimal reproducible example of a failing schema, which I'm not really qualified to do.

Would you be able to help/shed light on what's going on?

Copying the relevant details:

Ajv options object

{
    allErrors: true, 
    validateFormats: false, 
    strictSchema: false, 
    strictTypes: false
}

See OAI/OpenAPI-Specification#2489 for a discussion on the options.

JSON Schema

The OpenAPI 3.1 schema defined here: https://github.com/OAI/OpenAPI-Specification/blob/main/schemas/v3.1/schema.json

Sample data

the OpenApi v3.1 webhook example here: https://github.com/OAI/OpenAPI-Specification/blob/main/examples/v3.1/webhook-example.json

Your code

See: https://runkit.com/essential-randomness/612d87df07ae0a00092a3492.

This simply call ajv with the given options, passes the OpenApiv3.1 schema and tries to validate the OpenApi Webhook example.

Validation result, data AFTER validation, error messages

Error message:

Invalid: 
data/webhooks/newPet/post/requestBody/content/application~1json/schema must NOT have unevaluated properties, 
data/webhooks/newPet/post/requestBody must match "else" schema,
data/webhooks/newPet must match "else" schema, 
data/components/schemas/Pet must NOT have unevaluated properties, 
data/components/schemas/Pet must NOT have unevaluated properties


Thank you! Hopefully the errors are more understandable if you can read schemas :)

essential-randomness avatar Aug 31 '21 22:08 essential-randomness

This is a bug in ajv related to $dynamicRef. Here's your minimal reproduction.

schema

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",

  "type": "object",
  "properties": {
    "schema": { "$dynamicRef": "#meta" }
  },
  "unevaluatedProperties": false,

  "$defs": {
    "schema": {
      "$dynamicAnchor": "meta",
      "type": ["object", "boolean"]
    }
  }
}

instance

{
  "schema": { "type": "string" }
}

It appears that the $dynamicRef is resolving to the root of the schema rather than /$defs/schema where the $dynamicAnchor is located.

jdesrosiers avatar Sep 01 '21 18:09 jdesrosiers

Thank you so much for you help! Will follow up with them and hopefully we can get 3.1 working :)

essential-randomness avatar Sep 01 '21 18:09 essential-randomness

@essential-randomness can you cross-link the ajv ticket here so others can follow its progress?

karenetheridge avatar Sep 16 '21 19:09 karenetheridge

Absolutely! Here it is

https://github.com/ajv-validator/ajv/issues/1745

essential-randomness avatar Sep 17 '21 02:09 essential-randomness

See also https://github.com/ajv-validator/ajv/issues/1573 by @seriousme. @epoberezkin originally closed https://github.com/ajv-validator/ajv/issues/1745 as a duplicate of that one, but 8 days later reopened it

no-identd avatar Nov 08 '21 10:11 no-identd

@essential-randomness : see also: https://github.com/seriousme/openapi-schema-validator ;-)

seriousme avatar Nov 08 '21 17:11 seriousme

@essential-randomness : see also: https://github.com/seriousme/openapi-schema-validator ;-)

Oh I use this! (Thanks for making it)

Fairly sure it doesn't catch everything though. I've had to use 2-3 different validators to get most of the errors out and even then stuff still slips through. I've also not been able to make 3.1 schemas pass for other JS libraries, and when I looked at why yours worked there was a reason for why yours would and theirs wouldn't that I have now forgotten.

essential-randomness avatar Nov 08 '21 17:11 essential-randomness

I see that @seriousme/openapi-schema-validator modifies the official meta-schemas to work around the ajv bug.

@hyperjump/oas-schema-validator works properly without workarounds and can validate the schemas against the OpenAPI JSON Schema vocabulary or whatever JSON Schema dialect you choose. Here's a quick example.

const OasSchema = require("@hyperjump/oas-schema-validator");
const example = require("./example.openapi.json");

(async function () {
  const schema = await OasSchema.get("https://spec.openapis.org/oas/3.1/schema-base");
  const validate = await OasSchema.validate(schema);

  const result = validate(example, OasSchema.BASIC);
  console.log(result);
}()); 

jdesrosiers avatar Nov 08 '21 20:11 jdesrosiers

Yes, working on a fix here too… It’s a tricky one, at the time it was implemented the spec didn’t settle yet, but should be ok soon.

epoberezkin avatar Nov 11 '21 12:11 epoberezkin

any updates?

MatanYemini avatar Mar 22 '22 10:03 MatanYemini

@MatanYemini check https://github.com/cdimascio/express-openapi-validator/pull/713

no-identd avatar Mar 22 '22 17:03 no-identd

Just tested, openapi-schema-validator validation failed at the raw spec containing the "$dynamicRef": "#meta" hence it still uses a modified 3.1 schema where "$dynamicRef": "#meta" has been converted to "$ref": "#/$defs/schema"

With the modified 3.1 schema validation tests succeed as before.

seriousme avatar Mar 22 '22 18:03 seriousme

@seriousme @MatanYemini try with https://github.com/cdimascio/express-openapi-validator/pull/713#issuecomment-1140492669, or newer?

no-identd avatar Sep 09 '22 20:09 no-identd

@no-identd Upgrading AJV won't fix this problem until AJV fixes the bug responsible for the problem.

jdesrosiers avatar Sep 10 '22 18:09 jdesrosiers

Since the only problems here are with JSON Schema validators, and not the OAS itself, I'm going to close this- there are plenty of issues tracking the validator bugs/missing features.

handrews avatar Jan 27 '24 21:01 handrews