Skip to content

Commit

Permalink
feat: add support for OpenAPI version 3.1.0 (#1778)
Browse files Browse the repository at this point in the history
Co-authored-by: Jonas Lagoni <[email protected]>
  • Loading branch information
Athul0491 and jonaslagoni authored Feb 27, 2024
1 parent f7fa01e commit 35f871a
Show file tree
Hide file tree
Showing 14 changed files with 260 additions and 80 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,7 @@ The following table provides a short summary of available features for supported
</tr>
<tr>
<td><a href="./docs/usage.md#generate-models-from-openapi-documents">OpenAPI</a></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>
<td>We support the following OpenAPI versions: <em><a href="./docs/usage.md#generate-models-from-swagger-20-documents">Swagger 2.0</a>, <a href="./docs/usage.md#generate-models-from-openapi-documents">OpenAPI 3.0 and 3.1</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</a></td>
Expand Down
1 change: 1 addition & 0 deletions docs/usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ Long term if this is something you wish was supported, voice your [opinion here]
There are one way to generate models from an OpenAPI document

- [Generate from OpenAPI 3.0 JS object](../examples/openapi-from-object)
- [Generate from OpenAPI 3.1 JS object](../examples/openapi-v3_1-from-object)

The OpenAPI input processor expects that the property `openapi` is defined in order to know it should be processed.

Expand Down
2 changes: 1 addition & 1 deletion examples/openapi-from-object/package-lock.json

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

17 changes: 17 additions & 0 deletions examples/openapi-v3_1-from-object/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# OpenAPI 3.1 from object

A basic example of how to use Modelina with a basic OpenAPI v3.1.0 object.

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

exports[`Should be able to process a pure OpenAPI V3.1 object and should log expected output to console 1`] = `
Array [
"class TestPost_200ApplicationJson {
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; }
}",
]
`;

exports[`Should be able to process a pure OpenAPI V3.1 object and should log expected output to console 2`] = `
Array [
"class TestPostMultipartFormMinusData {
private _id?: string;
private _address?: Map<string, any>;
private _profileImage?: string;
private _additionalProperties?: Map<string, any>;
constructor(input: {
id?: string,
address?: Map<string, any>,
profileImage?: string,
additionalProperties?: Map<string, any>,
}) {
this._id = input.id;
this._address = input.address;
this._profileImage = input.profileImage;
this._additionalProperties = input.additionalProperties;
}
get id(): string | undefined { return this._id; }
set id(id: string | undefined) { this._id = id; }
get address(): Map<string, any> | undefined { return this._address; }
set address(address: Map<string, any> | undefined) { this._address = address; }
get profileImage(): string | undefined { return this._profileImage; }
set profileImage(profileImage: string | undefined) { this._profileImage = profileImage; }
get additionalProperties(): Map<string, any> | undefined { return this._additionalProperties; }
set additionalProperties(additionalProperties: Map<string, any> | undefined) { this._additionalProperties = additionalProperties; }
}",
]
`;
15 changes: 15 additions & 0 deletions examples/openapi-v3_1-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 process a pure OpenAPI V3.1 object', () => {
afterAll(() => {
jest.restoreAllMocks();
});
test('and should log expected output to console', async () => {
await generate();
expect(spy.mock.calls.length).toEqual(2);
expect(spy.mock.calls[0]).toMatchSnapshot();
expect(spy.mock.calls[1]).toMatchSnapshot();
});
});
70 changes: 70 additions & 0 deletions examples/openapi-v3_1-from-object/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import { TypeScriptGenerator } from '../../src';

const generator = new TypeScriptGenerator();
const swaggerDocument = {
openapi: '3.1.0',
info: {
version: '0.1',
title: 'Simple basic api'
},
paths: {
'/test': {
post: {
requestBody: {
content: {
'multipart/form-data': {
schema: {
type: 'object',
properties: {
id: {
type: 'string',
format: 'uuid'
},
address: {
type: 'object',
properties: {}
},
profileImage: {
type: 'string',
contentMediaType: 'image/png',
contentEncoding: 'base64'
}
}
}
}
}
},
responses: {
200: {
content: {
'application/json': {
schema: {
$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 models = await generator.generate(swaggerDocument);
for (const model of models) {
console.log(model.result);
}
}

if (require.main === module) {
generate();
}
10 changes: 10 additions & 0 deletions examples/openapi-v3_1-from-object/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/openapi-v3_1-from-object/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "openapi-v3_1-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"
}
}
89 changes: 33 additions & 56 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 @@ -36,7 +36,7 @@
],
"dependencies": {
"@apidevtools/json-schema-ref-parser": "^11.1.0",
"@apidevtools/swagger-parser": "^10.0.3",
"@apidevtools/swagger-parser": "^10.1.0",
"@asyncapi/parser": "^3.0.7",
"@smoya/multi-parser": "^5.0.1",
"@swc/core": "^1.3.5",
Expand Down
8 changes: 6 additions & 2 deletions src/models/OpenapiV3Schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ export class OpenapiV3Schema {
additionalItems?: OpenapiV3Schema | boolean;

//Draft 6 modifications
exclusiveMaximum?: number;
exclusiveMinimum?: number;
exclusiveMaximum?: boolean | number;
exclusiveMinimum?: boolean | number;
//Draft 6 replacements
$id?: string; //Replaces 'id'
//Draft 6 additions
Expand All @@ -103,6 +103,10 @@ export class OpenapiV3Schema {
externalDocs?: OpenAPIV3ExternalDocumentation;
example?: any;
deprecated?: boolean;

//OpenAPI 3.0 -> 3.1.0 additions
contentEncoding?: string;
contentMediaType?: string;
//Extensions
[k: string]: any; // eslint-disable-line no-undef

Expand Down
Loading

0 comments on commit 35f871a

Please sign in to comment.