Git Product home page Git Product logo

Comments (5)

gregsdennis avatar gregsdennis commented on July 18, 2024 1

@MaggieKimani1 you need to register the entire OpenAPI document under a URI identifier. Then, when you create a new schema, set its .BaseUri to the same identifier.

See this test:

public void ReferenceEmbeddedSchemaStartingWithOtherEmbeddedSchema()
{
var subjectSchemaJson = new JsonObject
{
["properties"] = new JsonObject
{
["data"] = new JsonObject
{
["$ref"] = "#/prop2/1"
}
}
};
var json = new JsonObject
{
["prop1"] = "foo",
["prop2"] = new JsonArray
(
"bar",
new JsonObject
{
["type"] = "integer"
}
),
["prop3"] = subjectSchemaJson
};
var options = new EvaluationOptions
{
OutputFormat = OutputFormat.List
};
var jsonBaseDoc = new JsonNodeBaseDocument(json, new Uri("http://localhost:1234/doc"));
options.SchemaRegistry.Register(jsonBaseDoc);
var subjectSchema = subjectSchemaJson.Deserialize(TestSerializerContext.Default.JsonSchema)!;
subjectSchema.BaseUri = jsonBaseDoc.BaseUri;
JsonNode instance = new JsonObject { ["data"] = 42 };
var result = subjectSchema.Evaluate(instance, options);
result.AssertValid();
}

This is why I suggested to you that you need to make your OpenAPIDocument type implement IBaseDocument. I created IBaseDocument explicitly to support this use case. You can also see it working in my Graeae project.

What I recommend:

  1. Make OpenApiDocument implement IBaseDocument.
  2. When you initialize OpenApiDocument, scan for schemas and set their .BaseUris.
  3. Register the OpenApiDocument with the schema registry. (You can use either the global registry or the one in EvaluationOptions. If you use the options one, you'll need to always use those options or remember to re-register the schema with the new options.)

Then you should be able to grab any nested schema and validate with it.

If you don't do these things, then you'll end up with the schema thinking it's the root of that base URI, and it'll try to resolve pointers on the schema instead of the actual document.

from json-everything.

elgonzo avatar elgonzo commented on July 18, 2024

How exactly is your code utilizing the schema(s) inside the OpenApiDocument?

(As a side note, JsonSchema.NET is already at version 6.1.2)

from json-everything.

elgonzo avatar elgonzo commented on July 18, 2024

For what it's worth, here is an illustrative example code, modifying the schema by adding the component schemas to it. This is done because the schema reference in your example schema has an #, which indicates an embedded schema, so by making the "components" object a part of the schema object the path of the reference will resolve successfully.

var yaml = new YamlStream();
yaml.Load(... the OpenApiDocument as shown in your report ...);
var joOpenAPIDoc = (JsonObject) yaml.ToJsonNode().First();

var joSchema = (JsonObject) joOpenAPIDoc["paths"]["/issues"]["get"]["responses"]["200"]["content"]["application/json"]["schema"];
var joComponentSchemas = (JsonObject)joOpenAPIDoc["components"];

joSchema.Add("components", joComponentSchemas.DeepClone());

var schema = joSchema.Deserialize<JsonSchema>();
var result = schema.Evaluate(... the example instance ...);

(Note that the example instance you have given will not validate successfully, because it is a root object with only an "example" property. However, your schema attempts to validate a root object with a mandatory "data" property.)

from json-everything.

MaggieKimani1 avatar MaggieKimani1 commented on July 18, 2024

How exactly is your code utilizing the schema(s) inside the OpenApiDocument?

(As a side note, JsonSchema.NET is already at version 6.1.2)

We're simply loading up the document in memory, then performing validation using a set of rules for every component within the document here https://github.com/microsoft/OpenAPI.NET/blob/f069dc154ab2c6c0c7e89cc6587f2f61b356d846/src/Microsoft.OpenApi/Reader/OpenApiJsonReader.cs#L108

When it gets to the media type object and finds an example node, it tries to validate it against the schema to see if the data type matches here: https://github.com/microsoft/OpenAPI.NET/blob/e76101a582b59d94a98f6103fe05c3d908ef79ef/src/Microsoft.OpenApi/Validations/Rules/OpenApiMediaTypeRules.cs#L35

https://github.com/microsoft/OpenAPI.NET/blob/e76101a582b59d94a98f6103fe05c3d908ef79ef/src/Microsoft.OpenApi/Validations/Rules/RuleHelpers.cs#L68

from json-everything.

MaggieKimani1 avatar MaggieKimani1 commented on July 18, 2024

For what it's worth, here is an illustrative example code, modifying the schema by adding the component schemas to it. This is done because the schema reference in your example schema has an #, which indicates an embedded schema, so by making the "components" object a part of the schema object the path of the reference will resolve successfully.

var yaml = new YamlStream();
yaml.Load(... the OpenApiDocument as shown in your report ...);
var joOpenAPIDoc = (JsonObject) yaml.ToJsonNode().First();

var joSchema = (JsonObject) joOpenAPIDoc["paths"]["/issues"]["get"]["responses"]["200"]["content"]["application/json"]["schema"];
var joComponentSchemas = (JsonObject)joOpenAPIDoc["components"];

joSchema.Add("components", joComponentSchemas.DeepClone());

var schema = joSchema.Deserialize<JsonSchema>();
var result = schema.Evaluate(... the example instance ...);

(Note that the example instance you have given will not validate successfully, because it is a root object with only an "example" property. However, your schema attempts to validate a root object with a mandatory "data" property.)

Does this mean I should try dereference the schema first before performing evaluation?
I see you're replacing the embedded $ref with the component schema here before calling schema.Evaluate()?

from json-everything.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.