From 2590f5663ddbaec9c760837e14e31d0cea77cee1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maciej=20Urba=C5=84czyk?= Date: Tue, 15 Nov 2022 09:32:51 +0100 Subject: [PATCH] fix: remove action and channel fields from Operation Trait Object (#674) --- src/models/operation-trait.ts | 8 +----- src/models/operation.ts | 6 +++- src/models/v2/operation-trait.ts | 18 ------------ src/models/v2/operation.ts | 8 ++++++ src/models/v3/operation-trait.ts | 26 ------------------ src/models/v3/operation.ts | 8 ++++++ src/spec-types/v3.ts | 2 -- test/models/v2/operation-trait.spec.ts | 36 ------------------------ test/models/v2/operation.spec.ts | 36 ++++++++++++++++++++++++ test/models/v3/operation-trait.spec.ts | 34 ----------------------- test/models/v3/operation.spec.ts | 38 ++++++++++++++++++++++++-- 11 files changed, 94 insertions(+), 126 deletions(-) diff --git a/src/models/operation-trait.ts b/src/models/operation-trait.ts index c22c8aa56..b92c6d82a 100644 --- a/src/models/operation-trait.ts +++ b/src/models/operation-trait.ts @@ -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; } diff --git a/src/models/operation.ts b/src/models/operation.ts index 854c76a9f..ef6c9516a 100644 --- a/src/models/operation.ts +++ b/src/models/operation.ts @@ -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; } diff --git a/src/models/v2/operation-trait.ts b/src/models/v2/operation-trait.ts index d7910b6dd..ffa124f43 100644 --- a/src/models/v2/operation-trait.ts +++ b/src/models/v2/operation-trait.ts @@ -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'; @@ -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'; @@ -25,10 +23,6 @@ export class OperationTrait; return (this._json.security || []).map((requirement, index) => { @@ -75,10 +61,6 @@ export class OperationTrait 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[] = []; diff --git a/src/models/v3/operation-trait.ts b/src/models/v3/operation-trait.ts index 1f5e158d0..44a898ef8 100644 --- a/src/models/v3/operation-trait.ts +++ b/src/models/v3/operation-trait.ts @@ -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'; @@ -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'; @@ -26,10 +22,6 @@ export class OperationTrait; return (this._json.security || []).map((requirement, index) => { diff --git a/src/models/v3/operation.ts b/src/models/v3/operation.ts index 7d6568517..6f12ac880 100644 --- a/src/models/v3/operation.ts +++ b/src/models/v3/operation.ts @@ -20,6 +20,14 @@ export class Operation extends OperationTrait 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[] = []; diff --git a/src/spec-types/v3.ts b/src/spec-types/v3.ts index 6ce81c9cb..df2d66d23 100644 --- a/src/spec-types/v3.ts +++ b/src/spec-types/v3.ts @@ -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; diff --git a/test/models/v2/operation-trait.spec.ts b/test/models/v2/operation-trait.spec.ts index 363bd0fb2..149d93f25 100644 --- a/test/models/v2/operation-trait.spec.ts +++ b/test/models/v2/operation-trait.spec.ts @@ -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: '...' }; diff --git a/test/models/v2/operation.spec.ts b/test/models/v2/operation.spec.ts index abfdc0ede..1a0c627d9 100644 --- a/test/models/v2/operation.spec.ts +++ b/test/models/v2/operation.spec.ts @@ -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 = {}; diff --git a/test/models/v3/operation-trait.spec.ts b/test/models/v3/operation-trait.spec.ts index 6601ee495..43d17acee 100644 --- a/test/models/v3/operation-trait.spec.ts +++ b/test/models/v3/operation-trait.spec.ts @@ -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: '...' }; diff --git a/test/models/v3/operation.spec.ts b/test/models/v3/operation.spec.ts index 887ada4a7..bea5f06ae 100644 --- a/test/models/v3/operation.spec.ts +++ b/test/models/v3/operation.spec.ts @@ -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' }); @@ -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);