Skip to content

Commit

Permalink
update implementation and test
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslagoni committed Mar 11, 2024
1 parent 922f614 commit 0a56771
Show file tree
Hide file tree
Showing 6 changed files with 105 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# JSON Schema change property name for additional properties

This example shows how to use the `propertyNameForAdditionalProperties` option on the JSON Schema input processor to change the property name that is used for representing additional properties.

## 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 render object with different additional property and should log expected output to console 1`] = `
Array [
"class Root {
private _email?: string;
private _extensions?: Map<string, any>;
constructor(input: {
email?: string,
extensions?: Map<string, any>,
}) {
this._email = input.email;
this._extensions = input.extensions;
}
get email(): string | undefined { return this._email; }
set email(email: string | undefined) { this._email = email; }
get extensions(): Map<string, any> | undefined { return this._extensions; }
set extensions(extensions: Map<string, any> | undefined) { this._extensions = extensions; }
}",
]
`;
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 object with different additional property', () => {
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();
});
});
29 changes: 29 additions & 0 deletions examples/json-schema-additional-properties-representation/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { TypeScriptGenerator } from '../../src';

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

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();
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"config" : { "example_name" : "json-schema-additional-properties-representation" },
"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"
}
}

0 comments on commit 0a56771

Please sign in to comment.