Skip to content

Commit

Permalink
fix: null check when checking schemaFormat in array items (#786)
Browse files Browse the repository at this point in the history
Co-authored-by: Chris Patmore <[email protected]>
  • Loading branch information
chrispatmore and Chris Patmore authored Jun 14, 2023
1 parent b2213f9 commit 74cfce1
Show file tree
Hide file tree
Showing 3 changed files with 94 additions and 10 deletions.
18 changes: 9 additions & 9 deletions src/ruleset/v2/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,17 +123,17 @@ export const v2CoreRuleset = {
given: [
// messages
'$.channels.*.[publish,subscribe].[?(@property === \'message\' && @.schemaFormat === void 0)]',
'$.channels.*.[publish,subscribe].message.oneOf[?(@.schemaFormat === void 0)]',
'$.channels.*.[publish,subscribe].message.oneOf[?(!@null && @.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe].[?(@property === \'message\' && @.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe].message.oneOf[?(@.schemaFormat === void 0)]',
'$.components.messages[?(@.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe].message.oneOf[?(!@null && @.schemaFormat === void 0)]',
'$.components.messages[?(!@null && @.schemaFormat === void 0)]',
// message traits
'$.channels.*.[publish,subscribe].message.traits[?(@.schemaFormat === void 0)]',
'$.channels.*.[publish,subscribe].message.oneOf.*.traits[?(@.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe].message.traits[?(@.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe].message.oneOf.*.traits[?(@.schemaFormat === void 0)]',
'$.components.messages.*.traits[?(@.schemaFormat === void 0)]',
'$.components.messageTraits[?(@.schemaFormat === void 0)]',
'$.channels.*.[publish,subscribe].message.traits[?(!@null && @.schemaFormat === void 0)]',
'$.channels.*.[publish,subscribe].message.oneOf.*.traits[?(!@null && @.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe].message.traits[?(!@null && @.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe].message.oneOf.*.traits[?(!@null && @.schemaFormat === void 0)]',
'$.components.messages.*.traits[?(!@null && @.schemaFormat === void 0)]',
'$.components.messageTraits[?(!@null && @.schemaFormat === void 0)]',
],
then: {
function: messageExamples,
Expand Down
85 changes: 84 additions & 1 deletion test/ruleset/rules/v2/asyncapi2-message-examples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ testRule('asyncapi2-message-examples', [
},
errors: [],
},

{
name: 'invalid avro spec case',
document: {
Expand Down Expand Up @@ -391,5 +392,87 @@ testRule('asyncapi2-message-examples', [
},
},
errors: [],
}
},

{
name: 'avro can contain null values',
document: {
asyncapi: '2.6.0',
channels: {
someChannel: {
publish: {
message: {
schemaFormat: 'application/vnd.apache.avro;version=1.9.0',
payload: {
type: 'record',
name: 'Command',
fields: [{
name: 'foo',
default: null,
type: ['null', 'string'],
}],
},
examples: [
{
payload: {}
},
],
},
},
},
},
},
errors: [],
},

{
name: 'handles oneOf processing',
document: {
asyncapi: '2.6.0',
channels: {
someChannel: {
publish: {
message: {
oneOf: [
{
schemaFormat: 'application/vnd.apache.avro;version=1.9.0',
payload: {
type: 'record',
name: 'Command',
fields: [{
name: 'foo',
default: null,
type: ['null', 'string'],
}],
},
examples: [
{
payload: {}
},
],
},
{
payload: {
type: 'string'
},
examples: [
{
payload: 1
},
],
},
],
},
},
},
},
},
errors: [
{
message: '"payload" property type must be string',
path: ['channels', 'someChannel', 'publish', 'message', 'oneOf', '1', 'examples', '0', 'payload'],
severity: DiagnosticSeverity.Error,
},
],
},
]);
1 change: 1 addition & 0 deletions test/ruleset/tester.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function testRule(ruleName: RuleNames, tests: Scenario,): void {
const doc = JSON.stringify(testCase.document);

const errors = await parser.validate(doc);
expect(errors.filter(({ code }) => code === 'uncaught-error')).toHaveLength(0);
expect(errors.filter(({ code }) => code === ruleName)).toEqual(
testCase.errors.map(error => expect.objectContaining(error) as unknown),
);
Expand Down

0 comments on commit 74cfce1

Please sign in to comment.