diff --git a/test/custom-operations/parse-schema-v2.spec.ts b/test/custom-operations/parse-schema-v2.spec.ts new file mode 100644 index 000000000..8504bb64d --- /dev/null +++ b/test/custom-operations/parse-schema-v2.spec.ts @@ -0,0 +1,130 @@ +import { AsyncAPIDocumentV2 } from '../../src/models'; +import { Parser } from '../../src/parser'; + +import type { v2 } from '../../src/spec-types'; + +describe('custom operations for v2 - parse schemas', function() { + const parser = new Parser(); + + it('should parse valid schema format', async function() { + const documentRaw = { + asyncapi: '2.0.0', + info: { + title: 'Valid AsyncApi document', + version: '1.0', + }, + channels: { + channel: { + publish: { + operationId: 'operationId', + message: { + schemaFormat: 'application/vnd.aai.asyncapi;version=2.0.0', + payload: { + type: 'object', + } + } + } + } + } + }; + const { document, diagnostics } = await parser.parse(documentRaw); + + expect(document).toBeInstanceOf(AsyncAPIDocumentV2); + expect(diagnostics.length > 0).toEqual(true); + + expect((document?.json()?.channels?.channel?.publish?.message as v2.MessageObject)?.payload).toEqual({ type: 'object', 'x-parser-schema-id': '' }); + }); + + it('should parse valid default schema format', async function() { + const documentRaw = { + asyncapi: '2.0.0', + info: { + title: 'Valid AsyncApi document', + version: '1.0', + }, + channels: { + channel: { + publish: { + operationId: 'operationId', + message: { + payload: { + type: 'object', + } + } + } + } + } + }; + const { document, diagnostics } = await parser.parse(documentRaw); + + expect(document).toBeInstanceOf(AsyncAPIDocumentV2); + expect(diagnostics.length > 0).toEqual(true); + + expect((document?.json()?.channels?.channel?.publish?.message as v2.MessageObject)?.payload).toEqual({ type: 'object', 'x-parser-schema-id': '' }); + }); + + it('should preserve this same references', async function() { + const documentRaw = { + asyncapi: '2.0.0', + info: { + title: 'Valid AsyncApi document', + version: '1.0', + }, + channels: { + channel: { + publish: { + operationId: 'operationId', + message: { + $ref: '#/components/messages/message' + } + } + } + }, + components: { + messages: { + message: { + payload: { + type: 'object', + } + } + } + } + }; + const { document, diagnostics } = await parser.parse(documentRaw); + + expect(document).toBeInstanceOf(AsyncAPIDocumentV2); + expect(diagnostics.length > 0).toEqual(true); + + expect((document?.json()?.channels?.channel?.publish?.message as v2.MessageObject)?.payload).toEqual({ type: 'object', 'x-parser-schema-id': '' }); + expect((document?.json().components?.messages?.message as v2.MessageObject)?.payload).toEqual({ type: 'object', 'x-parser-schema-id': '' }); + // check if logic preserves references + expect((document?.json()?.channels?.channel?.publish?.message as v2.MessageObject)?.payload === (document?.json().components?.messages?.message as v2.MessageObject)?.payload).toEqual(true); + }); + + it('should parse invalid schema format', async function() { + const documentRaw = { + asyncapi: '2.0.0', + info: { + title: 'Valid AsyncApi document', + version: '1.0', + }, + channels: { + channel: { + publish: { + operationId: 'operationId', + message: { + schemaFormat: 'not existing', + payload: { + type: 'object', + } + } + } + } + } + }; + const { document, diagnostics } = await parser.parse(documentRaw); + + expect(document).toBeUndefined(); + expect(diagnostics.length > 0).toEqual(true); + }); +}); diff --git a/test/custom-operations/parse-schema-v3.spec.ts b/test/custom-operations/parse-schema-v3.spec.ts new file mode 100644 index 000000000..9cc0fdd6e --- /dev/null +++ b/test/custom-operations/parse-schema-v3.spec.ts @@ -0,0 +1,147 @@ +import { AsyncAPIDocumentV3 } from '../../src/models'; +import { Parser } from '../../src/parser'; + +import type { v3 } from '../../src/spec-types'; + +describe('custom operations for v3 - parse schemas', function() { + const parser = new Parser(); + + it('should parse valid schema format', async function() { + const documentRaw = { + asyncapi: '3.0.0', + info: { + title: 'Valid AsyncApi document', + version: '1.0' + }, + channels: { + channel: { + address: 'channel', + messages: { + message: { + payload: { + schemaFormat: 'application/vnd.aai.asyncapi;version=2.0.0', + schema: { + type: 'object' + } + } + } + } + } + } + }; + const { document, diagnostics } = await parser.parse(documentRaw); + + expect(document).toBeInstanceOf(AsyncAPIDocumentV3); + expect(diagnostics.length === 0).toEqual(true); + + expect(((document?.json()?.channels?.channel as v3.ChannelObject).messages?.message as v3.MessageObject)?.payload?.schema).toEqual({ type: 'object', 'x-parser-schema-id': '' }); + }); + + it('should parse valid default schema format', async function() { + const documentRaw = { + asyncapi: '3.0.0', + info: { + title: 'Valid AsyncApi document', + version: '1.0' + }, + channels: { + channel: { + address: 'channel', + messages: { + message: { + payload: { + type: 'object' + } + } + } + } + } + }; + const { document, diagnostics } = await parser.parse(documentRaw); + + expect(document).toBeInstanceOf(AsyncAPIDocumentV3); + expect(diagnostics.length === 0).toEqual(true); + + expect(((document?.json()?.channels?.channel as v3.ChannelObject).messages?.message as v3.MessageObject)?.payload).toEqual({ type: 'object', 'x-parser-schema-id': '' }); + }); + + it('should preserve this same references', async function() { + const documentRaw = { + asyncapi: '3.0.0', + info: { + title: 'Valid AsyncApi document', + version: '1.0' + }, + channels: { + channel: { + address: 'channel', + messages: { + message: { + $ref: '#/components/messages/message' + } + } + } + }, + operations: { + operationId: { + action: 'receive', + channel: { + $ref: '#/channels/channel' + }, + messages: [ + { + $ref: '#/components/messages/message' + } + ] + } + }, + components: { + messages: { + message: { + payload: { + type: 'object' + } + } + } + } + }; + const { document, diagnostics } = await parser.parse(documentRaw); + console.log(diagnostics) + expect(document).toBeInstanceOf(AsyncAPIDocumentV3); + expect(diagnostics.length === 0).toEqual(true); + + expect(((document?.json()?.channels?.channel as v3.ChannelObject).messages?.message as v3.MessageObject)?.payload).toEqual({ type: 'object', 'x-parser-schema-id': '' }); + expect((document?.json().components?.messages?.message as v3.MessageObject)?.payload).toEqual({ type: 'object', 'x-parser-schema-id': '' }); + // check if logic preserves references + expect(((document?.json()?.channels?.channel as v3.ChannelObject).messages?.message as v3.MessageObject)?.payload === (document?.json().components?.messages?.message as v3.MessageObject)?.payload).toEqual(true); + }); + + it('should parse invalid schema format', async function() { + const documentRaw = { + asyncapi: '3.0.0', + info: { + title: 'Valid AsyncApi document', + version: '1.0' + }, + channels: { + channel: { + address: 'channel', + messages: { + message: { + payload: { + schemaFormat: 'not existing', + schema: { + type: 'object' + } + } + } + } + } + } + }; + const { document, diagnostics } = await parser.parse(documentRaw); + + expect(document).toBeUndefined(); + expect(diagnostics.length > 0).toEqual(true); + }); +});