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

fix: improve integration with old AsyncAPI parser #1050

Merged
merged 11 commits into from
Dec 19, 2022
Merged
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
1 change: 1 addition & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ For JSON Schema it follows the [JSON Schema input processing](#generate-models-f
There are two ways to generate models for an AsyncAPI document.

- [Generate from a parsed AsyncAPI 2.x document](../examples/asyncapi-from-parser)
- [Generate from a parsed AsyncAPI 2.x document, from the old v1 parser](../examples/asyncapi-from-v1-parser)
- [Generate from an AsyncAPI 2.x JS object](../examples/asyncapi-from-object)

The library expects the `asyncapi` property for the document to be set in order to understand the input format.
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ We love contributions and new examples that does not already exist, you can foll
These examples show a specific input and how they can be used:
- [asyncapi-from-object](./asyncapi-from-object) - A basic example where an AsyncAPI JS object is used to generate models.
- [asyncapi-from-parser](./asyncapi-from-parser) - A basic example where an AsyncAPI JS object from the [parser-js](https://github.com/asyncapi/parser-js) is used to generate models.
- [asyncapi-from-v1-parser](./asyncapi-from-v1-parser) - A basic example where an AsyncAPI JS object from the old v1 [parser-js](https://github.com/asyncapi/parser-js) is used to generate models.
- [json-schema-draft7-from-object](./json-schema-draft7-from-object) - A basic example where a JSON Schema draft 7 JS object is used to generate models.
- [json-schema-draft6-from-object](./json-schema-draft6-from-object) - A basic example where a JSON Schema draft 6 JS object is used to generate models.
- [json-schema-draft4-from-object](./json-schema-draft4-from-object) - A basic example where a JSON Schema draft 4 JS object is used to generate models.
Expand Down
2 changes: 1 addition & 1 deletion examples/asyncapi-from-parser/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const AsyncAPIDocument = {
export async function generate() : Promise<void> {
const parser = new Parser();
const { document } = await parser.parse(JSON.stringify(AsyncAPIDocument));
const models = await generator.generate(document as any);
const models = await generator.generate(document);
for (const model of models) {
console.log(model.result);
}
Expand Down
17 changes: 17 additions & 0 deletions examples/asyncapi-from-v1-parser/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# AsyncAPI from parser

A basic example of how to use Modelina with an AsyncAPI instance from the AsyncAPI parser.

## How to run this example

Run this example using:

```sh
npm i && npm run start
```

If you are on Windows, use the `start:windows` script instead:

```sh
npm i && npm run start:windows
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to process already parsed AsyncAPI document from old parser and should log expected output to console 1`] = `
Array [
"class AnonymousSchema_1 {
private _email?: string;

constructor(input: {
email?: string,
}) {
this._email = input.email;
}

get email(): string | undefined { return this._email; }
set email(email: string | undefined) { this._email = email; }
}",
]
`;
13 changes: 13 additions & 0 deletions examples/asyncapi-from-v1-parser/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; });
import {generate} from './index';

describe('Should be able to process already parsed AsyncAPI document from old parser', () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
expect(spy.mock.calls.length).toEqual(1);
expect(spy.mock.calls[0]).toMatchSnapshot();
});
});
42 changes: 42 additions & 0 deletions examples/asyncapi-from-v1-parser/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// eslint-disable-next-line @typescript-eslint/no-var-requires
const parser = require('@asyncapi/parserV1');
import { TypeScriptGenerator } from '../../src';

const generator = new TypeScriptGenerator();
const AsyncAPIDocument = {
asyncapi: '2.2.0',
info: {
title: 'example',
version: '0.1.0'
},
channels: {
'/test': {
subscribe: {
message: {
payload: {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
properties: {
email: {
type: 'string',
format: 'email'
}
}
}
}
}
}
}
};

export async function generate() : Promise<void> {
const parsedDoc = await parser.parse(JSON.stringify(AsyncAPIDocument));
const models = await generator.generate(parsedDoc);
for (const model of models) {
console.log(model.result);
}
}
if (require.main === module) {
generate();
}
10 changes: 10 additions & 0 deletions examples/asyncapi-from-v1-parser/package-lock.json

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

10 changes: 10 additions & 0 deletions examples/asyncapi-from-v1-parser/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "asyncapi-from-v1-parser" },
"scripts": {
"install": "cd ../.. && npm i",
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
"start:windows": "..\\..\\node_modules\\.bin\\ts-node --cwd ..\\..\\ .\\examples\\%npm_package_config_example_name%\\index.ts",
"test": "../../node_modules/.bin/jest --config=../../jest.config.js ./examples/$npm_package_config_example_name/index.spec.ts",
"test:windows": "..\\..\\node_modules\\.bin\\jest --config=..\\..\\jest.config.js examples/%npm_package_config_example_name%/index.spec.ts"
}
}
Loading