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-bindings-ref-filter.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@asyncapi/parser": patch
---

fix: filter out $ref keys in bindings() to prevent undefined parser output
8 changes: 5 additions & 3 deletions packages/parser/src/models/v2/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,11 @@ export class Components extends BaseModel<v2.ComponentsObject> implements Compon
const asyncapi = this.meta('asyncapi');
const pointer = `components/${itemsName}/${name}`;
bindings[name] = new Bindings(
Object.entries(bindingsData).map(([protocol, binding]) =>
this.createModel(Binding, binding, { protocol, pointer: `${pointer}/${protocol}` })
),
Object.entries(bindingsData)
.filter(([key]) => !key.startsWith('$'))
.map(([protocol, binding]) =>
this.createModel(Binding, binding, { protocol, pointer: `${pointer}/${protocol}` })
),
{ originalData: bindingsData as any, asyncapi, pointer }
);
return bindings;
Expand Down
8 changes: 5 additions & 3 deletions packages/parser/src/models/v2/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ import type { v2 } from '../../spec-types';
export function bindings(model: BaseModel<{ bindings?: Record<string, any> }>): BindingsInterface {
const bindings = model.json('bindings') || {};
return new Bindings(
Object.entries(bindings || {}).map(([protocol, binding]) =>
createModel(Binding, binding, { protocol, pointer: model.jsonPath(`bindings/${protocol}`) }, model)
),
Object.entries(bindings || {})
.filter(([key]) => !key.startsWith('$'))
.map(([protocol, binding]) =>
createModel(Binding, binding, { protocol, pointer: model.jsonPath(`bindings/${protocol}`) }, model)
),
{ originalData: bindings, asyncapi: model.meta('asyncapi'), pointer: model.jsonPath('bindings') }
);
}
Expand Down
8 changes: 5 additions & 3 deletions packages/parser/src/models/v3/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,9 +157,11 @@ export class Components extends BaseModel<v3.ComponentsObject> implements Compon
const asyncapi = this.meta('asyncapi');
const pointer = `components/${itemsName}/${name}`;
bindings[name] = new Bindings(
Object.entries(bindingsData).map(([protocol, binding]) =>
this.createModel(Binding, binding, { protocol, pointer: `${pointer}/${protocol}` })
),
Object.entries(bindingsData)
.filter(([key]) => !key.startsWith('$'))
.map(([protocol, binding]) =>
this.createModel(Binding, binding, { protocol, pointer: `${pointer}/${protocol}` })
),
{ originalData: bindingsData as any, asyncapi, pointer }
);
return bindings;
Expand Down
8 changes: 5 additions & 3 deletions packages/parser/src/models/v3/mixins.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,11 @@ export abstract class CoreModel<J extends CoreObject = CoreObject, M extends Rec
export function bindings(model: BaseModel<{ bindings?: BindingsObject }>): BindingsInterface {
const bindings = model.json('bindings') || {};
return new Bindings(
Object.entries(bindings || {}).map(([protocol, binding]) =>
createModel(Binding, binding, { protocol, pointer: model.jsonPath(`bindings/${protocol}`) }, model)
),
Object.entries(bindings || {})
.filter(([key]) => !key.startsWith('$'))
.map(([protocol, binding]) =>
createModel(Binding, binding, { protocol, pointer: model.jsonPath(`bindings/${protocol}`) }, model)
),
{ originalData: bindings as Record<string, Binding>, asyncapi: model.meta('asyncapi'), pointer: model.jsonPath('bindings') }
);
}
Expand Down
4 changes: 2 additions & 2 deletions packages/parser/src/schema-parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ export async function validateSchema(parser: Parser, input: ValidateSchemaInput)
return schemaParser.validate(input);
}

export async function parseSchema(parser: Parser, input: ParseSchemaInput) {
export async function parseSchema(parser: Parser, input: ParseSchemaInput): Promise<AsyncAPISchema> {
const schemaParser = parser.parserRegistry.get(input.schemaFormat);
if (schemaParser === undefined) {
throw new Error('Unknown schema format');

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

this is the reason it was failing for avro schema parsing, if there is any spec containing avro schema parsing, we need to register manually otherwise it will raise an error cc: @princerajpoot20

return input.data as AsyncAPISchema;
}
return schemaParser.parse(input);
}
Expand Down
13 changes: 10 additions & 3 deletions packages/parser/test/custom-operations/parse-schema-v2.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ describe('custom operations for v2 - parse schemas', function() {
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() {
it('should skip parsing unknown schema format and still return document', async function() {
const documentRaw = {
asyncapi: '2.0.0',
info: {
Expand All @@ -122,9 +122,16 @@ describe('custom operations for v2 - parse schemas', function() {
}
}
};
const { document, diagnostics } = await parser.parse(documentRaw);
const { document, diagnostics } = await parser.parse(documentRaw, {
validateOptions: { allowedSeverity: { error: true } },
});

expect(document).toBeUndefined();
expect(document).toBeInstanceOf(AsyncAPIDocumentV2);
expect(diagnostics.length > 0).toEqual(true);
expect(diagnostics.some(d => d.message?.includes('Unknown schema format'))).toEqual(true);
expect((document?.json()?.channels?.channel?.publish?.message as v2.MessageObject)?.payload).toEqual({
type: 'object',
'x-parser-schema-id': '<anonymous-schema-1>',
});
});
});
67 changes: 65 additions & 2 deletions packages/parser/test/custom-operations/parse-schema-v3.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ describe('custom operations for v3 - parse schemas', function() {
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 parse invalid schema format', async function() {
it('should skip parsing unknown schema format and still return document', async function() {
const documentRaw = {
asyncapi: '3.0.0',
info: {
Expand All @@ -130,7 +130,70 @@ describe('custom operations for v3 - parse schemas', function() {
};
const { document, diagnostics } = await parser.parse(documentRaw);

expect(document).toBeUndefined();
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 avro schema format without registering AvroSchemaParser', async function() {
const documentRaw = {
asyncapi: '3.1.0',
info: {
title: 'Adeo AsyncAPI Case Study',
version: '1.0.0',
},
channels: {
costingRequestChannel: {
address: 'adeo-{env}-case-study-COSTING-REQUEST-{version}',
bindings: {
kafka: {
replicas: 3,
partitions: 3,
},
},
messages: {
CostingRequest: {
payload: {
schemaFormat: 'application/vnd.apache.avro;version=1.9.0',
schema: {
type: 'record',
name: 'CostingRequest',
fields: [],
},
},
},
},
},
},
operations: {
receiveACostingRequest: {
action: 'receive',
channel: {
$ref: '#/channels/costingRequestChannel',
},
bindings: {
kafka: {
groupId: {
type: 'string',
},
},
},
},
},
};
const { document, diagnostics } = await parser.parse(documentRaw);

expect(document).toBeInstanceOf(AsyncAPIDocumentV3);
expect(filterLastVersionDiagnostics(diagnostics).length === 0).toEqual(true);
expect(document!.allChannels().get('costingRequestChannel')?.bindings().get('kafka')?.json()).toEqual({
replicas: 3,
partitions: 3,
});
expect(document!.allOperations().get('receiveACostingRequest')?.bindings().get('kafka')?.json()?.groupId).toEqual({
type: 'string',
});
});
});
7 changes: 7 additions & 0 deletions packages/parser/test/models/v2/mixins.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ describe('mixins', function() {
expect(bindings(d3)).toBeInstanceOf(BindingsV2);
expect(bindings(d3).length).toEqual(0);
});

it('should ignore JSON reference keys in bindings', function() {
const doc = { bindings: { $ref: '#/components/messageBindings/myBindings', amqp: { test: 'test1' } } };
const model = new Model(doc);
expect(bindings(model).length).toEqual(1);
expect(bindings(model).all()[0].protocol()).toEqual('amqp');
});
});

describe('hasDescription', function() {
Expand Down
17 changes: 17 additions & 0 deletions packages/parser/test/models/v3/mixins.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BaseModel } from '../../../src/models/base';
import { bindings } from '../../../src/models/v3/mixins';
import { BindingsV3 } from '../../../src/models/v3';

describe('mixins', function() {
describe('bindings', function() {
class Model extends BaseModel {}

it('should ignore JSON reference keys in bindings', function() {
const doc = { bindings: { $ref: '#/components/messageBindings/myBindings', kafka: { bindingVersion: '0.4.0' } } };
const model = new Model(doc);
expect(bindings(model)).toBeInstanceOf(BindingsV3);
expect(bindings(model).length).toEqual(1);
expect(bindings(model).all()[0].protocol()).toEqual('kafka');
});
});
});
Loading