-
-
Notifications
You must be signed in to change notification settings - Fork 241
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
fix(rulesets): handle empty payload and headers in AsyncAPI message's examples validation #2284
Merged
P0lip
merged 2 commits into
stoplightio:develop
from
magicmatatjahu:asyncapi/fix-message-examples-rule
Oct 5, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
packages/rulesets/src/asyncapi/functions/utils/__tests__/mergeTraits.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
import { mergeTraits } from '../mergeTraits'; | ||
|
||
describe('mergeTraits', () => { | ||
test('should merge one trait', () => { | ||
const result = mergeTraits({ payload: {}, traits: [{ payload: { someKey: 'someValue' } }] }); | ||
expect(result.payload).toEqual({ someKey: 'someValue' }); | ||
}); | ||
|
||
test('should merge two or more traits', () => { | ||
const result = mergeTraits({ | ||
payload: {}, | ||
traits: [ | ||
{ payload: { someKey1: 'someValue1' } }, | ||
{ payload: { someKey2: 'someValue2' } }, | ||
{ payload: { someKey3: 'someValue3' } }, | ||
], | ||
}); | ||
expect(result.payload).toEqual({ someKey1: 'someValue1', someKey2: 'someValue2', someKey3: 'someValue3' }); | ||
}); | ||
|
||
test('should override fields', () => { | ||
const result = mergeTraits({ | ||
payload: { someKey: 'someValue' }, | ||
traits: [ | ||
{ payload: { someKey: 'someValue1' } }, | ||
{ payload: { someKey: 'someValue2' } }, | ||
{ payload: { someKey: 'someValue3' } }, | ||
], | ||
}); | ||
expect(result.payload).toEqual({ someKey: 'someValue3' }); | ||
}); | ||
}); |
43 changes: 43 additions & 0 deletions
43
packages/rulesets/src/asyncapi/functions/utils/mergeTraits.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { isPlainObject } from '@stoplight/json'; | ||
|
||
type HaveTraits = { traits?: any[] } & Record<string, any>; | ||
|
||
/** | ||
* A function used to merge traits defined for the given object from the AsyncAPI document. | ||
* It uses the [JSON Merge Patch](https://www.rfc-editor.org/rfc/rfc7386). | ||
* | ||
* @param data An object with the traits | ||
* @returns Merged object | ||
*/ | ||
export function mergeTraits<T extends HaveTraits>(data: T): T { | ||
if (Array.isArray(data.traits)) { | ||
data = { ...data }; // shallow copy | ||
for (const trait of data.traits as T[]) { | ||
for (const key in trait) { | ||
data[key] = merge(data[key], trait[key]); | ||
} | ||
} | ||
} | ||
return data; | ||
} | ||
|
||
function merge<T>(origin: unknown, patch: unknown): T { | ||
// If the patch is not an object, it replaces the origin. | ||
if (!isPlainObject(patch)) { | ||
return patch as T; | ||
} | ||
|
||
const result = !isPlainObject(origin) | ||
? {} // Non objects are being replaced. | ||
: Object.assign({}, origin); // Make sure we never modify the origin. | ||
|
||
Object.keys(patch).forEach(key => { | ||
const patchVal = patch[key]; | ||
if (patchVal === null) { | ||
delete result[key]; | ||
} else { | ||
result[key] = merge(result[key], patchVal); | ||
} | ||
}); | ||
return result as T; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would probably add some more docs here and specifically stating that this is JSON Merge Patch.
https://www.rfc-editor.org/rfc/rfc7386
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.
Done, thanks! :)