Skip to content

Commit

Permalink
fix schema parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Oct 3, 2023
1 parent 0501206 commit 9d5f7ac
Show file tree
Hide file tree
Showing 2 changed files with 277 additions and 0 deletions.
130 changes: 130 additions & 0 deletions test/custom-operations/parse-schema-v2.spec.ts
Original file line number Diff line number Diff line change
@@ -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': '<anonymous-schema-1>' });
});

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': '<anonymous-schema-1>' });
});

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': '<anonymous-schema-1>' });
expect((document?.json().components?.messages?.message as v2.MessageObject)?.payload).toEqual({ type: 'object', 'x-parser-schema-id': '<anonymous-schema-1>' });
// 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);
});
});
147 changes: 147 additions & 0 deletions test/custom-operations/parse-schema-v3.spec.ts
Original file line number Diff line number Diff line change
@@ -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': '<anonymous-schema-1>' });
});

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': '<anonymous-schema-1>' });
});

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)

Check failure on line 109 in test/custom-operations/parse-schema-v3.spec.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - ubuntu-latest

Missing semicolon

Check failure on line 109 in test/custom-operations/parse-schema-v3.spec.ts

View workflow job for this annotation

GitHub Actions / Test NodeJS PR - macos-latest

Missing semicolon
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': '<anonymous-schema-1>' });
expect((document?.json().components?.messages?.message as v3.MessageObject)?.payload).toEqual({ type: 'object', 'x-parser-schema-id': '<anonymous-schema-1>' });
// 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);
});
});

0 comments on commit 9d5f7ac

Please sign in to comment.