Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 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
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