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: enable Typescript Input #594

Merged
merged 22 commits into from
Mar 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
847fc2f
feat: add starter files for TypescriptInputProcessor
ron-debajyoti Jan 21, 2022
7f5cd80
chore: add improvements to code
ron-debajyoti Jan 29, 2022
aefb876
refactor: add additional checks and tests for TypeScriptInputProcessor
ron-debajyoti Jan 31, 2022
efde5a2
refactor: changed the code to the simplest case
ron-debajyoti Feb 1, 2022
fb2d886
fix: remove codesmell errors
ron-debajyoti Feb 1, 2022
0eb535b
fix: improper test checks in TypeScriptInputProcessor.spec
ron-debajyoti Feb 3, 2022
9d8821e
fix: add better schema generation without bugs
ron-debajyoti Feb 9, 2022
1689385
fix: sonarcloud errors
ron-debajyoti Feb 9, 2022
570aa0e
fix: sonarcloud codesmell
ron-debajyoti Feb 9, 2022
bfe750c
chore: improved class TypeScriptInputProcessor with options support
ron-debajyoti Feb 15, 2022
cfe066d
feat: add test for checking TypeScriptInputProcessor process options
ron-debajyoti Feb 16, 2022
a191ec3
feat: add example for generating Java model from typescript input
ron-debajyoti Feb 16, 2022
ff4007d
docs: add updated docs for including TypeScript input
ron-debajyoti Feb 16, 2022
78385ab
fix: fix codesmell errors
ron-debajyoti Feb 16, 2022
da4667e
fix: fix naming error in examples
ron-debajyoti Feb 16, 2022
2e4e925
docs: update docs description
ron-debajyoti Feb 16, 2022
e8b47e9
Merge branch 'master' of https://github.com/asyncapi/modelina into ts…
ron-debajyoti Feb 17, 2022
e09b0da
fix: split java-from-typescript examples into two
ron-debajyoti Feb 18, 2022
4391f58
fix: update examples to make testing compatible across OS
ron-debajyoti Feb 21, 2022
54196f1
Merge branch 'master' of https://github.com/asyncapi/modelina into ts…
ron-debajyoti Mar 2, 2022
1895b77
Merge branch 'master' into ts-input-support
jonaslagoni Mar 2, 2022
4edf742
Merge branch 'master' into ts-input-support
jonaslagoni Mar 3, 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
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,17 @@
<em>Modelina is the official AsyncAPI SDK to generate data models (i.e. <a href="#outputs">Java/TypeScript classes, Go Structs, etc</a>) from <a href="#inputs">AsyncAPI documents, among other supported inputs</a>.</em>
</p>


[![blackbox pipeline status](https://img.shields.io/github/workflow/status/asyncapi/modelina/Blackbox%20testing%20(Stay%20Awhile%20and%20Listen)?label=blackbox%20testing)](https://github.com/asyncapi/modelina/actions/workflows/blackbox-testing.yml?query=branch%3Amaster++)
[![blackbox pipeline status](<https://img.shields.io/github/workflow/status/asyncapi/modelina/Blackbox%20testing%20(Stay%20Awhile%20and%20Listen)?label=blackbox%20testing>)](https://github.com/asyncapi/modelina/actions/workflows/blackbox-testing.yml?query=branch%3Amaster++)
[![Coverage Status](https://coveralls.io/repos/github/asyncapi/modelina/badge.svg?branch=master)](https://coveralls.io/github/asyncapi/modelina?branch=master)
[![Project Status: WIP – Initial development is in progress, but there has not yet been a stable, usable release suitable for the public.](https://www.repostatus.org/badges/latest/wip.svg)](https://www.repostatus.org/#wip)
![Maintenance score](https://img.shields.io/npms-io/maintenance-score/@asyncapi/modelina)
[![Npm latest version](https://img.shields.io/npm/v/@asyncapi/modelina)](https://www.npmjs.com/package/@asyncapi/modelina)
[![License](https://img.shields.io/github/license/asyncapi/modelina)](https://github.com/asyncapi/modelina/blob/master/LICENSE)
[![License](https://img.shields.io/github/license/asyncapi/modelina)](https://github.com/asyncapi/modelina/blob/master/LICENSE)
[![last commit](https://img.shields.io/github/last-commit/asyncapi/modelina)](https://github.com/asyncapi/modelina/commits/master)
[![Discussions](https://img.shields.io/github/discussions/asyncapi/modelina)](https://github.com/asyncapi/modelina/discussions)
[![Playground](https://img.shields.io/website?label=playground&url=https%3A%2F%2Fwww.asyncapi.com%2Ftools%2Fmodelina)](https://www.asyncapi.com/tools/modelina) <!-- ALL-CONTRIBUTORS-BADGE:START - Do not remove or modify this section -->
[![All Contributors](https://img.shields.io/badge/all_contributors-22-orange.svg?style=flat-square)](#contributors-)

<!-- ALL-CONTRIBUTORS-BADGE:END -->

---
Expand Down Expand Up @@ -52,6 +52,7 @@ This package is still under development and has not reached version 1.0.0 yet. T
Feel free to submit an issue if you require this project in other use-cases.

## Installation

Run this command to install Modelina in your project:

```bash
Expand All @@ -61,11 +62,13 @@ npm install @asyncapi/modelina
Once you've successfully installed Modelina in your project, it's time to select your generator. Check out the [examples](#examples) for the specific code.

## Features

The following table provides a short summary of available features for supported output languages.

To see the complete feature list for each language, please click the individual links for each language.

<a id="inputs"></a>

<!-- prettier-ignore-start -->
<!-- markdownlint-disable -->
<table>
Expand All @@ -85,6 +88,10 @@ To see the complete feature list for each language, please click the individual
<td>OpenAPI</td>
<td>We support the following OpenAPI versions: <em><a href="./docs/usage.md#generate-models-from-swagger-20-documents">Swagger 2.0</a> and <a href="./docs/usage.md#generate-models-from-openapi-documents">OpenAPI 3.0</a></em>, which generates models for all the defined request and response payloads.</td>
</tr>
<tr>
<td><a href="./docs/usage.md#generate-model-from-typescript-type-files">TypeScript file</a></td>
<td>We currently support TypeScript type file as input for model generation</td>
</tr>
</table>

<a id="outputs"></a>
Expand Down
8 changes: 8 additions & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ The OpenAPI input processor expects that the property `openapi` is defined in or

The response and request payloads, since it is a JSON Schema variant, is [interpreted as a such](./interpretation_of_JSON_Schema.md).

## Generate models from TypeScript type files

Currently, we support generating models from a TypeScript type file.

- [Generate Java model from a TypeScript file](../examples/java-from-typescript-type/)
ron-debajyoti marked this conversation as resolved.
Show resolved Hide resolved

The TypeScript input processor expects that the typescript file and base directory where it's present, is passed as input, in order to process the types accurately.

## Generate Go models

Go is one of the many output languages we support. Check out this [basic example for a live demonstration](../examples/generate-go-models) and the following [Go documentation for more advanced use-cases](./languages/Go.md).
Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,6 @@ This directory contains a series of self-contained examples that you can use as
- [generate-csharp-models](./generate-csharp-models) - A basic example to generate C# data models
- [java-change-collection-type](./java-change-collection-type) - An example to render collections as List in Java.
- [java-generate-hashcode](./java-generate-hashcode) - A basic example that shows how to generate models that overwrite the `hashCode` method
- [java-from-typescript-type](./java-from-typescript-type/) - A basic example that shows how to generate a Java model from a TypeScript type input file.
- [java-generate-marshalling](./java-generate-marshalling) - A basic example of how to use the un/marshalling functionality of the java class.
- [java-from-typescript-type-with-options](./java-from-typescript-type-with-options/) - A basic example that shows how to generate a Java model from a TypeScript type input file along with user provided options.
17 changes: 17 additions & 0 deletions examples/java-from-typescript-type-with-options/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# TODO: Generate Java model from a TypeScript Types file using processor options

This is a basic example of generating Java models using a Typescript type file as input and processor options. This can be extended to generating Go, C# and other supported language 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,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to generate Java models from a TypeScript type file
along with specific options and should log expected output to console 1`] = `
Array [
"public class OuterData {
private Object inner;
private String name;
private Map<String, Object> additionalProperties;

public Object getInner() { return this.inner; }
public void setInner(Object inner) { this.inner = inner; }

public String getName() { return this.name; }
public void setName(String name) { this.name = name; }

public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
]
`;
16 changes: 16 additions & 0 deletions examples/java-from-typescript-type-with-options/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; });
import {generate} from './index';

describe(`Should be able to generate Java models from a TypeScript type file
along with specific options`, () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
//GenerateWithOptions is called 4x, so even though we expect 1 model, we double it
expect(spy.mock.calls.length).toEqual(4);
expect(spy.mock.calls[1]).toMatchSnapshot();
});
});

31 changes: 31 additions & 0 deletions examples/java-from-typescript-type-with-options/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { JavaGenerator } from '../../src';
import * as path from 'path';
import * as fs from 'fs';

const generator = new JavaGenerator(
{
processorOptions: {
typescript: {
compilerOptions: {
strictNullChecks: true
}
}
}
}
);

const file = path.resolve(__dirname, './typescriptFile.ts');
const fileContents = fs.readFileSync(path.resolve(__dirname, './typescriptFile.ts'),'utf-8');

export async function generate() : Promise<void> {
const models = await generator.generate({
fileContents,
baseFile: file
});

for (const model of models) {
console.log(model.result);
}
}

generate();

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

10 changes: 10 additions & 0 deletions examples/java-from-typescript-type-with-options/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "java-from-typescript-type-with-options" },
"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"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface InnerData {
age: number;
name: string;
free: boolean;
}

export interface OuterData {
inner: InnerData;
name: string;
}
17 changes: 17 additions & 0 deletions examples/java-from-typescript-type/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generate Java from a TypeScript Types file

This is a basic example of generating Java models using a Typescript type file as input. This can be extended to generating Go, C# and other supported language 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,24 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to generate Java model from a TypeScript type file and should log expected output to console 1`] = `
Array [
"public class InnerData {
private Double age;
private String name;
private Boolean free;
private Map<String, Object> additionalProperties;

public Double getAge() { return this.age; }
public void setAge(Double age) { this.age = age; }

public String getName() { return this.name; }
public void setName(String name) { this.name = name; }

public Boolean getFree() { return this.free; }
public void setFree(Boolean free) { this.free = free; }

public Map<String, Object> getAdditionalProperties() { return this.additionalProperties; }
public void setAdditionalProperties(Map<String, Object> additionalProperties) { this.additionalProperties = additionalProperties; }
}",
]
`;
14 changes: 14 additions & 0 deletions examples/java-from-typescript-type/index.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const spy = jest.spyOn(global.console, 'log').mockImplementation(() => { return; });
import {generate} from './index';

describe('Should be able to generate Java model from a TypeScript type file', () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
//Generate is called 4x, so even though we expect 1 model, we double it
expect(spy.mock.calls.length).toEqual(4);
expect(spy.mock.calls[1]).toMatchSnapshot();
});
});
21 changes: 21 additions & 0 deletions examples/java-from-typescript-type/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { JavaGenerator } from '../../src';
import * as path from 'path';
import * as fs from 'fs';

const generator = new JavaGenerator();

const file = path.resolve(__dirname, './typescriptFile.ts');
const fileContents = fs.readFileSync(path.resolve(__dirname, './typescriptFile.ts'),'utf-8');

export async function generate() : Promise<void> {
const models = await generator.generate({
fileContents,
baseFile: file
});

for (const model of models) {
console.log(model.result);
}
}

generate();
10 changes: 10 additions & 0 deletions examples/java-from-typescript-type/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/java-from-typescript-type/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "java-from-typescript-type" },
"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"
}
}
9 changes: 9 additions & 0 deletions examples/java-from-typescript-type/typescriptFile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export type Shape = {
size: number;
}

export interface InnerData {
age: number;
name: string;
free: boolean;
}
Loading