Comments (5)
@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:
json-everything/JsonSchema.Tests/BaseDocumentTests.cs
Lines 163 to 207 in d30e707
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:
- Make
OpenApiDocument
implementIBaseDocument
. - When you initialize
OpenApiDocument
, scan for schemas and set their.BaseUri
s. - Register the
OpenApiDocument
with the schema registry. (You can use either the global registry or the one inEvaluationOptions
. 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.
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.
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.
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
from json-everything.
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)
- Custom `IAttributeHandler` never called HOT 5
- Anchors naming of 2020 still following 2019 rules HOT 5
- Generating a Custom JSON Schema HOT 1
- ArgumentOutOfRangeException thrown when calling JsonSchema's Evaluate methods from multiple threads. HOT 3
- 2020-12 output incorrectly places annotations
- Nullable decimal type removes multipleOf HOT 1
- Boolean applicator keywords must be non-empty HOT 1
- Issues with rendering on docs site HOT 1
- Verified error reporting type HOT 18
- 'Required' is an ambiguous reference between 'System.ComponentModel.DataAnnotations.RequiredAttribute' and 'Json.Schema.Generation.RequiredAttribute'? HOT 22
- Schema exception {"An item with the same key has already been added. Key: Child"} HOT 9
- Possibly wrong return type HOT 10
- Customizing the `format` generated for a field (without attributes) HOT 5
- Json.Schema.KeywordExtensions is not thread safe with custom keywords HOT 1
- Update Nuget package meta-data HOT 6
- Error "unknown context value" Raised After Checking for Existence HOT 2
- Online schema validator: Is successful validation of illegal json just a quirk or intentional HOT 5
- System.MissingMethodException for IReadOnlyCollection<JsonNode> after updating from JsonSchema.Net.Generation 4.3.0 to 4.3.0.2 HOT 5
- Can't get DiscriminatorKeyword working HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from json-everything.