Skip to content

Commit

Permalink
refactor: add schemaFormat method to the Message model (#582)
Browse files Browse the repository at this point in the history
  • Loading branch information
magicmatatjahu authored Aug 31, 2022
1 parent 1764391 commit 0f8bd39
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/models/message-trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type { SchemaInterface } from "./schema";

export interface MessageTraitInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
id(): string;
schemaFormat(): string;
hasMessageId(): boolean;
messageId(): string | undefined;
hasCorrelationId(): boolean;
Expand Down
6 changes: 6 additions & 0 deletions src/models/v2/message-trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { MessageExamples } from './message-examples';
import { MessageExample } from './message-example';
import { Schema } from './schema';

import { getDefaultSchemaFormat } from '../../schema-parser';

import { Mixin } from '../utils';
import { BindingsMixin } from './mixins/bindings';
import { DescriptionMixin } from './mixins/description';
Expand All @@ -29,6 +31,10 @@ export class MessageTrait extends Mixin(BaseModel, BindingsMixin, DescriptionMix
return this.messageId() || this._meta.id;
}

schemaFormat(): string {
return this._json.schemaFormat || getDefaultSchemaFormat(this._meta.asyncapi.semver.version);
}

hasMessageId(): boolean {
return !!this._json.messageId;
}
Expand Down
14 changes: 14 additions & 0 deletions test/models/v2/message-trait.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,20 @@ describe('MessageTrait model', function() {
});
});

describe('.schemaFormat()', function() {
it('should return defined schemaFormat', function() {
const doc = { schemaFormat: 'customSchemaFormat' };
const d = new MessageTrait(doc, { asyncapi: {} as any, pointer: '', id: 'message' });
expect(d.schemaFormat()).toEqual('customSchemaFormat');
});

it('should return default schemaFormat if schemaFormat field is absent', function() {
const doc = {};
const d = new MessageTrait(doc, { asyncapi: { semver: { version: '2.0.0' } } as any, pointer: '', id: 'message' });
expect(d.schemaFormat()).toEqual('application/vnd.aai.asyncapi;version=2.0.0');
});
});

describe('.hasMessageId()', function() {
it('should return true when there is a value', function() {
const doc = { messageId: '...' };
Expand Down
14 changes: 14 additions & 0 deletions test/models/v2/message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,20 @@ describe('Message model', function() {
});
});

describe('.schemaFormat()', function() {
it('should return defined schemaFormat', function() {
const doc = { schemaFormat: 'customSchemaFormat' };
const d = new Message(doc, { asyncapi: {} as any, pointer: '', id: 'message' });
expect(d.schemaFormat()).toEqual('customSchemaFormat');
});

it('should return default schemaFormat if schemaFormat field is absent', function() {
const doc = {};
const d = new Message(doc, { asyncapi: { semver: { version: '2.0.0' } } as any, pointer: '', id: 'message' });
expect(d.schemaFormat()).toEqual('application/vnd.aai.asyncapi;version=2.0.0');
});
});

describe('.hasPayload()', function() {
it('should return true when there is a value', function() {
const doc = { payload: {} };
Expand Down

0 comments on commit 0f8bd39

Please sign in to comment.