Skip to content

Commit

Permalink
fix: remove action and channel fields from Operation Trait Object (#674)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Nov 15, 2022
1 parent 6de1923 commit 2590f56
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 126 deletions.
8 changes: 1 addition & 7 deletions src/models/operation-trait.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import type { BaseModel } from './base';
import type { ChannelsInterface } from './channels';
import type { OperationAction } from './operation';
import type { SecurityRequirements } from './security-requirements';
import type { BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface } from './mixins';

export interface OperationTraitInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
id(): string | undefined;
hasId(): boolean;
action(): OperationAction | undefined;
isSend(): boolean;
isReceive(): boolean;
id(): string | undefined;
hasSummary(): boolean;
summary(): string | undefined;
security(): SecurityRequirements[];
channels(): ChannelsInterface;
}
6 changes: 5 additions & 1 deletion src/models/operation.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
import type { BaseModel } from './base';
import type { MessagesInterface } from './messages';
import type { OperationTraitsInterface } from './operation-traits';
import type { OperationTraitInterface } from './operation-trait';
import type { ChannelsInterface } from './channels';
import type { ServersInterface } from './servers';
import type { MessagesInterface } from './messages';

export type OperationAction = 'send' | 'receive' | 'publish' | 'subscribe';

export interface OperationInterface extends BaseModel, OperationTraitInterface {
action(): OperationAction;
isSend(): boolean;
isReceive(): boolean;
servers(): ServersInterface;
channels(): ChannelsInterface
messages(): MessagesInterface;
traits(): OperationTraitsInterface;
}
18 changes: 0 additions & 18 deletions src/models/v2/operation-trait.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { BaseModel } from '../base';
import { Channels } from '../channels';
import { SecurityScheme } from './security-scheme';
import { SecurityRequirements } from '../security-requirements';
import { SecurityRequirement } from './security-requirement';
Expand All @@ -9,7 +8,6 @@ import { bindings, hasDescription, description, extensions, hasExternalDocs, ext
import type { BindingsInterface } from '../bindings';
import type { ExtensionsInterface } from '../extensions';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { ChannelsInterface } from '../channels';
import type { OperationAction } from '../operation';
import type { OperationTraitInterface } from '../operation-trait';
import type { TagsInterface } from '../tags';
Expand All @@ -25,10 +23,6 @@ export class OperationTrait<J extends v2.OperationTraitObject = v2.OperationTrai
return this.id() !== undefined;
}

action(): OperationAction | undefined {
return this._meta.action;
}

hasSummary(): boolean {
return !!this._json.summary;
}
Expand All @@ -53,14 +47,6 @@ export class OperationTrait<J extends v2.OperationTraitObject = v2.OperationTrai
return externalDocs(this);
}

isSend(): boolean {
return this.action() === 'subscribe';
}

isReceive(): boolean {
return this.action() === 'publish';
}

security(): SecurityRequirements[] {
const securitySchemes = (this._meta?.asyncapi?.parsed?.components?.securitySchemes || {}) as Record<string, v2.SecuritySchemeObject>;
return (this._json.security || []).map((requirement, index) => {
Expand All @@ -75,10 +61,6 @@ export class OperationTrait<J extends v2.OperationTraitObject = v2.OperationTrai
});
}

channels(): ChannelsInterface {
return new Channels([]);
}

tags(): TagsInterface {
return tags(this);
}
Expand Down
8 changes: 8 additions & 0 deletions src/models/v2/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ export class Operation extends OperationTrait<v2.OperationObject> implements Ope
return this._meta.action;
}

isSend(): boolean {
return this.action() === 'subscribe';
}

isReceive(): boolean {
return this.action() === 'publish';
}

servers(): ServersInterface {
const servers: ServerInterface[] = [];
const serversData: any[] = [];
Expand Down
26 changes: 0 additions & 26 deletions src/models/v3/operation-trait.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { BaseModel } from '../base';
// import { Channels } from '../channels';
// import { Channel } from './channel';
import { SecurityScheme } from './security-scheme';
import { SecurityRequirements } from '../security-requirements';
import { SecurityRequirement } from './security-requirement';
Expand All @@ -10,8 +8,6 @@ import { bindings, hasDescription, description, extensions, hasExternalDocs, ext
import type { BindingsInterface } from '../bindings';
import type { ExtensionsInterface } from '../extensions';
import type { ExternalDocumentationInterface } from '../external-documentation';
import type { ChannelsInterface } from '../channels';
import type { OperationAction } from '../operation';
import type { OperationTraitInterface } from '../operation-trait';
import type { TagsInterface } from '../tags';

Expand All @@ -26,10 +22,6 @@ export class OperationTrait<J extends v3.OperationTraitObject = v3.OperationTrai
return this.id() !== undefined;
}

action(): OperationAction | undefined {
return this._json.action;
}

hasSummary(): boolean {
return !!this._json.summary;
}
Expand All @@ -54,24 +46,6 @@ export class OperationTrait<J extends v3.OperationTraitObject = v3.OperationTrai
return externalDocs(this);
}

isSend(): boolean {
return this.action() === 'send';
}

isReceive(): boolean {
return this.action() === 'receive';
}

channels(): ChannelsInterface {
// if (this._json.channel) {
// return new Channels([
// this.createModel(Channel, this._json.channel as v3.ChannelObject, { id: '', pointer: this.jsonPath('channel') })
// ]);
// }
// return new Channels([]);
return [] as any;
}

security(): SecurityRequirements[] {
const securitySchemes = (this._meta?.asyncapi?.parsed?.components?.securitySchemes || {}) as Record<string, v3.SecuritySchemeObject>;
return (this._json.security || []).map((requirement, index) => {
Expand Down
8 changes: 8 additions & 0 deletions src/models/v3/operation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ export class Operation extends OperationTrait<v3.OperationObject> implements Ope
return this._json.action;
}

isSend(): boolean {
return this.action() === 'send';
}

isReceive(): boolean {
return this.action() === 'receive';
}

servers(): ServersInterface {
const servers: ServerInterface[] = [];
const serversData: any[] = [];
Expand Down
2 changes: 0 additions & 2 deletions src/spec-types/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,6 @@ export interface OperationObject extends SpecificationExtensions {
}

export interface OperationTraitObject extends SpecificationExtensions {
action?: 'send' | 'receive';
channel?: ChannelObject | ReferenceObject;
summary?: string;
description?: string;
security?: Array<SecurityRequirementObject>;
Expand Down
36 changes: 0 additions & 36 deletions test/models/v2/operation-trait.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,42 +34,6 @@ describe('OperationTrait model', function() {
});
});

describe('.action()', function() {
it('should return kind/action of operation', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.action()).toEqual('publish');
});
});

describe('.isSend()', function() {
it('should return true when operation is subscribe', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'subscribe' });
expect(d.isSend()).toBeTruthy();
});

it('should return false when operation is publish', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.isSend()).toBeFalsy();
});
});

describe('.isReceive()', function() {
it('should return true when operation is publish', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.isReceive()).toBeTruthy();
});

it('should return false when operation is subscribe', function() {
const doc = {};
const d = new OperationTrait(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'subscribe' });
expect(d.isReceive()).toBeFalsy();
});
});

describe('.hasSummary()', function() {
it('should return true when there is a value', function() {
const doc = { summary: '...' };
Expand Down
36 changes: 36 additions & 0 deletions test/models/v2/operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,42 @@ describe('Operation model', function() {
});
});

describe('.action()', function() {
it('should return kind/action of operation', function() {
const doc = {};
const d = new Operation(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.action()).toEqual('publish');
});
});

describe('.isSend()', function() {
it('should return true when operation is subscribe', function() {
const doc = {};
const d = new Operation(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'subscribe' });
expect(d.isSend()).toBeTruthy();
});

it('should return false when operation is publish', function() {
const doc = {};
const d = new Operation(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.isSend()).toBeFalsy();
});
});

describe('.isReceive()', function() {
it('should return true when operation is publish', function() {
const doc = {};
const d = new Operation(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'publish' });
expect(d.isReceive()).toBeTruthy();
});

it('should return false when operation is subscribe', function() {
const doc = {};
const d = new Operation(doc, { asyncapi: {} as any, pointer: '', id: 'trait', action: 'subscribe' });
expect(d.isReceive()).toBeFalsy();
});
});

describe('.servers()', function() {
it('should return collection of servers - channel available on all servers', function() {
const doc = {};
Expand Down
34 changes: 0 additions & 34 deletions test/models/v3/operation-trait.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,40 +32,6 @@ describe('OperationTrait model', function() {
});
});

describe('.action()', function() {
it('should return kind/action of operation', function() {
const d = new OperationTrait({ action: 'send', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'trait' });
expect(d.action()).toEqual('send');
});
});

describe('.isSend()', function() {
it('should return true when operation has send action', function() {
const d = new OperationTrait({ action: 'send', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'trait' });
expect(d.isSend()).toBeTruthy();
});

it('should return false when operation has receive action', function() {
const doc = {};
const d = new OperationTrait({ action: 'receive', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'trait' });
expect(d.isSend()).toBeFalsy();
});
});

describe('.isReceive()', function() {
it('should return true when operation has receive action', function() {
const doc = {};
const d = new OperationTrait({ action: 'receive', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'trait' });
expect(d.isReceive()).toBeTruthy();
});

it('should return false when operation has send action', function() {
const doc = {};
const d = new OperationTrait({ action: 'send', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'trait' });
expect(d.isReceive()).toBeFalsy();
});
});

describe('.hasSummary()', function() {
it('should return true when there is a value', function() {
const doc = { summary: '...' };
Expand Down
38 changes: 36 additions & 2 deletions test/models/v3/operation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,47 @@ import { Server } from '../../../src/models/v3/server';
import { assertBindings, assertDescription, assertExtensions, assertExternalDocumentation, assertTags } from './utils';

describe('Operation model', function() {
describe('.id()', function() {
it('should return operationId', function() {
const d = new Operation({ action: 'send', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'operation' });
expect(d.id()).toEqual('operation');
});
});

describe('.action()', function() {
it('should return kind/action of operation', function() {
const d = new Operation({ action: 'send', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'trait' });
const d = new Operation({ action: 'send', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'operation' });
expect(d.action()).toEqual('send');
});
});

describe('.isSend()', function() {
it('should return true when operation has send action', function() {
const d = new Operation({ action: 'send', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'operation' });
expect(d.isSend()).toBeTruthy();
});

it('should return false when operation has receive action', function() {
const doc = {};
const d = new Operation({ action: 'receive', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'operation' });
expect(d.isSend()).toBeFalsy();
});
});

describe('.isReceive()', function() {
it('should return true when operation has receive action', function() {
const doc = {};
const d = new Operation({ action: 'receive', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'operation' });
expect(d.isReceive()).toBeTruthy();
});

it('should return false when operation has send action', function() {
const doc = {};
const d = new Operation({ action: 'send', channel: {} }, { asyncapi: {} as any, pointer: '', id: 'operation' });
expect(d.isReceive()).toBeFalsy();
});
});

describe('.servers()', function() {
it('should return collection of servers - channel available on all servers', function() {
const d = new Operation({ action: 'send', channel: {} }, { asyncapi: { parsed: { servers: { production: {}, development: {}, } } } as any, pointer: '', id: 'operation' });
Expand Down Expand Up @@ -76,7 +110,7 @@ describe('Operation model', function() {

describe('.traits()', function() {
it('should return collection of traits', function() {
const d = new Operation({ action: 'send', channel: {}, traits: [{ action: 'receive' }] });
const d = new Operation({ action: 'send', channel: {}, traits: [{}] });
expect(d.traits()).toBeInstanceOf(OperationTraits);
expect(d.traits().all()).toHaveLength(1);
expect(d.traits().all()[0]).toBeInstanceOf(OperationTrait);
Expand Down

0 comments on commit 2590f56

Please sign in to comment.