Skip to content

Commit

Permalink
feat: add support for multi-format schema (#814)
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya authored Jul 28, 2023
1 parent 21f2744 commit 9b79e90
Show file tree
Hide file tree
Showing 15 changed files with 236 additions and 162 deletions.
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;
hasMessageId(): boolean;
messageId(): string | undefined;
hasCorrelationId(): boolean;
Expand Down
1 change: 1 addition & 0 deletions src/models/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export interface SchemaInterface extends BaseModel<v2.AsyncAPISchemaObject>, Ext
propertyNames(): SchemaInterface | undefined;
readOnly(): boolean | undefined;
required(): Array<string> | undefined;
schemaFormat(): string
then(): SchemaInterface | undefined;
title(): string | undefined;
type(): string | Array<string> | undefined;
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,8 @@ 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 { getDefaultSchemaFormat } from '../../schema-parser';

import type { BindingsInterface } from '../bindings';
import type { CorrelationIdInterface } from '../correlation-id';
Expand All @@ -24,7 +24,11 @@ export class MessageTrait<J extends v2.MessageTraitObject = v2.MessageTraitObjec
return this.messageId() || this._meta.id || this.json(xParserMessageName) as string;
}

schemaFormat(): string {
hasSchemaFormat(): boolean {
return this.schemaFormat() !== undefined;
}

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

Expand Down
2 changes: 1 addition & 1 deletion src/models/v2/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class Message extends MessageTrait<v2.MessageObject> implements MessageIn

payload(): SchemaInterface | undefined {
if (!this._json.payload) return undefined;
return this.createModel(Schema, this._json.payload, { pointer: `${this._meta.pointer}/payload` });
return this.createModel(Schema, this._json.payload, { pointer: `${this._meta.pointer}/payload`, schemaFormat: this._json.schemaFormat });
}

servers(): ServersInterface {
Expand Down
7 changes: 6 additions & 1 deletion src/models/v2/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@ import { BaseModel } from '../base';

import { xParserSchemaId } from '../../constants';
import { extensions, hasExternalDocs, externalDocs } from './mixins';
import { getDefaultSchemaFormat } from '../../schema-parser';

import type { ExtensionsInterface } from '../extensions';
import type { ExternalDocumentationInterface } from '../external-docs';
import type { SchemaInterface } from '../schema';

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

export class Schema extends BaseModel<v2.AsyncAPISchemaObject, { id?: string, parent?: Schema }> implements SchemaInterface {
export class Schema extends BaseModel<v2.AsyncAPISchemaObject, { id?: string, parent?: Schema, schemaFormat?: string }> implements SchemaInterface {
id(): string {
return this.$id() || this._meta.id || this.json(xParserSchemaId as any) as string;
}
Expand Down Expand Up @@ -267,6 +268,10 @@ export class Schema extends BaseModel<v2.AsyncAPISchemaObject, { id?: string, pa
return this._json.required;
}

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

then(): SchemaInterface | undefined {
if (typeof this._json === 'boolean' || typeof this._json.then !== 'object') return;
return this.createModel(Schema, this._json.then, { pointer: `${this._meta.pointer}/then`, parent: this });
Expand Down
13 changes: 8 additions & 5 deletions src/models/v3/message-trait.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { MessageExample } from './message-example';
import { Schema } from './schema';

import { xParserMessageName } from '../../constants';
import { getDefaultSchemaFormat } from '../../schema-parser';
import { CoreModel } from './mixins';

import type { CorrelationIdInterface } from '../correlation-id';
Expand All @@ -19,14 +18,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
15 changes: 14 additions & 1 deletion src/models/v3/message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,20 @@ export class Message extends MessageTrait<v3.MessageObject> implements MessageIn

payload(): SchemaInterface | undefined {
if (!this._json.payload) return undefined;
return this.createModel(Schema, this._json.payload, { pointer: this.jsonPath('payload') });
return this.createModel(Schema, this._json.payload, { pointer: this.jsonPath('payload')});
}

hasSchemaFormat(): boolean {
// If it has a payload, schema format is expected (at least the default)
return this.hasPayload();
}

schemaFormat(): string | undefined {
if (this.hasSchemaFormat()) {
return this.payload()?.schemaFormat();
}

return undefined;
}

servers(): ServersInterface {
Expand Down
Loading

0 comments on commit 9b79e90

Please sign in to comment.