Skip to content

Commit

Permalink
Merge branch 'next-major-spec' into fix/replyIds
Browse files Browse the repository at this point in the history
  • Loading branch information
smoya authored Nov 23, 2023
2 parents 918b5fe + 4345060 commit 4679ba5
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 52 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@asyncapi/parser",
"version": "3.0.0-next-major-spec.9",
"version": "3.0.0-next-major-spec.12",
"description": "JavaScript AsyncAPI parser.",
"bugs": {
"url": "https://github.com/asyncapi/parser-js/issues"
Expand Down
4 changes: 2 additions & 2 deletions src/document.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AsyncAPIDocumentV2, AsyncAPIDocumentV3 } from './models';
import { AsyncAPIDocumentV2, AsyncAPIDocumentV3, ParserAPIVersion } from './models';
import { unstringify } from './stringify';
import { createDetailedAsyncAPI } from './utils';

Expand Down Expand Up @@ -43,7 +43,7 @@ export function isAsyncAPIDocument(maybeDoc: unknown): maybeDoc is AsyncAPIDocum
}
if (maybeDoc && typeof (maybeDoc as AsyncAPIDocumentInterface).json === 'function') {
const versionOfParserAPI = (maybeDoc as AsyncAPIDocumentInterface).json()[xParserApiVersion];
return versionOfParserAPI === 2;
return versionOfParserAPI === ParserAPIVersion;
}
return false;
}
Expand Down
27 changes: 26 additions & 1 deletion src/models/utils.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import type { BaseModel, ModelMetadata } from './base';
import type { DetailedAsyncAPI } from '../types';
import { SchemaInterface } from './schema';
import { SchemaTypesToIterate, traverseAsyncApiDocument } from '../iterator';
import { AsyncAPIDocumentInterface } from './asyncapi';
import { SchemasInterface } from '../models';

export interface Constructor<T> extends Function {
new (...any: any[]): T;
new(...any: any[]): T;
}

export type InferModelData<T> = T extends BaseModel<infer J> ? J : never;
Expand Down Expand Up @@ -30,3 +34,24 @@ export function objectIdFromJSONPointer(path: string, data: any): string {

return data.split('/').pop().replace(/~1/, '/');
}

export function schemasFromDocument<T extends SchemasInterface>(document: AsyncAPIDocumentInterface, SchemasModel: Constructor<T>, includeComponents: boolean): T {
const jsonInstances: Set<any> = new Set();
const schemas: Set<SchemaInterface> = new Set();

function callback(schema: SchemaInterface) {
// comparing the reference (and not just the value) to the .json() object
if (!jsonInstances.has(schema.json())) {
jsonInstances.add(schema.json());
schemas.add(schema); // unique schemas
}
}

let toIterate = Object.values(SchemaTypesToIterate);
if (!includeComponents) {
toIterate = toIterate.filter(s => s !== SchemaTypesToIterate.Components);
}
traverseAsyncApiDocument(document, callback, toIterate);

return new SchemasModel(Array.from(schemas));
}
23 changes: 3 additions & 20 deletions src/models/v2/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import { SecurityScheme } from './security-scheme';
import { Schemas } from './schemas';

import { extensions } from './mixins';
import { traverseAsyncApiDocument, SchemaTypesToIterate } from '../../iterator';
import { tilde } from '../../utils';
import { schemasFromDocument } from '../utils';

import type { AsyncAPIDocumentInterface } from '../asyncapi';
import type { InfoInterface } from '../info';
Expand All @@ -27,7 +27,6 @@ import type { OperationInterface } from '../operation';
import type { MessagesInterface } from '../messages';
import type { MessageInterface } from '../message';
import type { SchemasInterface } from '../schemas';
import type { SchemaInterface } from '../schema';
import type { SecuritySchemesInterface } from '../security-schemes';
import type { ExtensionsInterface } from '../extensions';

Expand Down Expand Up @@ -81,7 +80,7 @@ export class AsyncAPIDocument extends BaseModel<v2.AsyncAPIObject> implements As
}

schemas(): SchemasInterface {
return this.__schemas(false);
return schemasFromDocument(this, Schemas, false);
}

securitySchemes(): SecuritySchemesInterface {
Expand Down Expand Up @@ -130,26 +129,10 @@ export class AsyncAPIDocument extends BaseModel<v2.AsyncAPIObject> implements As
}

allSchemas(): SchemasInterface {
return this.__schemas(true);
return schemasFromDocument(this, Schemas, true);
}

extensions(): ExtensionsInterface {
return extensions(this);
}

private __schemas(withComponents: boolean) {
const schemas: Set<SchemaInterface> = new Set();
function callback(schema: SchemaInterface) {
if (!schemas.has(schema.json())) {
schemas.add(schema);
}
}

let toIterate = Object.values(SchemaTypesToIterate);
if (!withComponents) {
toIterate = toIterate.filter(s => s !== SchemaTypesToIterate.Components);
}
traverseAsyncApiDocument(this, callback, toIterate);
return new Schemas(Array.from(schemas));
}
}
32 changes: 8 additions & 24 deletions src/models/v3/asyncapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { Schemas } from './schemas';

import { extensions } from './mixins';
import { tilde } from '../../utils';
import { SchemaTypesToIterate, traverseAsyncApiDocument } from '../../iterator';
import { schemasFromDocument } from '../utils';

import type { AsyncAPIDocumentInterface } from '../asyncapi';
import type { InfoInterface } from '../info';
Expand All @@ -26,12 +26,12 @@ import type { MessageInterface } from '../message';
import type { ComponentsInterface } from '../components';
import type { SecuritySchemesInterface } from '../security-schemes';
import type { ExtensionsInterface } from '../extensions';
import type { SchemaInterface } from '../schema';
import type { SchemasInterface } from '../schemas';
import type { OperationInterface } from '../operation';
import type { ChannelInterface } from '../channel';
import type { ServerInterface } from '../server';

import type { v3 } from '../../spec-types';
import { OperationInterface } from '../operation';
import { ChannelInterface } from '../channel';
import { ServerInterface } from '../server';

export class AsyncAPIDocument extends BaseModel<v3.AsyncAPIObject> implements AsyncAPIDocumentInterface {
version(): string {
Expand Down Expand Up @@ -89,8 +89,8 @@ export class AsyncAPIDocument extends BaseModel<v3.AsyncAPIObject> implements As
return new Messages(messages);
}

schemas() {
return this.__schemas(false);
schemas(): SchemasInterface {
return schemasFromDocument(this, Schemas, false);
}

securitySchemes(): SecuritySchemesInterface {
Expand Down Expand Up @@ -138,26 +138,10 @@ export class AsyncAPIDocument extends BaseModel<v3.AsyncAPIObject> implements As
}

allSchemas(): SchemasInterface {
return this.__schemas(true);
return schemasFromDocument(this, Schemas, true);
}

extensions(): ExtensionsInterface {
return extensions(this);
}

private __schemas(withComponents: boolean) {
const schemas: Set<SchemaInterface> = new Set();
function callback(schema: SchemaInterface) {
if (!schemas.has(schema.json())) {
schemas.add(schema);
}
}

let toIterate = Object.values(SchemaTypesToIterate);
if (!withComponents) {
toIterate = toIterate.filter(s => s !== SchemaTypesToIterate.Components);
}
traverseAsyncApiDocument(this, callback, toIterate);
return new Schemas(Array.from(schemas));
}
}
8 changes: 6 additions & 2 deletions test/document.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,8 +114,12 @@ describe('utils', function() {
expect(isAsyncAPIDocument(createAsyncAPIDocument(detailed))).toEqual(true);
});

it('document with the x-parser-api-version extension set to 2 should be AsyncAPI document', async function() {
expect(isAsyncAPIDocument({ json() { return { [xParserApiVersion]: 2 }; } })).toEqual(true);
it('document with the x-parser-api-version extension set to 3 should be AsyncAPI document', async function() {
expect(isAsyncAPIDocument({ json() { return { [xParserApiVersion]: 3 }; } })).toEqual(true);
});

it('document with the x-parser-api-version extension set to 2 should not be AsyncAPI document', async function() {
expect(isAsyncAPIDocument({ json() { return { [xParserApiVersion]: 2 }; } })).toEqual(false);
});

it('document with the x-parser-api-version extension set to 1 should not be AsyncAPI document', async function() {
Expand Down
8 changes: 8 additions & 0 deletions test/models/v2/asyncapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,14 @@ describe('AsyncAPIDocument model', function() {
const d = new AsyncAPIDocument(doc);
expect(d.allSchemas()).toBeInstanceOf(Schemas);
});

it('should return a collection of schemas (with schemas from components) without duplicates', function() {
const sharedMessage = { payload: {} };
const doc = serializeInput<v2.AsyncAPIObject>({ channels: { 'user/signup': { publish: { message: sharedMessage }, subscribe: { message: { oneOf: [{ payload: {} }, {}] } } }, 'user/logout': { publish: { message: { payload: {} } } } }, components: { messages: { aMessage: sharedMessage } } });
const d = new AsyncAPIDocument(doc);
expect(d.allSchemas()).toBeInstanceOf(Schemas);
expect(d.allSchemas()).toHaveLength(3);
});
});

describe('mixins', function() {
Expand Down
7 changes: 7 additions & 0 deletions test/models/v3/asyncapi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,13 @@ describe('AsyncAPIDocument model', function() {
const d = new AsyncAPIDocument(doc);
expect(d.allSchemas()).toBeInstanceOf(Schemas);
});
it('should return a collection of schemas (with schemas from components) without duplicates', function() {
const sharedMessage = { payload: {} };
const doc = serializeInput<v3.AsyncAPIObject>({ channels: { userSignup: { address: 'user/signup', messages: { someMessage1: { payload: {}}, someMessage2: sharedMessage } } }, components: { messages: { aMessage: sharedMessage } } });
const d = new AsyncAPIDocument(doc);
expect(d.allSchemas()).toBeInstanceOf(Schemas);
expect(d.allSchemas()).toHaveLength(2);
});
});

describe('.allServers()', function() {
Expand Down

0 comments on commit 4679ba5

Please sign in to comment.