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

refactor: introduce new core models #655

Merged
merged 83 commits into from
Mar 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
83 commits
Select commit Hold shift + click to select a range
e7fdf46
docs: add quadrrem as a contributor for code, test
allcontributors[bot] Dec 15, 2021
593a6e8
ci: update global workflows
asyncapi-bot Dec 16, 2021
cf8c922
docs: generate java data models
mahakporwal02 Jan 3, 2022
d659d80
fix: model dependencies are not accurately resolved
jonaslagoni Jan 3, 2022
7b6010e
fix: wrong property name in JavaCommonPresetOptions interface (#544)
kamko Jan 3, 2022
3d1208a
chore(release): v0.39.10
asyncapi-bot Jan 3, 2022
b4770ea
docs: add mahakporwal02 as a contributor for example, test, doc (#549)
allcontributors[bot] Jan 3, 2022
b85e11e
docs: add kamko as a contributor for test, bug, code (#552)
allcontributors[bot] Jan 3, 2022
2b48a05
docs: add OpenAPI input as example
jonaslagoni Jan 3, 2022
f067637
fix: correctly store already converted schemas while parsing AsyncApi…
kamko Jan 4, 2022
65efaea
chore(release): v0.39.11
asyncapi-bot Jan 4, 2022
99b5b44
feat: complete model rendering and file output for JavaScript generator
jonaslagoni Jan 4, 2022
4cf252c
chore(release): v0.40.0 (#556)
asyncapi-bot Jan 4, 2022
17f8f7a
fix: removing TypeScript exported base model
jonaslagoni Jan 4, 2022
120181d
chore(release): v0.40.1
asyncapi-bot Jan 4, 2022
ef4c0b7
fix: special cases in enum values for C# generator
ron-debajyoti Jan 4, 2022
8d9b7ff
docs: add ron-debajyoti as a contributor for code, test
allcontributors[bot] Jan 4, 2022
a897e07
chore(release): v0.40.2
asyncapi-bot Jan 5, 2022
11cc2c1
feat: add file output for C# generator (#537)
jonaslagoni Jan 5, 2022
d4ed0e3
chore(release): v0.41.0
asyncapi-bot Jan 5, 2022
5cbe362
feat: add file output for TypeScript generator (#536)
jonaslagoni Jan 7, 2022
623dcb2
chore(release): v0.42.0
asyncapi-bot Jan 7, 2022
7b5966e
ci: update global workflows
asyncapi-bot Jan 10, 2022
49a534c
docs: generate go data model (#568)
mahakporwal02 Jan 10, 2022
dc68646
chore: fixing sonarcloud duplication problems (#570)
jonaslagoni Jan 10, 2022
cb81099
fix: model dependencies not needed for C# generator
jonaslagoni Jan 10, 2022
b203752
chore(release): v0.42.1 (#571)
asyncapi-bot Jan 10, 2022
d84b8b5
fix: non-required properties are required by constructor (#553)
mahakporwal02 Jan 10, 2022
e1fb4d7
chore(release): v0.42.2 (#572)
asyncapi-bot Jan 10, 2022
07bcb3f
docs: example on how to include a custom function in the data model
mahakporwal02 Jan 11, 2022
dd3163c
docs: add mahakporwal02 as a contributor for code
allcontributors[bot] Jan 11, 2022
cafc1ff
feat: complete model rendering and file output for Go generator (#540)
jonaslagoni Jan 11, 2022
b06c242
docs: example of csharp example serializer
ron-debajyoti Jan 11, 2022
acaea00
chore(release): v0.43.0 (#574)
asyncapi-bot Jan 11, 2022
1dc565e
docs: add ron-debajyoti as a contributor for doc, example (#576)
allcontributors[bot] Jan 11, 2022
e22c136
docs: generate C# models (#578)
ron-debajyoti Jan 11, 2022
928daa0
docs: generate Java data models that overwrite equal method (#577)
mahakporwal02 Jan 11, 2022
d9266db
docs: generate Java data models that overwrite hashCode method (#580)
mahakporwal02 Jan 12, 2022
17273c1
docs: model to change collection types for Java models (#579)
ron-debajyoti Jan 13, 2022
2a058d6
ci: update global workflows
asyncapi-bot Jan 13, 2022
51ae271
ci: update global workflows (#584)
asyncapi-bot Jan 14, 2022
ef07448
ci: update global workflows (#585)
asyncapi-bot Jan 14, 2022
ffc0cd8
docs: add missing documentation for indentation example (#541)
jonaslagoni Jan 14, 2022
1c684c5
chore: added nvmrc (#582)
ritik307 Jan 14, 2022
18564f8
docs: add ritik307 as a contributor for doc (#588)
allcontributors[bot] Jan 14, 2022
8ff4bb9
docs: linked to advance language usage (#589)
mahakporwal02 Jan 15, 2022
5622923
feat: assign optional arguments in the constructor for JS generator (…
mahakporwal02 Jan 15, 2022
5dc200e
chore(release): v0.44.0 (#590)
asyncapi-bot Jan 17, 2022
88cf92d
ci: fix coverage problems with checkout (#592)
jonaslagoni Jan 17, 2022
57948b5
ci: update global workflows (#593)
asyncapi-bot Jan 20, 2022
b2683cd
feat: un/marshal helper function for JS (#591)
Samridhi-98 Jan 26, 2022
9341e33
chore(release): v0.45.0 (#600)
asyncapi-bot Jan 27, 2022
017d07b
chore(deps): bump node-fetch from 2.6.5 to 2.6.7 (#602)
dependabot[bot] Jan 27, 2022
810d607
ci: update global workflows (#603)
asyncapi-bot Jan 27, 2022
079db79
docs: added jackson annotation example (#601)
Samridhi-98 Jan 31, 2022
bc2f3ef
feat: support AsyncAPI v2.3.0 (#563)
jonaslagoni Jan 31, 2022
71ac65a
chore(release): v0.46.0 (#606)
asyncapi-bot Jan 31, 2022
51062c1
fix: missing namespace for Type in C# JsonSerializerPreset (#597)
jonaslagoni Feb 2, 2022
7b2a33f
chore(release): v0.46.1 (#611)
asyncapi-bot Feb 2, 2022
8818321
ci: update global workflows (#617)
asyncapi-bot Feb 8, 2022
388495e
feat: exclude underscore from TS key enum rendering (#613)
Ishan-Saini Feb 8, 2022
b819c6f
chore(release): v0.47.0 (#618)
asyncapi-bot Feb 8, 2022
e3aeeb1
ci: update global workflows (#620)
asyncapi-bot Feb 8, 2022
b82a78b
docs: add Ishan-Saini as a contributor for code, test (#619)
allcontributors[bot] Feb 9, 2022
8a79fc0
ci: update global workflows (#631)
asyncapi-bot Feb 11, 2022
e943a71
docs: added dependency info for C# serializer preset (#630)
Samridhi-98 Feb 12, 2022
b3c10bf
ci: only run workflows when necessary (#624)
jonaslagoni Feb 13, 2022
afe46b1
feat: add un/marshal JSON preset for Java (#596)
ritik307 Feb 13, 2022
9f596f8
docs: add ritik307 as a contributor for code, test, example (#632)
allcontributors[bot] Feb 13, 2022
594e7f1
chore(release): v0.48.0 (#633)
asyncapi-bot Feb 13, 2022
5567586
docs: add javascript marshaling example (#635)
Samridhi-98 Feb 14, 2022
c9980fc
feat: add generate example preset for JS (#629)
Samridhi-98 Feb 15, 2022
c773f4c
feat: new `accessor` preset hook for C# class renderer (#626)
jonaslagoni Feb 16, 2022
0ff2e89
chore(release): v0.49.0 (#650)
asyncapi-bot Feb 16, 2022
9e4add4
docs: add Samridhi-98 as a contributor for code, test, doc, example (…
allcontributors[bot] Feb 16, 2022
10aca3b
docs: add java un/marshalling reference to docs (#645)
ritik307 Feb 16, 2022
b0a41ca
Added models
jonaslagoni Feb 17, 2022
e4abbe6
Merge branch 'next' into feature/introduce_models
jonaslagoni Feb 28, 2022
ca0a1b2
fixed lint errors
jonaslagoni Mar 2, 2022
6bc51a9
Switched to inline modifiers for constructor
jonaslagoni Mar 4, 2022
842aad3
Changed naming of models
jonaslagoni Mar 4, 2022
2c2e3ca
Added export of models
jonaslagoni Mar 4, 2022
aeec94e
Removed unnecessary redefining of properties
jonaslagoni Mar 8, 2022
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
Prev Previous commit
Next Next commit
feat: add file output for TypeScript generator (#536)
  • Loading branch information
jonaslagoni authored Jan 7, 2022
commit 5cbe3622d9f9081370a6139df0f7d27e7d9ed562
1 change: 1 addition & 0 deletions docs/advanced.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The file generators all follow the same pattern regardless of output language, w

Supported by:
- Java
- TypeScript
- C#
- JavaScript

Expand Down
9 changes: 9 additions & 0 deletions docs/languages/TypeScript.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ There are special use-cases that each language supports; this document pertains
- [Generate union types instead of enums](#generate-union-types-instead-of-enums)
- [Generate un/marshal functions for classes](#generate-unmarshal-functions-for-classes)
- [Generate example data function](#generate-example-data-function)
- [Rendering complete models to a specific module system](#rendering-complete-models-to-a-specific-module-system)

<!-- tocstop -->

Expand Down Expand Up @@ -50,3 +51,11 @@ You might stumble upon a user case (we had one in code generation) where you wan
This can be done by including the preset `TS_COMMON_PRESET` using the option `example`.

Check out this [example out for a live demonstration](../../examples/typescript-generate-example).


## Rendering complete models to a specific module system
In some cases you might need to render the complete models to a specific module system such as ESM and CJS.

Check out this [example for a live demonstration how to generate the complete TypeScript models to use ESM module system](../../examples/typescript-use-esm).

Check out this [example for a live demonstration how to generate the complete TypeScript models to use CJS module system](../../examples/typescript-use-cjs).
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ This directory contains a series of self-contained examples that you can use as
- [typescript-enum-type](./typescript-enum-type) - A basic example of how to use Modelina can output different types of enums in TypeScript.
- [typescript-generate-marshalling](./typescript-generate-marshalling) - A basic example of how to use the un/marshalling functionality of the typescript class.
- [typescript-generate-example](./typescript-generate-example) - A basic example of how to use Modelina and output a TypeScript class with an example function.
- [typescript-use-esm](./typescript-use-esm) - A basic example that generate the models to use ESM module system.
- [typescript-use-cjs](./typescript-use-cjs) - A basic example that generate the models to use CJS module system.
- [indentation-type-and-size](./indentation-type-and-size) - This example shows how to change the indentation type and size of the generated model.
- [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.
Expand Down
12 changes: 8 additions & 4 deletions examples/javascript-use-esm/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exports[`Should be able to render models to ESM module system and should log exp
Array [
"

export default class Person {
class Person {
email;

constructor(input) {
Expand All @@ -13,15 +13,17 @@ export default class Person {

get email() { return this.email; }
set email(email) { this.email = email; }
}",
}
export default Person;
",
]
`;

exports[`Should be able to render models to ESM module system and should log expected output to console 2`] = `
Array [
"import Person from './Person';

export default class Root {
class Root {
person;

constructor(input) {
Expand All @@ -30,6 +32,8 @@ export default class Root {

get person() { return this.person; }
set person(person) { this.person = person; }
}",
}
export default Root;
",
]
`;
2 changes: 1 addition & 1 deletion examples/javascript-use-esm/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"config" : { "example_name" : "javascript-module-system" },
"config" : { "example_name" : "javascript-use-esm" },
"scripts": {
"install": "cd ../.. && npm i",
"start": "../../node_modules/.bin/ts-node --cwd ../../ ./examples/$npm_package_config_example_name/index.ts",
Expand Down
17 changes: 17 additions & 0 deletions examples/typescript-use-cjs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# TypeScript models use CJS module system

This example shows how you can generate the models to use CJS module system.

## 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
```
41 changes: 41 additions & 0 deletions examples/typescript-use-cjs/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to render models to ESM module system and should log expected output to console 1`] = `
Array [
"

class Person {
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; }
}
module.exports = Person;",
]
`;

exports[`Should be able to render models to ESM module system and should log expected output to console 2`] = `
Array [
"const Person = require('./Person');

class Root {
private _person?: Person;

constructor(input: {
person?: Person,
}) {
this._person = input.person;
}

get person(): Person | undefined { return this._person; }
set person(person: Person | undefined) { this._person = person; }
}
module.exports = Root;",
]
`;
15 changes: 15 additions & 0 deletions examples/typescript-use-cjs/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 models to ESM module system', () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
//Generate is called 2x, so even though we expect 2 models, we double it
expect(spy.mock.calls.length).toEqual(4);
expect(spy.mock.calls[1]).toMatchSnapshot();
expect(spy.mock.calls[2]).toMatchSnapshot();
});
});
33 changes: 33 additions & 0 deletions examples/typescript-use-cjs/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { TypeScriptGenerator } from '../../src';

const generator = new TypeScriptGenerator();
const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
properties: {
person: {
type: 'object',
additionalProperties: false,
properties: {
email: {
type: 'string',
format: 'email'
}
}
}
}
};

export async function generate() : Promise<void> {
const models = await generator.generateCompleteModels(
jsonSchemaDraft7,
{
moduleSystem: 'CJS'
}
);
for (const model of models) {
console.log(model.result);
}
}
generate();
10 changes: 10 additions & 0 deletions examples/typescript-use-cjs/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/typescript-use-cjs/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "typescript-use-cjs" },
"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"
}
}
17 changes: 17 additions & 0 deletions examples/typescript-use-esm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# TypeScript models use ESM module system

This example shows how you can generate the models to use ESM module system.

## 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
```
43 changes: 43 additions & 0 deletions examples/typescript-use-esm/__snapshots__/index.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to render models to ESM module system and should log expected output to console 1`] = `
Array [
"

class Person {
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; }
}
export default Person;
",
]
`;

exports[`Should be able to render models to ESM module system and should log expected output to console 2`] = `
Array [
"import Person from './Person';

class Root {
private _person?: Person;

constructor(input: {
person?: Person,
}) {
this._person = input.person;
}

get person(): Person | undefined { return this._person; }
set person(person: Person | undefined) { this._person = person; }
}
export default Root;
",
]
`;
15 changes: 15 additions & 0 deletions examples/typescript-use-esm/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 models to ESM module system', () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
//Generate is called 2x, so even though we expect 2 models, we double it
expect(spy.mock.calls.length).toEqual(4);
expect(spy.mock.calls[1]).toMatchSnapshot();
expect(spy.mock.calls[2]).toMatchSnapshot();
});
});
33 changes: 33 additions & 0 deletions examples/typescript-use-esm/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { TypeScriptGenerator } from '../../src';

const generator = new TypeScriptGenerator();
const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
properties: {
person: {
type: 'object',
additionalProperties: false,
properties: {
email: {
type: 'string',
format: 'email'
}
}
}
}
};

export async function generate() : Promise<void> {
const models = await generator.generateCompleteModels(
jsonSchemaDraft7,
{
moduleSystem: 'ESM'
}
);
for (const model of models) {
console.log(model.result);
}
}
generate();
10 changes: 10 additions & 0 deletions examples/typescript-use-esm/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/typescript-use-esm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "typescript-use-esm" },
"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"
}
}
3 changes: 2 additions & 1 deletion src/generators/javascript/JavaScriptFileGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export class JavaScriptFileGenerator extends JavaScriptGenerator implements Abst
*/
public async generateToFiles(input: Record<string, unknown> | CommonInputModel, outputDirectory: string, options?: JavaScriptRenderCompleteModelOptions): Promise<OutputModel[]> {
let generatedModels = await this.generateCompleteModels(input, options || {});
generatedModels = generatedModels.filter((outputModel) => { return outputModel.modelName !== undefined; });
//Filter anything out that have not been successfully generated
generatedModels = generatedModels.filter((outputModel) => { return outputModel.modelName !== ''; });
for (const outputModel of generatedModels) {
const filePath = path.resolve(outputDirectory, `${outputModel.modelName}.js`);
await FileHelpers.writerToFileSystem(outputModel.result, filePath);
Expand Down
17 changes: 14 additions & 3 deletions src/generators/javascript/JavaScriptGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,25 @@ export class JavaScriptGenerator extends AbstractGenerator<JavaScriptOptions, Ja
// eslint-disable-next-line @typescript-eslint/no-unused-vars
async renderCompleteModel(model: CommonModel, inputModel: CommonInputModel, options: JavaScriptRenderCompleteModelOptions): Promise<RenderOutput> {
const outputModel = await this.render(model, inputModel);
const modelDependencies = model.getNearestDependencies().map((dependencyModelName) => {
const formattedDependencyModelName = this.options.namingConvention?.type ? this.options.namingConvention.type(dependencyModelName, { inputModel, model: inputModel.models[String(dependencyModelName)] }) : dependencyModelName;
let modelDependencies = model.getNearestDependencies();
//Ensure model dependencies have their rendered name
modelDependencies = modelDependencies.map((dependencyModelName) => {
return this.options.namingConvention?.type ? this.options.namingConvention.type(dependencyModelName, { inputModel, model: inputModel.models[String(dependencyModelName)] }) : dependencyModelName;
});
//Filter out any dependencies that is recursive to it'self
modelDependencies = modelDependencies.filter((dependencyModelName) => {
return dependencyModelName !== outputModel.renderedName;
});
//Create the correct dependency imports
modelDependencies = modelDependencies.map((formattedDependencyModelName) => {
if (options.moduleSystem === 'CJS') {
return `const ${formattedDependencyModelName} = require('./${formattedDependencyModelName}');`;
}
return `import ${formattedDependencyModelName} from './${formattedDependencyModelName}';`;
});
let modelCode = `export default ${outputModel.result}`;
let modelCode = `${outputModel.result}
export default ${outputModel.renderedName};
`;
if (options.moduleSystem === 'CJS') {
modelCode = `${outputModel.result}
module.exports = ${outputModel.renderedName};`;
Expand Down
Loading