Skip to content
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: null check when checking schemaFormat in array items #786

Merged
merged 1 commit into from
Jun 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]',
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just null check anywhere we are checking the value of a property inside an object

'$.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);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test wasn't seeing the uncaught errors which threw me for a long time

expect(errors.filter(({ code }) => code === ruleName)).toEqual(
testCase.errors.map(error => expect.objectContaining(error) as unknown),
);
Expand Down