Skip to content
This repository was archived by the owner on Aug 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #3 from smoya/feat/majorVersion
Browse files Browse the repository at this point in the history
feat!: change Parser-API version args to represent just the major
  • Loading branch information
smoya authored Oct 4, 2023
2 parents d42ea8a + 6a38e4a commit ffae2b4
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 49 deletions.
26 changes: 13 additions & 13 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
Expand Up @@ -43,7 +43,7 @@
"@asyncapi/protobuf-schema-parser": "^3.0.0",
"@asyncapi/raml-dt-schema-parser": "^4.0.4",
"parserv2": "npm:@asyncapi/parser@^2.1.0",
"parserv3": "npm:@asyncapi/parser@^2.2.0-next-major-spec.2"
"parserv3": "npm:@asyncapi/parser@^3.0.0-next-major-spec.3"
},
"devDependencies": {
"@jest/types": "^29.0.2",
Expand Down
12 changes: 4 additions & 8 deletions src/convert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,22 @@ import type { AsyncAPIDocumentInterface as AsyncAPIDocumentInterfaceParserV3 } f
import type { DetailedAsyncAPI as DetailedAsyncAPIParserV2 } from 'parserv2/esm/types';
import type { DetailedAsyncAPI as DetailedAsyncAPIParserV3 } from 'parserv3/esm/types';

import { majorParserAPIVersion } from './utils';

export type AsyncAPIDocument = AsyncAPIDocumentInterfaceParserV2 | AsyncAPIDocumentInterfaceParserV3;

export function ConvertDocumentParserAPIVersion(doc: AsyncAPIDocument, toParserAPIVersion: string): AsyncAPIDocument {
export function ConvertDocumentParserAPIVersion(doc: AsyncAPIDocument, toParserAPIMajorVersion: number): AsyncAPIDocument {
if (!doc || !doc.json) return doc;

const docParserAPI = doc.extensions().get('x-parser-api-version')?.value();
const docParserAPIMajorVersion: number = docParserAPI ? Number(String(docParserAPI).split('.')[0]) : 0;
const toMajorParserAPIVersion = majorParserAPIVersion(toParserAPIVersion);
const docParserAPIMajorVersion: number = docParserAPI || 0;

if (docParserAPIMajorVersion === toMajorParserAPIVersion) {
if (docParserAPIMajorVersion === toParserAPIMajorVersion) {
return doc; // Nothing to do
}

const detailedAsyncAPI = doc.meta().asyncapi;
switch (toMajorParserAPIVersion) {
switch (toParserAPIMajorVersion) {
case 1:
return createAsyncAPIDocumentParserV2(detailedAsyncAPI as DetailedAsyncAPIParserV2);
// break;
case 2:
return createAsyncAPIDocumentParserV3(detailedAsyncAPI as DetailedAsyncAPIParserV3);
default:
Expand Down
10 changes: 3 additions & 7 deletions src/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { ProtoBuffSchemaParser } from '@asyncapi/protobuf-schema-parser';
import type { ParserOptions as ParserOptionsParserV2 } from 'parserv2/esm/parser';
import type { ParserOptions as ParserOptionsParserV3 } from 'parserv3/esm/parser';

import { majorParserAPIVersion } from './utils';

export type ParserOptions = ParserOptionsParserV2 | ParserOptionsParserV3;
export type Options = {
includeSchemaParsers?: boolean;
Expand All @@ -19,7 +17,7 @@ export type Options = {

type Parser = ParserV2 | ParserV3;

export function NewParser(parserAPIVersion: string, options?: Options): Parser {
export function NewParser(parserAPIMajorVersion?: number, options?: Options): Parser {
const parserOptions: ParserOptions = options?.parserOptions || {};

// This is done globally instead of per version because latest versions of those schema parsers are still compatible with newer versions of the Parser-JS.
Expand All @@ -41,13 +39,11 @@ export function NewParser(parserAPIVersion: string, options?: Options): Parser {
parserOptions.schemaParsers.push(...filteredDefaultSchemas as any);
}
}

const parserAPIMajorVersion = majorParserAPIVersion(parserAPIVersion);

switch (parserAPIMajorVersion) {
case 1:
return new ParserV2(parserOptions as ParserOptionsParserV2);
default:
case 0: // Using Parser v3 (latest atm) by default
default: // default to latest version
case 2:
return new ParserV3(parserOptions as ParserOptionsParserV3);
}
Expand Down
5 changes: 0 additions & 5 deletions src/utils.ts

This file was deleted.

13 changes: 7 additions & 6 deletions test/convert.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,30 @@ describe('ConvertDocumentParserAPIVersion()', function() {
const doc = { asyncapi: '2.6.0', info: { title: '', description: '', version: ''}, channels: {} };
const parsedDocParserV2 = (await new ParserV2().parse(doc)).document as AsyncAPIDocument;

// Even though the value here is '1' for Parser V3, we force to be '2.0.0' for this test to do the equality check later.
parsedDocParserV2['_json']['x-parser-api-version'] = '2.0.0';
// Even though the value here is 1 for Parser V3, we force to be 2 for this test to do the equality check later.
parsedDocParserV2['_json']['x-parser-api-version'] = 2;

const parsedDocParserV3 = await new ParserV3().parse(doc);
const convertedDoc = ConvertDocumentParserAPIVersion(parsedDocParserV2, '2.0.0');
const convertedDoc = ConvertDocumentParserAPIVersion(parsedDocParserV2, 2);

expect(convertedDoc).toEqual(parsedDocParserV3.document);
});

it('Converts from Parser-API v2 to Parser-API v1', async function() {
const doc = { asyncapi: '2.6.0', info: { title: '', description: '', version: ''}, channels: {} };
const parsedDocParserV3 = (await new ParserV3().parse(doc)).document as AsyncAPIDocument;

// Even though the value here is '2.0.0' for Parser V3, we force to be 1 for this test to do the equality check later.
// Even though the value here is 2 for Parser V3, we force to be 1 for this test to do the equality check later.
parsedDocParserV3['_json']['x-parser-api-version'] = 1;

const parsedDocParserV2 = (await new ParserV2().parse(doc)).document;
const convertedDoc = ConvertDocumentParserAPIVersion(parsedDocParserV3, '1.0.0');
const convertedDoc = ConvertDocumentParserAPIVersion(parsedDocParserV3, 1);
expect(convertedDoc).toEqual(parsedDocParserV2);
});

it('Skips converting if no document is passed', async function() {
const doc = { } as AsyncAPIDocument;
const convertedDoc = ConvertDocumentParserAPIVersion(doc, '1.0.0');
const convertedDoc = ConvertDocumentParserAPIVersion(doc, 1);
expect(convertedDoc).toEqual(doc);
});
});
18 changes: 9 additions & 9 deletions test/parse.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@ const fakeSchemaParser = {

describe('NewParser()', function() {
it('Creates a Parser without options compatible with Parser-API v1 and caches it', async function() {
const parser = NewParser('1.0.0');
const parser = NewParser(1);
expect(parser).toBeInstanceOf(ParserV2);
});

it('Creates a Parser with options compatible with Parser-API v1', async function() {
const options: Options = { parserOptions: { schemaParsers: [fakeSchemaParser]} };
const parser = NewParser('1.0.0', options);
const parser = NewParser(1, options);

expect(parser).toBeInstanceOf(ParserV2);
expect(parser.parserRegistry.get('fake-format')).not.toBeUndefined();
Expand All @@ -33,7 +33,7 @@ describe('NewParser()', function() {
it('Creates a Parser with options including known Schema Parsers and do not overwrite those with Parser-API v1', async function() {
const knownSchemaParser = AvroSchemaParser();
const options: Options = { parserOptions: { schemaParsers: [knownSchemaParser]}, includeSchemaParsers: true };
const parser = NewParser('1.0.0', options);
const parser = NewParser(1, options);

expect(parser).toBeInstanceOf(ParserV2);
expect(parser.parserRegistry.get(knownSchemaParser.getMimeTypes()[0])).toStrictEqual(knownSchemaParser);
Expand All @@ -43,21 +43,21 @@ describe('NewParser()', function() {
});

it('Creates a Parser without options compatible with Parser-API v2', async function() {
const parser = NewParser('2.0.0');
const parser = NewParser(2);
expect(parser).toBeInstanceOf(ParserV3);
});

it('Creates a Parser with options compatible with Parser-API v2', async function() {
const options: Options = { parserOptions: { schemaParsers: [fakeSchemaParser]} };
const parser = NewParser('2.0.0', options);
const parser = NewParser(2, options);
expect(parser).toBeInstanceOf(ParserV3);
expect(parser.parserRegistry.get('fake-format')).not.toBeUndefined();
});

it('Creates a Parser with options including known Schema Parsers and do not overwrite those with Parser-API v2', async function() {
const knownSchemaParser = AvroSchemaParser();
const options: Options = { parserOptions: { schemaParsers: [knownSchemaParser]}, includeSchemaParsers: true };
const parser = NewParser('2.0.0', options);
const parser = NewParser(2, options);

expect(parser).toBeInstanceOf(ParserV3);
expect(parser.parserRegistry.get(knownSchemaParser.getMimeTypes()[0])).toStrictEqual(knownSchemaParser);
Expand All @@ -67,21 +67,21 @@ describe('NewParser()', function() {
});

it('Creates a Parser without options compatible with old Parser API (AKA v0)', async function() {
const parser = NewParser(''); // could be '0.0.0' as well
const parser = NewParser(); // could be '0.0.0' as well
expect(parser).toBeInstanceOf(ParserV3); // Using Parser v3 (latest atm) by default
});

it('Creates a Parser with options compatible with old Parser API (AKA v0)', async function() {
const options: Options = { parserOptions: { schemaParsers: [fakeSchemaParser]} };
const parser = NewParser('0.0.0', options); // could be empty string as well
const parser = NewParser(0, options); // could be empty string as well
expect(parser).toBeInstanceOf(ParserV3);
expect(parser.parserRegistry.get('fake-format')).not.toBeUndefined();
});

it('Creates a Parser with options including known Schema Parsers and do not overwrite those with Parser-API v2', async function() {
const knownSchemaParser = AvroSchemaParser();
const options: Options = { parserOptions: { schemaParsers: [knownSchemaParser]}, includeSchemaParsers: true };
const parser = NewParser('0.0.0', options);
const parser = NewParser(0, options);

expect(parser).toBeInstanceOf(ParserV3);
expect(parser.parserRegistry.get(knownSchemaParser.getMimeTypes()[0])).toStrictEqual(knownSchemaParser);
Expand Down

0 comments on commit ffae2b4

Please sign in to comment.