Repository: asyncapi/modelina
Pull request #333 (2021-08-30 14:04:00)

fix: c# generator should render types as nullable by default
**Description**
This PR introduces a change to the underlying C# type rendering. It changes the rendering for `float`, `int` and `bool` to being optional primitives, instead of primitives. As we don't render required properties (yet, gonna do a follow-up issue) we should make them optional.

sonarcloud[bot]: Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; ![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')

[![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=BUG)  
[![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=VULNERABILITY)  
[![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=SECURITY_HOTSPOT)  
[![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=333&resolved=false&types=CODE_SMELL)

[![No Coverage information](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/NoCoverageInfo-16px.png 'No Coverage information')](https://sonarcloud.io/component_measures?id=asyncapi_generator-model-sdk&pullRequest=333) No Coverage information  
[![0.1%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.1%')](https://sonarcloud.io/component_measures?id=asyncapi_generator-model-sdk&pullRequest=333&metric=new_duplicated_lines_density&view=list) [0.1% Duplication](https://sonarcloud.io/component_measures?id=asyncapi_generator-model-sdk&pullRequest=333&metric=new_duplicated_lines_density&view=list)



Patch:
diff --git a/src/generators/csharp/CSharpRenderer.ts b/src/generators/csharp/CSharpRenderer.ts
index 4fd0bda4..b14b968c 100644
--- a/src/generators/csharp/CSharpRenderer.ts
+++ b/src/generators/csharp/CSharpRenderer.ts
@@ -76,11 +76,11 @@ export abstract class CSharpRenderer extends AbstractRenderer<CSharpOptions> {
     case 'string':
       return 'string';
     case 'integer':
-      return 'int';
+      return 'int?';
     case 'number':
-      return 'float';
+      return 'float?';
     case 'boolean':
-      return 'bool';
+      return 'bool?';
     case 'object':
       return 'object';
     case 'array': {


Test patch:
diff --git a/test/generators/csharp/CSharpRenderer.spec.ts b/test/generators/csharp/CSharpRenderer.spec.ts
index 213f1198..fc39ce9d 100644
--- a/test/generators/csharp/CSharpRenderer.spec.ts
+++ b/test/generators/csharp/CSharpRenderer.spec.ts
@@ -14,16 +14,16 @@ describe('CSharpRenderer', () => {
     test('Should render undefined as dynamic type', () => {
       expect(renderer.toCSharpType(undefined, new CommonModel())).toEqual('dynamic');
     });
-    test('Should render integer as int type', () => {
-      expect(renderer.toCSharpType('integer', new CommonModel())).toEqual('int');
+    test('Should render integer as nullable int type', () => {
+      expect(renderer.toCSharpType('integer', new CommonModel())).toEqual('int?');
     });
-    test('Should render number as float type', () => {
-      expect(renderer.toCSharpType('number', new CommonModel())).toEqual('float');
+    test('Should render number as nullable float type', () => {
+      expect(renderer.toCSharpType('number', new CommonModel())).toEqual('float?');
     });
     test('Should render array as slice of the type', () => {
       const model = new CommonModel();
       model.items = CommonModel.toCommonModel({ type: 'number' });
-      expect(renderer.toCSharpType('array', model)).toEqual('float[]');
+      expect(renderer.toCSharpType('array', model)).toEqual('float?[]');
     });
     test('Should render tuple with multiple types as dynamic', () => {
       const model = new CommonModel();
@@ -56,7 +56,7 @@ describe('CSharpRenderer', () => {
     test('Should render single union type', () => {
       const model = CommonModel.toCommonModel({ type: ['number'] });
       const renderedType = renderer.renderType(model);
-      expect(renderedType).toEqual('float');
+      expect(renderedType).toEqual('float?');
     });
     test('Should render union types with multiple types as slice of interface', () => {
       const model = CommonModel.toCommonModel({ type: ['number', 'string'] });
diff --git a/test/generators/csharp/__snapshots__/CSharpGenerator.spec.ts.snap b/test/generators/csharp/__snapshots__/CSharpGenerator.spec.ts.snap
index b511ba34..5cbb217c 100644
--- a/test/generators/csharp/__snapshots__/CSharpGenerator.spec.ts.snap
+++ b/test/generators/csharp/__snapshots__/CSharpGenerator.spec.ts.snap
@@ -43,8 +43,8 @@ exports[`CSharpGenerator should render \`class\` type 1`] = `
   private string streetName;
   private string city;
   private string state;
-  private float houseNumber;
-  private bool marriage;
+  private float? houseNumber;
+  private bool? marriage;
   private dynamic members;
   private dynamic[] tupleType;
   private string[] arrayType;
@@ -69,13 +69,13 @@ exports[`CSharpGenerator should render \`class\` type 1`] = `
     set { state = value; }
   }
 
-  public float HouseNumber 
+  public float? HouseNumber 
   {
     get { return houseNumber; }
     set { houseNumber = value; }
   }
 
-  public bool Marriage 
+  public bool? Marriage 
   {
     get { return marriage; }
     set { marriage = value; }
@@ -118,8 +118,8 @@ exports[`CSharpGenerator should render \`class\` type 2`] = `
   private string streetName;
   private string city;
   private string state;
-  private float houseNumber;
-  private bool marriage;
+  private float? houseNumber;
+  private bool? marriage;
   private dynamic members;
   private dynamic[] tupleType;
   private string[] arrayType;
@@ -144,13 +144,13 @@ exports[`CSharpGenerator should render \`class\` type 2`] = `
     set { state = value; }
   }
 
-  public float HouseNumber 
+  public float? HouseNumber 
   {
     get { return houseNumber; }
     set { houseNumber = value; }
   }
 
-  public bool Marriage 
+  public bool? Marriage 
   {
     get { return marriage; }
     set { marriage = value; }