Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat!: add schema union support #785

Closed
Closed
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
3 changes: 2 additions & 1 deletion src/models/message-trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import type { SchemaInterface } from './schema';

export interface MessageTraitInterface extends BaseModel, BindingsMixinInterface, DescriptionMixinInterface, ExtensionsMixinInterface, ExternalDocumentationMixinInterface, TagsMixinInterface {
id(): string;
schemaFormat(): string;
hasSchemaFormat(): boolean;
schemaFormat(): string | undefined;
smoya marked this conversation as resolved.
Show resolved Hide resolved
hasMessageId(): boolean;
messageId(): string | undefined;
hasCorrelationId(): boolean;
Expand Down
8 changes: 6 additions & 2 deletions src/models/v2/message-trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import { MessageExample } from './message-example';
import { Schema } from './schema';

import { xParserMessageName } from '../../constants';
import { getDefaultSchemaFormat } from '../../schema-parser';
import { bindings, hasDescription, description, extensions, hasExternalDocs, externalDocs, tags } from './mixins';
import { CoreModel } from './mixins';

import type { BindingsInterface } from '../bindings';
import type { CorrelationIdInterface } from '../correlation-id';
Expand All @@ -18,12 +17,17 @@ import type { SchemaInterface } from '../schema';
import type { TagsInterface } from '../tags';

import type { v2 } from '../../spec-types';
import { getDefaultSchemaFormat } from 'schema-parser';

export class MessageTrait<J extends v2.MessageTraitObject = v2.MessageTraitObject> extends BaseModel<J, { id: string }> implements MessageTraitInterface {
id(): string {
return this.messageId() || this._meta.id || this.json(xParserMessageName) as string;
}

hasSchemaFormat(): boolean {
return true;
}

schemaFormat(): string {
smoya marked this conversation as resolved.
Show resolved Hide resolved
return this._json.schemaFormat || getDefaultSchemaFormat(this._meta.asyncapi.semver.version);
}
Expand Down
12 changes: 8 additions & 4 deletions src/models/v3/message-trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,18 @@ export class MessageTrait<J extends v3.MessageTraitObject = v3.MessageTraitObjec
return this.messageId() || this._meta.id || this.extensions().get(xParserMessageName)?.value<string>() as string;
}

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

hasMessageId(): boolean {
return !!this._json.messageId;
}

hasSchemaFormat(): boolean {
return false;
}

schemaFormat(): string | undefined {
return undefined;
}

messageId(): string | undefined {
return this._json.messageId;
}
Expand Down
6 changes: 6 additions & 0 deletions src/models/v3/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ import type { ServerInterface } from '../server';
import type { SchemaInterface } from '../schema';

import type { v3 } from '../../spec-types';
import { getDefaultSchemaFormat } from 'schema-parser';

export class Message extends MessageTrait<v3.MessageObject> implements MessageInterface {
hasPayload(): boolean {
return !!this._json.payload;
}

schemaFormat(): string | undefined {
const payloadIsSchemaUnion = this._json.payload.schemaFormat !== undefined;
return payloadIsSchemaUnion ? this._json.payload.schemaFormat : getDefaultSchemaFormat(this._meta.asyncapi.semver.version);
Comment on lines +28 to +29
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const payloadIsSchemaUnion = this._json.payload.schemaFormat !== undefined;
return payloadIsSchemaUnion ? this._json.payload.schemaFormat : getDefaultSchemaFormat(this._meta.asyncapi.semver.version);
const payloadIsSchemaUnion = this._json.payload?.schemaFormat !== undefined;
return payloadIsSchemaUnion ? this._json.payload.schemaFormat : getDefaultSchemaFormat(this._meta.asyncapi.semver.version);

I think payload can be undefined, right? We should be careful then.

}

payload(): SchemaInterface | undefined {
if (!this._json.payload) return undefined;
return this.createModel(Schema, this._json.payload, { pointer: this.jsonPath('payload') });
Expand Down
9 changes: 7 additions & 2 deletions src/spec-types/v3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,14 @@ export interface OperationBindingsObject extends SpecificationExtensions {
export type MessagesObject = Record<string, MessageObject | ReferenceObject>;

export interface MessageObject extends MessageTraitObject, SpecificationExtensions {
payload?: any;
payload?: AllSchemaObjects;
traits?: Array<MessageTraitObject | ReferenceObject>;
}

export interface MessageTraitObject extends SpecificationExtensions {
messageId?: string;
headers?: SchemaObject;
correlationId?: CorrelationIDObject | ReferenceObject;
schemaFormat?: string;
contentType?: string;
name?: string;
title?: string;
Expand Down Expand Up @@ -398,6 +397,12 @@ export interface OAuthFlowObjectAuthorizationCode extends OAuthFlowObjectBase, S
authorizationUrl: string;
tokenUrl: string;
}
export interface MultiFormatSchemaObject {
schemaFormat: string;
schema: AsyncAPISchemaObject | ReferenceObject | any;
}

export type AllSchemaObjects = MultiFormatSchemaObject | AsyncAPISchemaObject | ReferenceObject | any;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


export type SchemaObject = AsyncAPISchemaObject | ReferenceObject;

Expand Down
13 changes: 13 additions & 0 deletions test/models/v2/message-trait.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,19 @@ describe('MessageTrait model', function() {
});
});

describe('.hasSchemaFormat()', function() {
it('should always contain schemaFormat even when not', function() {
const doc = { };
const d = new MessageTrait(doc, { asyncapi: {} as any, pointer: '', id: 'message' });
expect(d.hasSchemaFormat()).toEqual(true);
});
it('should always contain schemaFormat', function() {
const doc = { schemaFormat: 'customSchemaFormat' };
const d = new MessageTrait(doc, { asyncapi: {} as any, pointer: '', id: 'message' });
expect(d.hasSchemaFormat()).toEqual(true);
});
});

describe('.schemaFormat()', function() {
it('should return defined schemaFormat', function() {
const doc = { schemaFormat: 'customSchemaFormat' };
Expand Down
18 changes: 10 additions & 8 deletions test/models/v3/message-trait.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,19 @@ describe('MessageTrait model', function() {
});
});

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

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('.schemaFormat()', function() {
it('should return undefined schemaFormat', function() {
const doc = { };
const d = new MessageTrait(doc, { asyncapi: {} as any, pointer: '', id: 'message' });
expect(d.schemaFormat()).toEqual(undefined);
});
});

Expand Down
6 changes: 3 additions & 3 deletions test/models/v3/message.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,15 @@ describe('Message model', function() {

describe('.schemaFormat()', function() {
it('should return defined schemaFormat', function() {
const doc = { schemaFormat: 'customSchemaFormat' };
const doc = { payload: {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');
const d = new Message(doc, { asyncapi: { semver: { version: '3.0.0' } } as any, pointer: '', id: 'message' });
expect(d.schemaFormat()).toEqual('application/vnd.aai.asyncapi;version=3.0.0');
});
});

Expand Down