-
-
Notifications
You must be signed in to change notification settings - Fork 196
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rendering only not-required value-typed properties as nullable (#…
…747)
- Loading branch information
1 parent
446d25a
commit 21953c8
Showing
9 changed files
with
146 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# C# Generate required properties | ||
|
||
An example showing that when a property of a not-nullable type (value type) is set as required, it's not going to be made into nullable. | ||
|
||
## 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 | ||
``` |
37 changes: 37 additions & 0 deletions
37
examples/csharp-generate-required-properties/__snapshots__/index.spec.ts.snap
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Jest Snapshot v1, https://goo.gl/fbAQLP | ||
|
||
exports[`Should be able to render required properties without the question mark at the end if the type of it is not nullable and should log expected output to console 1`] = ` | ||
Array [ | ||
"public class Root | ||
{ | ||
private bool requiredBoolean; | ||
private bool? notRequiredBoolean; | ||
private string requiredString; | ||
private string notRequiredString; | ||
public bool RequiredBoolean | ||
{ | ||
get { return requiredBoolean; } | ||
set { requiredBoolean = value; } | ||
} | ||
public bool? NotRequiredBoolean | ||
{ | ||
get { return notRequiredBoolean; } | ||
set { notRequiredBoolean = value; } | ||
} | ||
public string RequiredString | ||
{ | ||
get { return requiredString; } | ||
set { requiredString = value; } | ||
} | ||
public string NotRequiredString | ||
{ | ||
get { return notRequiredString; } | ||
set { notRequiredString = value; } | ||
} | ||
}", | ||
] | ||
`; |
14 changes: 14 additions & 0 deletions
14
examples/csharp-generate-required-properties/index.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 render required properties without the question mark at the end if the type of it is not nullable', () => { | ||
afterAll(() => { | ||
jest.restoreAllMocks(); | ||
}); | ||
test('and should log expected output to console', async () => { | ||
await generate(); | ||
//Generate is called 2x, so even though we expect 1 model, we double it | ||
expect(spy.mock.calls.length).toEqual(2); | ||
expect(spy.mock.calls[1]).toMatchSnapshot(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { CSharpGenerator } from '../../src'; | ||
|
||
const generator = new CSharpGenerator(); | ||
const jsonSchemaDraft7 = { | ||
$schema: 'http://json-schema.org/draft-07/schema#', | ||
type: 'object', | ||
additionalProperties: false, | ||
properties: { | ||
requiredBoolean: { | ||
type: 'boolean', | ||
}, | ||
notRequiredBoolean: { | ||
type: 'boolean', | ||
}, | ||
requiredString: { | ||
type: 'string' | ||
}, | ||
notRequiredString: { | ||
type: 'string' | ||
}, | ||
}, | ||
required: ['requiredBoolean', 'requiredString'] | ||
}; | ||
|
||
export async function generate(): Promise<void> { | ||
const models = await generator.generate(jsonSchemaDraft7); | ||
for (const model of models) { | ||
console.log(model.result); | ||
} | ||
} | ||
generate(); |
10 changes: 10 additions & 0 deletions
10
examples/csharp-generate-required-properties/package-lock.json
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
{ | ||
"config": { | ||
"example_name": "csharp-generate-required-properties" | ||
}, | ||
"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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters