-
-
Notifications
You must be signed in to change notification settings - Fork 240
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rulesets): handle empty payload and headers in AsyncAPI message's…
… examples validation (#2284)
- Loading branch information
1 parent
ba90a20
commit 4068221
Showing
9 changed files
with
256 additions
and
16 deletions.
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