-
-
Notifications
You must be signed in to change notification settings - Fork 98
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: enable AsyncAPI v3 validation #825
feat: enable AsyncAPI v3 validation #825
Conversation
cb22b1a
to
7b4d21c
Compare
Validation fails with the following errors: [
{
"code":"asyncapi-document-resolved",
"message":"'someOperation1' property must match exactly one schema in oneOf",
"path":[
"operations",
"someOperation1"
],
"range":{
"end":{
"character":46,
"line":20
},
"start":{
"character":21,
"line":10
}
},
"severity":0
},
{
"code":"asyncapi-document-resolved",
"message":"'someOperation2' property must match exactly one schema in oneOf",
"path":[
"operations",
"someOperation2"
],
"range":{
"end":{
"character":46,
"line":35
},
"start":{
"character":21,
"line":24
}
},
"severity":0
}
] The validation rule that fails is the Thanks @jonaslagoni for helping (and actually finding) that out. The error messages shown above are also wrong. We do some error filtering as stated in https://github.com/asyncapi/parser-js/blob/master/src/ruleset/functions/documentStructure.ts#L20, and it seems we are doing too much and, in this case, we skip the real error, which can be found in the list of errors prior to the filtering: [
{
"instancePath": "/operations/someOperation1",
"schemaPath": "#/required",
"keyword": "required",
"params": {
"missingProperty": "$ref"
},
"message": "must have required property '$ref'"
},
{
"instancePath": "/operations/someOperation1/channel",
"schemaPath": "#/required",
"keyword": "required",
"params": {
"missingProperty": "$ref"
},
"message": "must have required property '$ref'"
},
{
"instancePath": "/operations/someOperation1",
"schemaPath": "#/additionalProperties/oneOf",
"keyword": "oneOf",
"params": {
"passingSchemas": null
},
"message": "must match exactly one schema in oneOf"
},
{
"instancePath": "/operations/someOperation2",
"schemaPath": "#/required",
"keyword": "required",
"params": {
"missingProperty": "$ref"
},
"message": "must have required property '$ref'"
},
{
"instancePath": "/operations/someOperation2/channel",
"schemaPath": "#/required",
"keyword": "required",
"params": {
"missingProperty": "$ref"
},
"message": "must have required property '$ref'"
},
{
"instancePath": "/operations/someOperation2",
"schemaPath": "#/additionalProperties/oneOf",
"keyword": "oneOf",
"params": {
"passingSchemas": null
},
"message": "must match exactly one schema in oneOf"
}
] The expected error should be the ones with an |
Based on my prior comment, this is what we need to do next in this PR:
Alternatives for the second point will be to either allow by JSON Schema to define |
2be79d9
to
37c28bf
Compare
37c28bf
to
511fb5f
Compare
511fb5f
to
7128146
Compare
I’m thinking that we should not go in the direction of manipulating the schema for the The alternative is to remove the |
@smoya Issues are fixed :) |
Thanks for the help @magicmatatjahu 🙌 🙌 !! I still have this concern and I would love to know your opinion as you are the one with most knowledge on this code. I would like to have this PR merged but to keep iterating after so we go for a better and maintainable alternative. I created a follow up issue so we don't block this anymore. |
I agree that it's not good but parser should throws all errors, so I can accept situation when the source code is "ugly" but DX is better. We have also option to change the JSON Schemas to allow also inline objects in given places, but it won't follow the spec. There is also a idea to allow inline objects in spec itself - but then
|
Yeah, this is something I want to bring up and discuss with the community.
Yeah, I agree there is no better alternative and, for sure, this validation is an important addition on top of the basic schema validation. Maybe we could work on an automation, for every time we find a property with only a reference to a |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As I see it there is one additional approach that is unmentioned, and that is we could make the reference resolver ignore the specific path i.e.
- channelObject.properties.servers.items.$ref
- operationSchema.properties.channel.$ref
- operationSchema.properties.messages.items.$ref
- operationReplySchema.properties.channel.$ref
- operationReplySchema.properties.messages.items.$ref
Just for that specific rule, but it might be hard to pull of. There are also some more unknowns such as can the library we use do this? (I know https://apitools.dev/json-schema-ref-parser/docs/options.html can) Can we still ensure the refs are correct and point to a valid location?
So yea, I think your current solution makes sense 👍
@smoya have you added enough test cases to test for all those paths and that they are caught?
The alternative is to remove the asyncapi-document-resolved rule forever and just relay on the asyncapi-document-unresolved. The point is that we are filtering errors here, so we would need to return them and not filter. What is the real implication of it?
There is also a idea to allow inline objects in spec itself - but then 3.1.0 is needed.
I don't see this as making much sense from a spec point of view, so it might be really hard to do just because reference resolvement becomes hard to do 😄
}, | ||
|
||
{ | ||
name: 'valid case for 3.X.X (case when other $ref errors should also occur but we filter them out)', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are these names on cases still valid?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@magicmatatjahu pinging you because you added this test. What are the errors you expect here to be filtered?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Spectral works in two modes - with schema resolved and unresolved (with untouched refs). Even if we wrote a ref in a place then Spectral would not throw an error (because it operates on resolved schema). This is more of a test for checking if rule isn't "breaking" resolved/unresolved spectral behaviour, just in case for us :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you want you can change the name of the test because it should have a different name, I can't remember why I called it that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed. Thanks!
Kudos, SonarCloud Quality Gate passed! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
/rtm |
🎉 This PR is included in version 2.2.0-next-major-spec.1 🎉 The release is available on: Your semantic-release bot 📦🚀 |
Tests are failing. Read comments below.