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

feat: add Avro Schema input processor #1753

Merged
merged 20 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,10 @@ The following table provides a short summary of available features for supported
<td><a href="./docs/usage.md#generate-models-from-asyncapi-documents">AsyncAPI</a></td>
<td>We support the following AsyncAPI versions: <em>2.0.0 -> 2.6.0</em>, which generates models for all the defined message payloads. It supports the following schemaFormats AsyncAPI Schema object, JSON Schema draft 7, <a href="./examples/asyncapi-avro-schema">AVRO 1.9</a>, <a href="./examples/asyncapi-raml-schema">RAML 1.0 data type</a>, and <a href="./examples/asyncapi-openapi-schema">OpenAPI 3.0 Schema</a>.</td>
</tr>
<tr>
<td><a href="./docs/usage.md#generate-models-from-avro-schema-documents">Avro Schema<a></td>
<td>We support the following Avro versions: <a href="./examples/avro-schema-from-object">v1.x</a></td>
</tr>
<tr>
<td><a href="./docs/usage.md#generate-models-from-json-schema-documents">JSON Schema</a></td>
<td>We support the following JSON Schema versions: <em>Draft-4, Draft-6 and Draft-7</em></td>
Expand Down
11 changes: 11 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ For more specific integration options, please check out the [integration documen
- [Generate models from AsyncAPI documents](#generate-models-from-asyncapi-documents)
* [Limitations and Compatibility](#limitations-and-compatibility)
+ [Polymorphism](#polymorphism)
- [Generate models from Avro Schema documents](#generate-models-from-avro-schema-documents)
- [Generate models from JSON Schema documents](#generate-models-from-json-schema-documents)
- [Generate models from Swagger 2.0 documents](#generate-models-from-swagger-20-documents)
* [Limitations and Compatibility](#limitations-and-compatibility-1)
Expand Down Expand Up @@ -107,6 +108,16 @@ There are three ways to generate models for a JSON Schema document.

The library expects the `$schema` property for the document to be set in order to understand the input format. By default, if no other inputs are detected, it defaults to `JSON Schema draft 7`. The process of interpreting a JSON Schema to a model can be read [here](./inputs/JSON_Schema.md).

## Generate models from Avro Schema documents

See the below example to get started with Avro Schema for generating models.

- [Generate from an Avro Schema JS Object](../examples/avro-schema-from-object)

The Avro input processor expects the `name` and `type` property, as per [Avro Schema specs](https://avro.apache.org/docs/1.11.1/specification/#schema-declaration), in the input object in order to proceed successfully.

> Note: Currently, we do not have a support for `map`, `fixed` and `byte` data type. It would be introduced soon.

## Generate models from Swagger 2.0 documents

There are one way to generate models from a Swagger 2.0 document.
Expand Down
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ These examples show a specific input and how they can be used:
- [asyncapi-raml-schema](./asyncapi-raml-schema) - A basic example of how to use Modelina with an AsyncAPI document using RAML 1.0 data types as payload format.
- [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.
- [avro-schema-from-object](./avro-schema-from-object) - A basic example where an Avro Schema JS Object 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
17 changes: 17 additions & 0 deletions examples/avro-schema-from-object/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Avro Schema Input

A basic example of how to use Modelina with an Avro Schema JS object to generate models.

## 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 render model using Avro Schema and should log expected output to console 1`] = `
Array [
"class Person {
private _reservedName: string;

constructor(input: {
reservedName: string,
}) {
this._reservedName = input.reservedName;
}

get reservedName(): string { return this._reservedName; }
set reservedName(reservedName: string) { this._reservedName = reservedName; }
}",
]
`;
15 changes: 15 additions & 0 deletions examples/avro-schema-from-object/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => {
return;
});
import { generate } from './index';

describe('Should be able to render model using Avro Schema', () => {
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();
});
});
27 changes: 27 additions & 0 deletions examples/avro-schema-from-object/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TypeScriptGenerator } from '../../src';

const generator = new TypeScriptGenerator();
const AvroSchemaDoc = {
name: 'Person',
namespace: 'com.company',
type: 'record',
fields: [
{
name: 'name',
type: 'string',
example: 'Donkey',
minLength: 0,
maxLenght: 20
}
]
};

export async function generate(): Promise<void> {
const models = await generator.generate(AvroSchemaDoc);
for (const model of models) {
console.log(model.result);
}
}
if (require.main === module) {
generate();
}
10 changes: 10 additions & 0 deletions examples/avro-schema-from-object/package-lock.json

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

12 changes: 12 additions & 0 deletions examples/avro-schema-from-object/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"config": {
"example_name": "avro-schema-from-object"
},
"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
Loading