diff --git a/.changeset/fix-operation-channel-schema.md b/.changeset/fix-operation-channel-schema.md new file mode 100644 index 000000000..fcce000b4 --- /dev/null +++ b/.changeset/fix-operation-channel-schema.md @@ -0,0 +1,5 @@ +--- +"@asyncapi/parser": patch +--- + +Fix schema parsing for messages defined through operation channel references. diff --git a/packages/parser/src/custom-operations/parse-schema.ts b/packages/parser/src/custom-operations/parse-schema.ts index d33c017b4..a7a2f3c58 100644 --- a/packages/parser/src/custom-operations/parse-schema.ts +++ b/packages/parser/src/custom-operations/parse-schema.ts @@ -29,6 +29,8 @@ const customSchemasPathsV3 = [ '$.components.channels.*.messages.*.payload', '$.components.channels.*.messages.*.headers', // operations + '$.operations.*.channel.messages.*.payload', + '$.operations.*.channel.messages.*.headers', '$.operations.*.messages.*.payload', '$.operations.*.messages.*.headers', '$.components.operations.*.messages.*.payload', diff --git a/packages/parser/test/custom-operations/parse-schema-v3.spec.ts b/packages/parser/test/custom-operations/parse-schema-v3.spec.ts index 12cb24403..44731618e 100644 --- a/packages/parser/test/custom-operations/parse-schema-v3.spec.ts +++ b/packages/parser/test/custom-operations/parse-schema-v3.spec.ts @@ -1,6 +1,7 @@ import { AsyncAPIDocumentV3 } from '../../src/models'; import { Parser } from '../../src/parser'; import { filterLastVersionDiagnostics } from '../utils'; +import type { SchemaParser } from '../../src/schema-parser'; import type { v3 } from '../../src/spec-types'; @@ -253,4 +254,45 @@ describe('custom operations for v3 - parse schemas', function() { type: 'string', }); }); + + it('should parse schema through an external operation channel reference', async function() { + const documentRaw = { + asyncapi: '3.0.0', + info: { + title: 'External operation channel test', + version: '1.0.0', + }, + operations: { + operation: { + action: 'receive', + channel: { + $ref: '../mocks/parse/operation-channel.yaml#/channels/BarChannel', + }, + }, + }, + }; + + const customParser: SchemaParser = { + validate: () => [], + parse: async input => ({ + ...(input.data as any), + 'x-custom-parser': true, + }), + getMimeTypes: () => ['application/vnd.aai.asyncapi;version=2.0.0'], + }; + + parser.registerSchemaParser(customParser); + + const { document } = await parser.parse(documentRaw, { + source: __filename, + }); + + expect( + (document?.json() as any).operations.operation.channel.messages.AMessage + .payload.schema, + ).toEqual({ + type: 'object', + 'x-custom-parser': true, + }); + }); }); diff --git a/packages/parser/test/mocks/parse/operation-channel.yaml b/packages/parser/test/mocks/parse/operation-channel.yaml new file mode 100644 index 000000000..41a5faff6 --- /dev/null +++ b/packages/parser/test/mocks/parse/operation-channel.yaml @@ -0,0 +1,9 @@ +channels: + BarChannel: + address: test + messages: + AMessage: + payload: + schemaFormat: 'application/vnd.aai.asyncapi;version=2.0.0' + schema: + type: object