modelina icon indicating copy to clipboard operation
modelina copied to clipboard

feat: add nullable object in ts generator

Open jeremylvln opened this issue 3 years ago • 2 comments

Description

When a model with a composed type of ['object', 'null'], the TS generator will generate an Inner type for the object and create a nullable type alias.

Example (from the tests):

The following model,

{
  $id: 'NullableObject',
  type: ['object', 'null'],
  properties: {
    foo: {
      type: 'string'
    },
  },
  additionalProperties: false,
}

Will generate the following code:

interface NullableObjectInner {
  foo?: string;
}
type NullableObject = NullableObjectInner | null;

or

class NullableObjectInner {
  private _foo?: string;

  constructor(input: {
    foo?: string,
  }) {
    this._foo = input.foo;
  }

  get foo(): string | undefined { return this._foo; }
  set foo(foo: string | undefined) { this._foo = foo; }
}
type NullableObject = NullableObjectInner | null;

Related issue(s)

Fixes #754

jeremylvln avatar May 18 '22 15:05 jeremylvln

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

No Coverage information No Coverage information
0.0% 0.0% Duplication

sonarqubecloud[bot] avatar May 18 '22 15:05 sonarqubecloud[bot]

Pull Request Test Coverage Report for Build 2346704745

  • 14 of 15 (93.33%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage decreased (-0.004%) to 92.101%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/generators/typescript/TypeScriptGenerator.ts 8 9 88.89%
<!-- Total: 14 15
Totals Coverage Status
Change from base Build 2345953286: -0.004%
Covered Lines: 2817
Relevant Lines: 2916

💛 - Coveralls

coveralls avatar May 23 '22 06:05 coveralls