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: 4 additions & 1 deletion packages/parser/src/custom-operations/apply-traits.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,17 @@ function applyTraitsToObjectV2(value: Record<string, unknown>) {
}

const v3TraitPaths = [
// channels
'$.channels.*',
'$.components.channels.*',
// operations
'$.operations.*',
'$.operations.*.channel.messages.*',
'$.operations.*.messages.*',
'$.components.operations.*',
'$.components.operations.*.channel.messages.*',
'$.components.operations.*.messages.*',
// Channels
// messages inside channels
'$.channels.*.messages.*',
'$.components.channels.*.messages.*',
// messages
Expand Down
10 changes: 10 additions & 0 deletions packages/parser/src/models/channel-trait.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { BaseModel } from './base';
import type { ChannelParametersInterface } from './channel-parameters';
import type { CoreMixinInterface } from './mixins';
import type { ServersInterface } from './servers';

export interface ChannelTraitInterface extends BaseModel, CoreMixinInterface {
id(): string;
servers(): ServersInterface;
parameters(): ChannelParametersInterface;
}
4 changes: 4 additions & 0 deletions packages/parser/src/models/channel-traits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import type { Collection } from './collection';
import type { ChannelTraitInterface } from './channel-trait';

export type ChannelTraitsInterface = Collection<ChannelTraitInterface>;
2 changes: 2 additions & 0 deletions packages/parser/src/models/channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { BaseModel } from './base';
import type { ChannelParametersInterface } from './channel-parameters';
import type { ChannelTraitsInterface } from './channel-traits';
import type { MessagesInterface } from './messages';
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface } from './mixins';
import type { OperationsInterface } from './operations';
Expand All @@ -12,4 +13,5 @@ export interface ChannelInterface extends BaseModel, BindingsMixinInterface, Des
operations(): OperationsInterface;
messages(): MessagesInterface;
parameters(): ChannelParametersInterface;
traits(): ChannelTraitsInterface;
}
2 changes: 2 additions & 0 deletions packages/parser/src/models/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { ChannelsInterface } from './channels';
import type { MessagesInterface } from './messages';
import type { SchemasInterface } from './schemas';
import type { ChannelParametersInterface } from './channel-parameters';
import type { ChannelTraitsInterface } from './channel-traits';
import type { ServerVariablesInterface } from './server-variables';
import type { OperationTraitsInterface } from './operation-traits';
import type { MessageTraitsInterface } from './message-traits';
Expand All @@ -21,6 +22,7 @@ export interface ComponentsInterface extends BaseModel, ExtensionsMixinInterface
channelParameters(): ChannelParametersInterface;
serverVariables(): ServerVariablesInterface;
operations(): OperationsInterface;
channelTraits(): ChannelTraitsInterface;
operationTraits(): OperationTraitsInterface;
messageTraits(): MessageTraitsInterface;
correlationIds(): CorrelationIdsInterface;
Expand Down
2 changes: 2 additions & 0 deletions packages/parser/src/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export * from './channel-parameter';
export * from './channel-parameters';
export * from './channel';
export * from './channels';
export * from './channel-trait';
export * from './channel-traits';
export * from './collection';
export * from './components';
export * from './contact';
Expand Down
10 changes: 10 additions & 0 deletions packages/parser/src/models/v2/channel-traits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Collection } from '../collection';

import type { ChannelTraitsInterface } from '../channel-traits';
import type { ChannelTraitInterface } from '../channel-trait';

export class ChannelTraits extends Collection<ChannelTraitInterface> implements ChannelTraitsInterface {
override get(id: string): ChannelTraitInterface | undefined {
return this.collections.find(trait => trait.id() === id);
}
}
6 changes: 6 additions & 0 deletions packages/parser/src/models/v2/channel.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { BaseModel } from '../base';
import { ChannelTraits } from './channel-traits';
import { ChannelParameters } from './channel-parameters';
import { ChannelParameter } from './channel-parameter';
import { Messages } from './messages';
Expand All @@ -12,6 +13,7 @@ import { bindings, hasDescription, description, extensions } from './mixins';
import type { BindingsInterface } from '../bindings';
import type { ChannelInterface } from '../channel';
import type { ChannelParametersInterface } from '../channel-parameters';
import type { ChannelTraitsInterface } from '../channel-traits';
import type { ExtensionsInterface } from '../extensions';
import type { MessagesInterface } from '../messages';
import type { MessageInterface } from '../message';
Expand Down Expand Up @@ -81,6 +83,10 @@ export class Channel extends BaseModel<v2.ChannelObject, { id: string, address:
);
}

traits(): ChannelTraitsInterface {
return new ChannelTraits([]);
}

bindings(): BindingsInterface {
return bindings(this);
}
Expand Down
6 changes: 6 additions & 0 deletions packages/parser/src/models/v2/components.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseModel } from '../base';
import { Collection } from '../collection';
import { ChannelTraits } from './channel-traits';

import { Bindings } from './bindings';
import { Binding } from './binding';
Expand Down Expand Up @@ -37,6 +38,7 @@ import type { ChannelsInterface } from '../channels';
import type { MessagesInterface } from '../messages';
import type { SchemasInterface } from '../schemas';
import type { ChannelParametersInterface } from '../channel-parameters';
import type { ChannelTraitsInterface } from '../channel-traits';
import type { ServerVariablesInterface } from '../server-variables';
import type { OperationTraitsInterface } from '../operation-traits';
import type { SecuritySchemesInterface } from '../security-schemes';
Expand Down Expand Up @@ -75,6 +77,10 @@ export class Components extends BaseModel<v2.ComponentsObject> implements Compon
return this.createCollection('parameters', ChannelParameters, ChannelParameter);
}

channelTraits(): ChannelTraitsInterface {
return new ChannelTraits([]);
}

serverVariables(): ServerVariablesInterface {
return this.createCollection('serverVariables', ServerVariables, ServerVariable);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/parser/src/models/v2/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export { ChannelParameter as ChannelParameterV2 } from './channel-parameter';
export { ChannelParameters as ChannelParametersV2 } from './channel-parameters';
export { Channel as ChannelV2 } from './channel';
export { Channels as ChannelsV2 } from './channels';
export { ChannelTraits as ChannelTraitsV2 } from './channel-traits';
export { Components as ComponentsV2 } from './components';
export { Contact as ContactV2 } from './contact';
export { CorrelationId as CorrelationIdV2 } from './correlation-id';
Expand Down Expand Up @@ -34,4 +35,4 @@ export { ServerVariables as ServerVariablesV2 } from './server-variables';
export { Server as ServerV2 } from './server';
export { Servers as ServersV2 } from './servers';
export { Tag as TagV2 } from './tag';
export { Tags as TagsV2 } from './tags';
export { Tags as TagsV2 } from './tags';
39 changes: 39 additions & 0 deletions packages/parser/src/models/v3/channel-trait.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { ChannelParameters } from './channel-parameters';
import { ChannelParameter } from './channel-parameter';
import { Servers } from './servers';
import { Server } from './server';
import { CoreModel } from './mixins';

import type { ChannelTraitInterface } from '../channel-trait';
import type { ChannelParametersInterface } from '../channel-parameters';
import type { ServersInterface } from '../servers';
import type { ServerInterface } from '../server';
import type { v3 } from '../../spec-types';

export class ChannelTrait<J extends v3.ChannelTraitObject = v3.ChannelTraitObject> extends CoreModel<J, { id: string }> implements ChannelTraitInterface {
id(): string {
return this._meta.id;
}

servers(): ServersInterface {
const servers: ServerInterface[] = [];
const allowedServers = this._json.servers ?? [];
Object.entries(this._meta.asyncapi?.parsed.servers ?? {}).forEach(([serverName, server]) => {
if (allowedServers.length === 0 || allowedServers.includes(server)) {
servers.push(this.createModel(Server, server, { id: serverName, pointer: `/servers/${serverName}` }));
}
});
return new Servers(servers);
}

parameters(): ChannelParametersInterface {
return new ChannelParameters(
Object.entries(this._json.parameters ?? {}).map(([channelParameterName, channelParameter]) => {
return this.createModel(ChannelParameter, channelParameter as v3.ParameterObject, {
id: channelParameterName,
pointer: this.jsonPath(`parameters/${channelParameterName}`),
});
})
);
}
}
10 changes: 10 additions & 0 deletions packages/parser/src/models/v3/channel-traits.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Collection } from '../collection';

import type { ChannelTraitsInterface } from '../channel-traits';
import type { ChannelTraitInterface } from '../channel-trait';

export class ChannelTraits extends Collection<ChannelTraitInterface> implements ChannelTraitsInterface {
override get(id: string): ChannelTraitInterface | undefined {
return this.collections.find(trait => trait.id() === id);
}
}
14 changes: 14 additions & 0 deletions packages/parser/src/models/v3/channel.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { ChannelParameters } from './channel-parameters';
import { ChannelParameter } from './channel-parameter';
import { ChannelTrait } from './channel-trait';
import { ChannelTraits } from './channel-traits';
import { Messages } from './messages';
import { Message } from './message';
import { Operations } from './operations';
Expand All @@ -10,6 +12,7 @@ import { xParserObjectUniqueId } from '../../constants';
import { CoreModel } from './mixins';
import type { ChannelInterface } from '../channel';
import type { ChannelParametersInterface } from '../channel-parameters';
import type { ChannelTraitsInterface } from '../channel-traits';
import type { MessagesInterface } from '../messages';
import type { OperationsInterface } from '../operations';
import type { OperationInterface } from '../operation';
Expand Down Expand Up @@ -69,4 +72,15 @@ export class Channel extends CoreModel<v3.ChannelObject, { id: string }> impleme
})
);
}

traits(): ChannelTraitsInterface {
return new ChannelTraits(
(this._json.traits ?? []).map((trait, index) => {
return this.createModel(ChannelTrait, trait as v3.ChannelTraitObject, {
id: '',
pointer: this.jsonPath(`traits/${index}`),
});
})
);
}
}
7 changes: 7 additions & 0 deletions packages/parser/src/models/v3/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Collection } from '../collection';
import { Bindings } from './bindings';
import { Binding } from './binding';
import { Channel } from './channel';
import { ChannelTrait } from './channel-trait';
import { ChannelParameter } from './channel-parameter';
import { CorrelationId } from './correlation-id';
import { MessageTrait } from './message-trait';
Expand All @@ -17,6 +18,7 @@ import { ServerVariable } from './server-variable';
import { extensions } from './mixins';
import { Servers } from './servers';
import { Channels } from './channels';
import { ChannelTraits } from './channel-traits';
import { Messages } from './messages';
import { Schemas } from './schemas';
import { ChannelParameters } from './channel-parameters';
Expand Down Expand Up @@ -46,6 +48,7 @@ import type { ChannelsInterface } from '../channels';
import type { MessagesInterface } from '../messages';
import type { SchemasInterface } from '../schemas';
import type { ChannelParametersInterface } from '../channel-parameters';
import type { ChannelTraitsInterface } from '../channel-traits';
import type { ServerVariablesInterface } from '../server-variables';
import type { OperationTraitsInterface } from '../operation-traits';
import type { SecuritySchemesInterface } from '../security-schemes';
Expand Down Expand Up @@ -83,6 +86,10 @@ export class Components extends BaseModel<v3.ComponentsObject> implements Compon
return this.createCollection('parameters', ChannelParameters, ChannelParameter);
}

channelTraits(): ChannelTraitsInterface {
return this.createCollection('channelTraits', ChannelTraits, ChannelTrait);
}

serverVariables(): ServerVariablesInterface {
return this.createCollection('serverVariables', ServerVariables, ServerVariable);
}
Expand Down
2 changes: 2 additions & 0 deletions packages/parser/src/models/v3/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ export { ChannelParameter as ChannelParameterV3 } from './channel-parameter';
export { ChannelParameters as ChannelParametersV3 } from './channel-parameters';
export { Channel as ChannelV3 } from './channel';
export { Channels as ChannelsV3 } from './channels';
export { ChannelTrait as ChannelTraitV3 } from './channel-trait';
export { ChannelTraits as ChannelTraitsV3 } from './channel-traits';
export { Components as ComponentsV3 } from './components';
export { Contact as ContactV3 } from './contact';
export { CorrelationId as CorrelationIdV3 } from './correlation-id';
Expand Down
7 changes: 6 additions & 1 deletion packages/parser/src/spec-types/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,13 @@ export interface ServerBindingsObject extends SpecificationExtensions {

export type ChannelsObject = Record<string, ChannelObject | ReferenceObject>;

export interface ChannelObject extends SpecificationExtensions {
export interface ChannelObject extends ChannelTraitObject, SpecificationExtensions {
address?: string | null;
messages?: MessagesObject;
traits?: Array<ChannelTraitObject | ReferenceObject>;
}

export interface ChannelTraitObject extends SpecificationExtensions {
title?: string;
summary?: string;
description?: string;
Expand Down Expand Up @@ -257,6 +261,7 @@ export interface ComponentsObject extends SpecificationExtensions {
replies?: Record<string, OperationReplyObject | ReferenceObject>;
replyAddresses?: Record<string, OperationReplyAddressObject | ReferenceObject>;
correlationIds?: Record<string, CorrelationIDObject | ReferenceObject>;
channelTraits?: Record<string, ChannelTraitObject | ReferenceObject>;
operationTraits?: Record<string, OperationTraitObject | ReferenceObject>;
messageTraits?: Record<string, MessageTraitObject | ReferenceObject>;
tags?: Record<string, TagObject | ReferenceObject>;
Expand Down
Loading
Loading