-
-
Notifications
You must be signed in to change notification settings - Fork 240
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
Add an *effective* flavor for AsyncAPI documents where traits
have been applied
#2235
Comments
@c-pius Hello! Thanks for the issue! I understand the problem because writing a new rule to check uniqueness of However, I have a serious objection to merging traits by spectral - consider that after merging you will be operating on a single object (without looking at traits). Let's take an example: asyncapi: '2.4.0',
channels: {
someChannel1: {
subscribe: {
message: {
messageId: 'id1',
},
},
},
someChannel2: {
subscribe: {
message: {
messageId: 'id2',
},
},
publish: {
message: {
oneOf: [
{
messageId: 'id3',
traits: [
{
messageId: 'id4',
},
{
messageId: 'id2',
},
],
},
{
messageId: 'id3',
traits: [
{
messageId: 'id1',
},
],
},
],
},
},
},
}, When we would have merged all the traits then the error would show the path
instead of:
In this case, the user would have a problem in understanding where the error occurs - would have to know that a certain trait can override something and would have to check the trait "visually" without spectral. Using the above example, we can see that the problem is not as simple to solve as enabling/disabling What do you think? |
@magicmatatjahu yeah I see where you are coming from. Maybe that could be addressed by configurable plain vs effective flavor. Using effective means you validate the end result of the message, regardless what "syntactic sugar" was used to define it. Consequently, also a path pointing to the message is somehow correct (and the only thing you can really do then). If you want to validate down into the single traits, you can do it with the plain flavor but that might cause some pain (pun intended 😄). At least I would consider a path pointing to the message not as completely wrong, and an acceptable trade-off for the a lot easier validation on message level that you could do. Please correct me if I am wrong but using the core functions like defined is virtually impossible on AsyncAPI messages right now because you can never know where an attribute is coming from? |
Yeah, atm it's problematic, you would only check "root" of message without merged traits, so We'll see, maybe Spectral Team will consider adding a new option to the rule shape you proposed :) |
Definitely a problem, but not one I see as critical, i.e. spectral already handles this behavior with the refs right? Cause the same "problem" must be present there. I definitely see this as only an implementation issue that can easily be handed within spectral itself 🙂 I am definitely for finding a way to support this yes 👍 |
But Spectral points to the
and if given Also I think that I have idea how to enable that behaviour. Rather to have implemented in source code functionality like {
given: [ ... ],
then: {
function: mergeTraits,
functionOptions: {
field: 'operatioId' // example
function: truthy
},
},
} so we will "extend" |
Sounds like a good plan 🤔 👍 |
User story.
As a developer, I want to write
AsyncAPI
rules working on the "effective" AsyncAPI document. Effective meaning that Operation, and Message traits (see fields) have been applied to the document, similar as done with$ref
resolving.Is your feature request related to a problem?
We want to validate that some of our common header attributes are always defined for certain messages. However, some define those headers directly on each message, others define a common trait that is mixing those headers into the messages. This makes it very hard to validate the message for such common headers as we need to consider multiple places where the info may be, including varying naming of the traits and even the algorithm for applying traits.
Example:
Based on the AsyncAPI Streetlights. We have the
LightMeasured
message where themyCommonHeader
is not directly defined on the message, but mixed in via theCommonHeaders
trait. Checking thatmyCommonHeader
is defined on the message is hard as I have to a) check if it is directly on the message, b) check what traits are defined on the message and further check each of those if the header is defined there (even considering that traits are applied in-order using json-merge-patch algorithm).Resulting effective message schema:
Describe the solution you'd like
Similar to the
$ref
resolving, provide a possibility for rules to work on either the "plain" AsyncAPI without applied traits, or the "effective" AsyncAPI where traits have been applied.Additional context
Note that traits in AsyncAPI also have their flaws and potential for undesired behavior. But actually, I think that providing validations rules based on the effective AsyncAPI would be a means to reduce errors made because of wrongly applying traits.
The text was updated successfully, but these errors were encountered: