Skip to content

Commit

Permalink
feat: enable AsyncAPI v3 (#1600)
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni authored Nov 14, 2023
1 parent 52fd126 commit 125278e
Show file tree
Hide file tree
Showing 7 changed files with 513 additions and 47 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ module.exports = {
modulePathIgnorePatterns: [
'<rootDir>/examples/TEMPLATE',
'<rootDir>/test/generators/template',
'<rootDir>/test/processors/TemplateInputProcessor.spec.ts'
'<rootDir>/test/processors/TemplateInputProcessor.spec.ts',
'modelina-website'
],
watchPathIgnorePatterns: ['<rootDir>/node_modules']
};
213 changes: 208 additions & 5 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 @@ -38,7 +38,7 @@
"@apidevtools/json-schema-ref-parser": "^9.0.9",
"@apidevtools/swagger-parser": "^10.0.3",
"@smoya/multi-parser": "^4.0.0",
"@asyncapi/parser": "^2.1.0",
"@asyncapi/parser": "3.0.0-next-major-spec.8",
"@swc/core": "^1.3.5",
"@swc/jest": "^0.2.23",
"@types/node": "^20.3.3",
Expand Down
36 changes: 8 additions & 28 deletions src/processors/AsyncAPIInputProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {

import { AbstractInputProcessor } from './AbstractInputProcessor';
import { JsonSchemaInputProcessor } from './JsonSchemaInputProcessor';
import { InputMetaModel, ProcessorOptions, UnionModel } from '../models';
import { InputMetaModel, ProcessorOptions } from '../models';
import { Logger } from '../utils';
import { AsyncapiV2Schema } from '../models/AsyncapiV2Schema';
import { convertToMetaModel } from '../helpers';
Expand All @@ -29,7 +29,9 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
'2.3.0',
'2.4.0',
'2.5.0',
'2.6.0'
'2.6.0',
'2.6.0',
'3.0.0'
];

/**
Expand Down Expand Up @@ -58,7 +60,7 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
doc = createAsyncAPIDocument(detailed);
} else {
const parserOptions = options?.asyncapi || {};
const parser = NewParser(1, {
const parser = NewParser(2, {
parserOptions,
includeSchemaParsers: true
});
Expand All @@ -67,7 +69,7 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
parserOptions
);
if (document) {
doc = document as AsyncAPIDocumentInterface;
doc = document as unknown as AsyncAPIDocumentInterface;
} else {
const err = new Error(
'Input is not an correct AsyncAPI document so it cannot be processed.'
Expand All @@ -83,21 +85,6 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
inputModel.originalInput = doc;

const addToInputModel = (payload: AsyncAPISchemaInterface) => {
const id = payload.title() || payload.id();

for (const model of Object.values(inputModel.models)) {
if (model instanceof UnionModel) {
for (const union of model.union) {
if (union.name === id) {
Logger.warn(
`Model ${id} has already been added to the input model`
);
return;
}
}
}
}

const schema = AsyncAPIInputProcessor.convertToInternalSchema(payload);
const newCommonModel =
JsonSchemaInputProcessor.convertSchemaToCommonModel(schema, options);
Expand Down Expand Up @@ -158,13 +145,6 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
}
}
}

for (const message of doc.messages()) {
const payload = message.payload();
if (payload) {
addToInputModel(payload);
}
}
} else {
for (const message of doc.allMessages()) {
const payload = message.payload();
Expand Down Expand Up @@ -337,7 +317,7 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
alreadyIteratedSchemas
);
} else {
dependencies[String(dependencyName)] = dependency as string[];
dependencies[String(dependencyName)] = dependency;
}
}
convertedSchema.dependencies = dependencies;
Expand Down Expand Up @@ -404,7 +384,7 @@ export class AsyncAPIInputProcessor extends AbstractInputProcessor {
if (AsyncAPIInputProcessor.isFromParser(input)) {
return input.version();
}
return input && input.asyncapi;
return input?.asyncapi;
}

/**
Expand Down
Loading

0 comments on commit 125278e

Please sign in to comment.