Skip to content
Open
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
5 changes: 5 additions & 0 deletions .changeset/fix-payload-message-null.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/parser": patch
---

fix: allow payload property named message with null value (#863)
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ export function asyncApi2MessageExamplesParserRule(parser: Parser): RuleDefiniti
recommended: true,
given: [
// messages
'$.channels.*.[publish,subscribe][?(@property === \'message\' && @.schemaFormat !== void 0)]',
'$.channels.*.[publish,subscribe][?(@property === \'message\' && !@null && @.schemaFormat !== void 0)]',
'$.channels.*.[publish,subscribe].message.oneOf[?(!@null && @.schemaFormat !== void 0)]',
'$.components.channels.*.[publish,subscribe].message[?(@property === \'message\' && @.schemaFormat !== void 0)]',
'$.components.channels.*.[publish,subscribe].message[?(@property === \'message\' && !@null && @.schemaFormat !== void 0)]',
'$.components.channels.*.[publish,subscribe].message.oneOf[?(!@null && @.schemaFormat !== void 0)]',
'$.components.messages[?(!@null && @.schemaFormat !== void 0)]',
// message traits
Expand Down
16 changes: 8 additions & 8 deletions packages/parser/src/ruleset/v2/ruleset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,9 @@ export const v2CoreRuleset = {
recommended: true,
given: [
// messages
'$.channels.*.[publish,subscribe][?(@property === \'message\' && @.schemaFormat === void 0)]',
'$.channels.*.[publish,subscribe][?(@property === \'message\' && !@null && @.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][?(@property === \'message\' && !@null && @.schemaFormat === void 0)]',
'$.components.channels.*.[publish,subscribe].message.oneOf[?(!@null && @.schemaFormat === void 0)]',
'$.components.messages[?(!@null && @.schemaFormat === void 0)]',
// message traits
Expand Down Expand Up @@ -201,9 +201,9 @@ export const v2SchemasRuleset = (parser: Parser) => {
severity: 'error',
recommended: true,
given: [
'$.channels[*][publish,subscribe][?(@property === \'message\' && @.schemaFormat === void 0)].payload.default^',
'$.channels[*][publish,subscribe][?(@property === \'message\' && !@null && @.schemaFormat === void 0)].payload.default^',
'$.channels.*.parameters.*.schema.default^',
'$.components.channels[*][publish,subscribe][?(@property === \'message\' && @.schemaFormat === void 0)].payload.default^',
'$.components.channels[*][publish,subscribe][?(@property === \'message\' && !@null && @.schemaFormat === void 0)].payload.default^',
'$.components.channels.*.parameters.*.schema.default^',
'$.components.schemas.*.default^',
'$.components.parameters.*.schema.default^',
Expand All @@ -223,9 +223,9 @@ export const v2SchemasRuleset = (parser: Parser) => {
severity: 'error',
recommended: true,
given: [
'$.channels[*][publish,subscribe][?(@property === \'message\' && @.schemaFormat === void 0)].payload.examples^',
'$.channels[*][publish,subscribe][?(@property === \'message\' && !@null && @.schemaFormat === void 0)].payload.examples^',
'$.channels.*.parameters.*.schema.examples^',
'$.components.channels[*][publish,subscribe][?(@property === \'message\' && @.schemaFormat === void 0)].payload.examples^',
'$.components.channels[*][publish,subscribe][?(@property === \'message\' && !@null && @.schemaFormat === void 0)].payload.examples^',
'$.components.channels.*.parameters.*.schema.examples^',
'$.components.schemas.*.examples^',
'$.components.parameters.*.schema.examples^',
Expand Down Expand Up @@ -338,9 +338,9 @@ export const v2RecommendedRuleset = {
recommended: true,
formats: AsyncAPIFormats.filterByMajorVersions(['2']).excludeByVersions(['2.0.0', '2.1.0', '2.2.0', '2.3.0']).formats(), // message.messageId is available starting from v2.4.
given: [
'$.channels.*.[publish,subscribe][?(@property === "message" && @.oneOf == void 0)]',
'$.channels.*.[publish,subscribe][?(@property === "message" && !@null && @.oneOf == void 0)]',
'$.channels.*.[publish,subscribe].message.oneOf.*',
'$.components.channels.*.[publish,subscribe][?(@property === "message" && @.oneOf == void 0)]',
'$.components.channels.*.[publish,subscribe][?(@property === "message" && !@null && @.oneOf == void 0)]',
'$.components.channels.*.[publish,subscribe].message.oneOf.*',
'$.components.messages.*',
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,4 +477,52 @@ testRule('asyncapi2-message-examples', [
},
],
},

// See https://github.com/asyncapi/parser-js/issues/863
{
name: 'valid case with payload property named message and null example value',
document: {
asyncapi: '2.4.0',
info: {
title: 'Messages',
version: '1.1.0',
},
defaultContentType: 'application/json',
channels: {
'test/message': {
subscribe: {
message: {
$ref: '#/components/messages/transferReturned',
},
},
},
},
components: {
messages: {
transferReturned: {
title: 'Transfer Returned',
payload: {
type: 'object',
properties: {
message: {
type: ['string', 'null'],
maxLength: 50,
},
},
required: ['message'],
},
examples: [
{
name: 'return',
payload: {
message: null,
},
},
],
},
},
},
},
errors: [],
},
]);
Original file line number Diff line number Diff line change
Expand Up @@ -396,4 +396,52 @@ testRule('asyncapi2-schema-examples', [
},
],
},

// See https://github.com/asyncapi/parser-js/issues/863
{
name: 'valid case with payload property named message and null example value',
document: {
asyncapi: '2.4.0',
info: {
title: 'Messages',
version: '1.1.0',
},
defaultContentType: 'application/json',
channels: {
'test/message': {
subscribe: {
message: {
$ref: '#/components/messages/transferReturned',
},
},
},
},
components: {
messages: {
transferReturned: {
title: 'Transfer Returned',
payload: {
type: 'object',
properties: {
message: {
type: ['string', 'null'],
maxLength: 50,
},
},
required: ['message'],
},
examples: [
{
name: 'return',
payload: {
message: null,
},
},
],
},
},
},
},
errors: [],
},
]);
53 changes: 53 additions & 0 deletions packages/parser/test/validate.spec.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AsyncAPIDocumentV2 } from '../src/models';
import { AsyncAPIDocument } from '../src/models/v3/asyncapi';
import { Parser } from '../src/parser';
import { hasErrorDiagnostic, hasWarningDiagnostic } from '../src/utils';
Expand Down Expand Up @@ -97,4 +98,56 @@ describe('validate()', function() {
expect(document).toBeInstanceOf(AsyncAPIDocument);
expect(filterLastVersionDiagnostics(diagnostics)).toHaveLength(0);
});

// See https://github.com/asyncapi/parser-js/issues/863
it('should parse document with payload property named message and null example value', async function() {
const documentRaw = {
asyncapi: '2.4.0',
info: {
title: 'Messages',
version: '1.1.0',
},
defaultContentType: 'application/json',
channels: {
'test/message': {
subscribe: {
message: {
$ref: '#/components/messages/transferReturned',
},
},
},
},
components: {
messages: {
transferReturned: {
title: 'Transfer Returned',
payload: {
type: 'object',
properties: {
message: {
type: ['string', 'null'],
maxLength: 50,
},
},
required: ['message'],
},
examples: [
{
name: 'return',
payload: {
message: null,
},
},
],
},
},
},
};
const { document, diagnostics } = await parser.parse(documentRaw);

expect(document).toBeInstanceOf(AsyncAPIDocumentV2);
expect(diagnostics.some(d => d.message?.includes('Cannot read properties of null'))).toEqual(false);
expect(diagnostics.some(d => d.code === 'uncaught-error')).toEqual(false);
expect(hasErrorDiagnostic(diagnostics)).toEqual(false);
});
});
Loading