Skip to content

Commit

Permalink
feat: allow change of how enum is interpreted when a single value (#1853
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jonaslagoni authored Mar 10, 2024
1 parent 0134760 commit 2d62fb8
Show file tree
Hide file tree
Showing 23 changed files with 600 additions and 272 deletions.
6 changes: 6 additions & 0 deletions docs/migrations/version-3-to-4.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

This document contain all the breaking changes and migrations guidelines for adapting your code to the new version.

## Deprecation of `processor.interpreter`

Since the early days we had the option to set `processorOptions.interpreter` options to change how JSON Schema is interpreted to Meta models. However, these options are more accurately part of the `processorOptions.jsonSchema` options.

Use this instead going forward.

## Fixed edge cases for camel case names

Naming such as object properties using camel case formatting had an edge case where if they contained a number followed by an underscore and a letter it would be incorrectly formatted. This has been fixed in this version, which might mean properties, model names, etc that use camel case might be renamed.
Expand Down
6 changes: 3 additions & 3 deletions docs/presets.md
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ class Root {
}
```

### Ap/pre-pending to existng rendered content
### Ap/pre-pending to existing rendered content
As the hello world example appended content, this time lets prepend some content to the properties.
```ts
import { TypeScriptGenerator } from '@asyncapi/modelina';
Expand All @@ -199,7 +199,7 @@ const generator = new TypeScriptGenerator({
class: {
property({ content }) {
const description = '// Hello world!'
return `${description}\n${content}`;
return `${content}\n${description}`;
}
}
}
Expand Down Expand Up @@ -305,7 +305,7 @@ self({ dependencyManager, content }) {

Some languages has specific helper functions, and some very basic interfaces, such as for Java.

In TypeScript because you can have different import syntaxes based on the module system such as [CJS](../examples/typescript-use-cjs/) or [ESM](../examples/typescript-use-esm/), therefore it provies a specific function `addTypeScriptDependency` that takes care of that logic, and you just have to remember `addTypeScriptDependency('ImportanWhat', 'FromWhere')`.
In TypeScript because you can have different import syntaxes based on the module system such as [CJS](../examples/typescript-use-cjs/) or [ESM](../examples/typescript-use-esm/), therefore it provides a specific function `addTypeScriptDependency` that takes care of that logic, and you just have to remember `addTypeScriptDependency('Import what', 'From where')`.

### Overriding the default preset

Expand Down
2 changes: 2 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ These examples show a specific input and how they can be used:
- [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.
- [json-schema-single-enum-as-const](./json-schema-single-enum-as-const) - An advanced example that shows how to change how `enum` are interpreted when containing a single value.
- [swagger2.0-from-object](./swagger2.0-from-object) - A basic example where a Swagger 2.0 JS object is used to generate models.
- [meta-model](./meta-model) - A basic example how to provide a meta model for the generator


## General examples
These are examples that can be applied in all scenarios.
- [include-custom-function](./include-custom-function) - A basic example where a custom function is included.
Expand Down
19 changes: 19 additions & 0 deletions examples/json-schema-single-enum-as-const/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# JSON Schema interpret single enum as constant

This example shows how to use the `interpretSingleEnumAsConst` option on the JSON Schema input processor to instead of interpreting `{enum: ['single value']}` as an enum, it will instead only be generated as a constant value.

This ONLY applies when it's a single value.

## 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,17 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Should be able to render TEMPLATE and should log expected output to console 1`] = `
Array [
"class Root {
private _email?: 'test' = 'test';
constructor(input: {
}) {
}
get email(): 'test' | undefined { return this._email; }
}",
]
`;
15 changes: 15 additions & 0 deletions examples/json-schema-single-enum-as-const/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 TEMPLATE', () => {
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();
});
});
30 changes: 30 additions & 0 deletions examples/json-schema-single-enum-as-const/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { TypeScriptGenerator } from '../../src';

const generator = new TypeScriptGenerator({
processorOptions: {
jsonSchema: {
interpretSingleEnumAsConst: true
}
}
});
const jsonSchemaDraft7 = {
$schema: 'http://json-schema.org/draft-07/schema#',
type: 'object',
additionalProperties: false,
properties: {
email: {
type: 'string',
enum: ['test']
}
}
};

export async function generate(): Promise<void> {
const models = await generator.generate(jsonSchemaDraft7);
for (const model of models) {
console.log(model.result);
}
}
if (require.main === module) {
generate();
}
10 changes: 10 additions & 0 deletions examples/json-schema-single-enum-as-const/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/json-schema-single-enum-as-const/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "json-schema-single-enum-as-const" },
"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

0 comments on commit 2d62fb8

Please sign in to comment.