Comments (6)
Hi and welcome,
I'll try to answer all of this as best as I can:
1- You are setting the schema as the schema to validate and as its own parent. This is useless. You don't need the protected method, since data can be validated from a single root schema. Try it, it won't change the results.
2- Version 0.9 will report subvalidation items instead of this for all collection schemas (oneOf/anyOf/allOf). You can try it, this is already available.
3- If you set $ref, all siblings are ignored. You need to refactor your description to include it. By default, nullable is false
.
4- I don't get this one. In your Animal schema, you defined subspecies
as nullable. Your discriminator does not work. This only allowed when using oneOf, anyOf and allOf.
I hope those answers are helpful to you. If so, please close the issue by yourself.
Thanks.
from openapi4j.
Thank you @llfbandit for your swift response!
Unfortunately I don't get it quite how to use the library correctly.
-
How do I have to initialize the
SchemaValidator
in order to resolve$ref: "#/components/schemas/*"
correctly? To clarify my point I added some more examples . -
Yes, this is why I wanted to use the
0.9-SNAPSHOT
, but failed because I do not initialize theSchemaValidator
correctly. -
I think my spec description already includes
nullable: true
in the referencedSafariPark
object, which is fromtype: object
and setsnullable: true
.
Or would it be better to go for the allOf quantor and set thenullable: true
in the parent of the $ref instead of inside the ref itself, like pointed out here:
properties:
maybeSafari:
allOf:
- $ref: "#/components/schemas/SafariPark"
nullable: true
- I don't undestand why the discriminator does not work here as it is inherited by all
Animal
instances, which are just grouped byBigFive
. If there are some errors in the logic of my schema I would appreciate if you can point them out a little more in detail, so that I can investigate such inconsistencies.
To 1.:
I started to use the other constructor as I had issues with the $ref: "#/components/schemas/*"
references, which were not resolved properly when I just pass the schemas form path components/schemas
as root.
val objectValidator: SchemaValidator = new SchemaValidator("Animal", schema.get("components").get("schemas"))
Which leads to an
org.openapi4j.core.exception.ResolutionException: Reference '#/components/schemas/SafariPark' is unreachable in '/.
But if just put my zoo.json
as root schema then the SchemaValidator
validates against anything.
The following three examples accept this object with foo
properties without complaining.
1.1 Without ValidationContext
only SchemaValidator
"Some object with foo props" should "not be valid against the schema" in {
val schema: JsonNode = TreeUtil.json.readTree(new File("zoo.json"))
val data: JsonNode = TreeUtil.json.readTree(
"""
|{
| "foo": "bar"
|}
|""".stripMargin)
val objectValidator: SchemaValidator = new SchemaValidator("Animal", schema)
val validation = new ValidationResults();
objectValidator.validate(data, validation)
assert(validation.isValid === false)
}
1.2 Do I necessarily have to create a ValidationContext
where the schema is the root?
"Some object with foo props" should "not be valid against the schema with a ValidationContext from root" in {
val schema: JsonNode = TreeUtil.json.readTree(new File("zoo.json"))
val data: JsonNode = TreeUtil.json.readTree(
"""
|{
| "foo": "bar"
|}
|""".stripMargin)
val apiContext: OAI3Context = new OAI3Context(new URI("/"), schema)
val validationContext: ValidationContext[OAI3] = new ValidationContext(apiContext)
validationContext.setFastFail(false)
val objectValidator: SchemaValidator = new SchemaValidator(validationContext, "Animal", schema)
val validation = new ValidationResults();
objectValidator.validate(data, validation)
assert(validation.isValid === false)
}
1.3 And if I do so from which path do I have to put the schema into the SchemaValidator
?
"Some object with foo props" should
"not be valid against the schema with a ValidationContext from root and a SchemaValidator from path #/components/schemas" in {
val schema: JsonNode = TreeUtil.json.readTree(new File("zoo.json"))
val data: JsonNode = TreeUtil.json.readTree(
"""
|{
| "foo": "bar"
|}
|""".stripMargin)
val apiContext: OAI3Context = new OAI3Context(new URI("/"), schema)
val validationContext: ValidationContext[OAI3] = new ValidationContext(apiContext)
validationContext.setFastFail(false)
val objectValidator: SchemaValidator = new SchemaValidator(validationContext, "Animal", schema.get("components").get("schemas"))
val validation = new ValidationResults();
objectValidator.validate(data, validation)
assert(validation.isValid === false)
}
from openapi4j.
Assuming you're now using 0.9-SNAPSHOT.
The following pseudo-code should be enough to answer 1 & 2 (you need to now what schema you will validate data against):
val apiContext: OAI3Context = new OAI3Context(your_doc_location_as_URL))
val validationContext: ValidationContext[OAI3] = new ValidationContext(apiContext)
val objectValidator: SchemaValidator = new SchemaValidator(validationContext, null, animalSchema)
3- Ok, did not see it. Need to check.
4- Sorry my answer was unclear. As far as I can tell, you are validating your data directly from Animal
"abstract" schema not from BigFive
or a dedicated animal instance. This is why you don't get what you expect.
from openapi4j.
3 is fixed now in latest 0.9-SNAPSHOT.
from openapi4j.
Are you still experiencing difficulties to validate your data?
For more clarification, when OAI3Context
is created JSON references are registered and so known when you validate your data. Obviously, you should not re-create it each time you need validation.
from openapi4j.
Thank you very much @llfbandit! Yes, this helped. I am sorry I was unavailable for some time.
But I ran into an uninformative error message, for which I opened a new PR #105.
from openapi4j.
Related Issues (20)
- int64 or int32 format not identified by openapi4j tool HOT 3
- Mandatory element missing is not throwing error HOT 3
- Some of the optional fields when using oneOf are not being validated HOT 1
- Convenience methods to support easy response validation HOT 3
- Spring Adapter
- Validating data against schema with $ref HOT 2
- Incorrect path pattern for path parameter HOT 2
- Creating schema references programatically HOT 7
- OpenApi3Parser - add ability to provide spec string/stream to parse method HOT 2
- Integer formats not identified in 1.0.5 version openapi4j tool HOT 1
- Scheme that contains path parameters with minus sign in name fails validation HOT 1
- Throw ValidationException on path pattern collision
- Give the option to throw a validation error with no response validator is found
- RequestValidator fails when path params contains minus char HOT 1
- Get the path of an operation HOT 2
- ADDITIONAL_PROPS_RESTRICT = true rejects all attributes HOT 1
- Duplicate properties, Duplicate Headers are not being validated HOT 2
- Remove toString() call to replace NPE with more meaningful message
- Validation of callback URLs can't handle allOf/oneOf/... HOT 3
- Path pattern collision leads to incorrect Path selection
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 openapi4j.