bgh
bgh

Reputation: 2170

Use OpenAPI 3.1 Schema to validate an OpenAPI spec

I've been using the OpenAPI 3.0 schema to validate OpenAPI 3.0 specifications. However, I don't seem to be able to use the OpenAPI 3.1 schema to validate an OpenAPI 3.1 specification.

With OpenAPI 3.0 I can use this online JSON Validator with the schema/2021-09-28 schema as from the OpenAPI Initiative Registry.

For OpenAPI 3.1 I downloaded the schema-base/2022-10-07 schema from this same page. When I paste it into the online JSON Validator and run it against an OpenAPI spec, it reports the following error:

Could not read the JSON Schema : virtual://server/schema.json Error when resolving schema reference '#/$defs/dialect'. Path 'properties.jsonSchemaDialect', line 9, position 26.

In my .NET code I've tried doing the JSON validation using both JsonSchema.Net and Newtonsoft.Json.Schema. Neither of these libraries are able to load the JSON Schema as they fail to resolve this schema reference: https://spec.openapis.org/oas/3.1/schema/2022-10-07#/$defs/parameter/dependentSchemas/schema/$defs/styles-for-path

What am I missing?

Upvotes: 2

Views: 345

Answers (4)

bgh
bgh

Reputation: 2170

Another way to evaluate the OpenAPI schema is by using the Graeae by simply trying to deserialize it and catch any JsonException:

using Graeae.Models;
using Yaml2JsonNode;

YamlSerializer.Deserialize<OpenApiDocument>(openApiDefinitionString);

This has the advantage over my other answer that in addition to OpenAPI 3.1 it also supports OpenAPI 3.0.

Upvotes: 0

bgh
bgh

Reputation: 2170

I got the validation working in .NET by using JsonSchema.Net.OpenApi as suggested by @Jeremy Fiel and @gregsdennis. Thank you!

I created a fresh .NET 8.0 console application and installed JsonSchema.Net.OpenApi 3.1.0.

In order to get it to work I had to do the following:

  • Add JsonPointer.Net 5.0.2, otherwise I get the exception FileNotFoundException: Could not load file or assembly 'JsonPointer.Net, Version=5.0.0.0...
  • In addition to the OpenApi.Vocabularies.Register() statement for JsonSchema.Net.OpenApi, also add Register() calls for registering the OpenAPI dialect and OpenAPI document schemas as well.
  • Seeing as the MetaSchemas.DocumentSchema references the 2022/02/27 schema, and not the latest 2022/10/07 one, take care to load the corresponding JSON schema, i.e. schema-base/2022-02-27 — not schema-base/2022-10-07.

Here's my working C# code. The OpenAPI-3.1-Schema.json contains the schema-base/2022-02-27 schema from the OpenAPI Initiative Registry.

using System.Text.Json.Nodes;
using Json.Schema;
using OpenApi = Json.Schema.OpenApi;

OpenApi.Vocabularies.Register();
SchemaRegistry.Global.Register(OpenApi.MetaSchemas.OpenApiDialect);
SchemaRegistry.Global.Register(OpenApi.MetaSchemas.DocumentSchema);

var openApiSchema = JsonSchema.FromFile("OpenAPI-3.1-Schema.json");

using var definitionStream = File.OpenRead("MyDefinitionUsingOpenApi3.1.json");
var jsonNode = JsonNode.Parse(definitionStream);
var evaluationResults = openApiSchema.Evaluate(jsonNode);

if (!evaluationResults.IsValid)
{
    Console.WriteLine("Validation Failed!");
}

Upvotes: 1

gregsdennis
gregsdennis

Reputation: 8428

Jeremy's suggestion to use JsonSchema.Net.OpenApi is good, but I'd recommend going one step further and using Graeae. Graeae provides Open API description models and supports validation and dereferencing.

Disclaimer: Both JsonSchema.Net and Graeae are my projects.

Upvotes: 0

Jeremy Fiel
Jeremy Fiel

Reputation: 3307

liquid and Newtonsoft don't support JSON Schema Draft 2020-12 which is what OpenAPI 3.1 is based upon.

If you want another solid .Net package, you can try https://www.nuget.org/packages/JsonSchema.Net.OpenApi

Upvotes: 3

Related Questions