Repository: asyncapi/modelina
Pull request #356 (2021-09-06 10:38:16)

fix: csharp enum de/serializer
**Description**
Currently, enums are rendered not as their values, but as the index of the enum value. This PR uses the new functionality in https://github.com/asyncapi/modelina/pull/355 to render accurate values.


jonaslagoni: Blocked by https://github.com/asyncapi/modelina/pull/355
sonarcloud[bot]: SonarCloud Quality Gate failed.&nbsp; &nbsp; ![Quality Gate failed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/failed-16px.png 'Quality Gate failed')

[![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=356&resolved=false&types=BUG) [![C](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/C-16px.png 'C')](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&resolved=false&types=BUG) [1 Bug](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=CODE_SMELL) [1 Code Smell](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356) 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=356&metric=new_duplicated_lines_density&view=list) [0.1% Duplication](https://sonarcloud.io/component_measures?id=asyncapi_generator-model-sdk&pullRequest=356&metric=new_duplicated_lines_density&view=list)


coveralls: ## Pull Request Test Coverage Report for [Build 1205658923](https://coveralls.io/builds/42673670)

* **45** of **51**   **(88.24%)**  changed or added relevant lines in **2** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage decreased (**-0.3%**) to **93.121%**

---

|  Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [src/generators/csharp/renderers/EnumRenderer.ts](https://coveralls.io/builds/42673670/source?filename=src%2Fgenerators%2Fcsharp%2Frenderers%2FEnumRenderer.ts#L65) | 28 | 29 | 96.55%
| [src/generators/csharp/presets/JsonSerializerPreset.ts](https://coveralls.io/builds/42673670/source?filename=src%2Fgenerators%2Fcsharp%2Fpresets%2FJsonSerializerPreset.ts#L13) | 17 | 22 | 77.27%
<!-- | **Total:** | **45** | **51** | **88.24%** | -->


|  Totals | [![Coverage Status](https://coveralls.io/builds/42673670/badge)](https://coveralls.io/builds/42673670) |
| :-- | --: |
| Change from base [Build 1198064241](https://coveralls.io/builds/42639792): |  -0.3% |
| Covered Lines: | 1899 |
| Relevant Lines: | 1944 |

---
##### 💛  - [Coveralls](https://coveralls.io)

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=356&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=356&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=CODE_SMELL) [1 Code Smell](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356) 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=356&metric=new_duplicated_lines_density&view=list) [0.1% Duplication](https://sonarcloud.io/component_measures?id=asyncapi_generator-model-sdk&pullRequest=356&metric=new_duplicated_lines_density&view=list)


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=356&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=356&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=CODE_SMELL) [1 Code Smell](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356) 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=356&metric=new_duplicated_lines_density&view=list) [0.1% Duplication](https://sonarcloud.io/component_measures?id=asyncapi_generator-model-sdk&pullRequest=356&metric=new_duplicated_lines_density&view=list)


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=356&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=356&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=CODE_SMELL) [1 Code Smell](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356) 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=356&metric=new_duplicated_lines_density&view=list) [0.1% Duplication](https://sonarcloud.io/component_measures?id=asyncapi_generator-model-sdk&pullRequest=356&metric=new_duplicated_lines_density&view=list)


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=356&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=356&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=asyncapi_generator-model-sdk&pullRequest=356&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=356&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=356&resolved=false&types=CODE_SMELL) [0 Code Smells](https://sonarcloud.io/project/issues?id=asyncapi_generator-model-sdk&pullRequest=356&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=356) 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=356&metric=new_duplicated_lines_density&view=list) [0.1% Duplication](https://sonarcloud.io/component_measures?id=asyncapi_generator-model-sdk&pullRequest=356&metric=new_duplicated_lines_density&view=list)



Patch:
diff --git a/src/generators/csharp/presets/JsonSerializerPreset.ts b/src/generators/csharp/presets/JsonSerializerPreset.ts
index 2fcb14f2..61d405c0 100644
--- a/src/generators/csharp/presets/JsonSerializerPreset.ts
+++ b/src/generators/csharp/presets/JsonSerializerPreset.ts
@@ -1,9 +1,22 @@
 import { CSharpRenderer } from '../CSharpRenderer';
 import { CSharpPreset } from '../CSharpPreset';
-import { getUniquePropertyName, DefaultPropertyNames, FormatHelpers } from '../../../helpers';
-import { CommonModel } from '../../../models';
+import { getUniquePropertyName, DefaultPropertyNames, FormatHelpers, TypeHelpers, ModelKind } from '../../../helpers';
+import { CommonInputModel, CommonModel } from '../../../models';
 
-function renderSerializeAdditionalProperties(model: CommonModel, renderer: CSharpRenderer) {
+function renderSerializeProperty(modelInstanceVariable: string, model: CommonModel, inputModel: CommonInputModel) {
+  let value = modelInstanceVariable;
+  if (model.$ref) {
+    const resolvedModel = inputModel.models[model.$ref];
+    const propertyModelKind = TypeHelpers.extractKind(resolvedModel);
+    //Referenced enums is the only one who need custom serialization
+    if (propertyModelKind === ModelKind.ENUM) {
+      value = `${value}.GetValue()`;
+    }
+  }
+  return `JsonSerializer.Serialize(writer, ${value});`;
+}
+
+function renderSerializeAdditionalProperties(model: CommonModel, renderer: CSharpRenderer, inputModel: CommonInputModel) {
   const serializeAdditionalProperties = '';
   if (model.additionalProperties !== undefined) {
     let additionalPropertyName = getUniquePropertyName(model, DefaultPropertyNames.additionalProperties);
@@ -19,28 +32,29 @@ if (value.AdditionalProperties != null) {
     }
     // write property name and let the serializer serialize the value itself
     writer.WritePropertyName(additionalProperty.Key);
-    JsonSerializer.Serialize(writer, additionalProperty.Value);
+    ${renderSerializeProperty('additionalProperty.Value', model.additionalProperties, inputModel)}
   }
 }`;
   }
   return serializeAdditionalProperties;
 }
 
-function renderSerializeProperties(model: CommonModel, renderer: CSharpRenderer) {
+function renderSerializeProperties(model: CommonModel, renderer: CSharpRenderer, inputModel: CommonInputModel) {
   let serializeProperties = '';
   if (model.properties !== undefined) {
     for (const [propertyName, propertyModel] of Object.entries(model.properties)) {
       const formattedPropertyName = FormatHelpers.upperFirst(renderer.nameProperty(propertyName, propertyModel));
-      serializeProperties += `if(value.${formattedPropertyName} != null) { 
+      const modelInstanceVariable = `value.${formattedPropertyName}`;
+      serializeProperties += `if(${modelInstanceVariable} != null) { 
   // write property name and let the serializer serialize the value itself
   writer.WritePropertyName("${propertyName}");
-  JsonSerializer.Serialize(writer, value.${formattedPropertyName});
+  ${renderSerializeProperty(modelInstanceVariable, propertyModel, inputModel)}
 }\n`;
     }
   }
   return serializeProperties;
 }
-function renderSerializePatternProperties(model: CommonModel, renderer: CSharpRenderer) {
+function renderSerializePatternProperties(model: CommonModel, renderer: CSharpRenderer, inputModel: CommonInputModel) {
   let serializePatternProperties = '';
   if (model.patternProperties !== undefined) {
     for (const [pattern, patternModel] of Object.entries(model.patternProperties)) {
@@ -57,7 +71,7 @@ if(value.${patternPropertyName} != null) {
     }
     // write property name and let the serializer serialize the value itself
     writer.WritePropertyName(patternProp.Key);
-    JsonSerializer.Serialize(writer, patternProp.Value);
+    ${renderSerializeProperty('patternProp.Value', patternModel, inputModel)}
   }
 }`;
     }
@@ -87,14 +101,15 @@ function renderPropertiesList(model: CommonModel, renderer: CSharpRenderer) {
 /**
  * Render `serialize` function based on model
  */
-function renderSerialize({ renderer, model }: {
+function renderSerialize({ renderer, model, inputModel }: {
   renderer: CSharpRenderer,
   model: CommonModel,
+  inputModel: CommonInputModel
 }): string {
   const formattedModelName = renderer.nameType(model.$id);
-  const serializeProperties = renderSerializeProperties(model, renderer);
-  const serializePatternProperties = renderSerializePatternProperties(model, renderer);
-  const serializeAdditionalProperties = renderSerializeAdditionalProperties(model, renderer);
+  const serializeProperties = renderSerializeProperties(model, renderer, inputModel);
+  const serializePatternProperties = renderSerializePatternProperties(model, renderer, inputModel);
+  const serializeAdditionalProperties = renderSerializeAdditionalProperties(model, renderer, inputModel);
   const propertiesList = renderPropertiesList(model, renderer);
 
   return `public override void Write(Utf8JsonWriter writer, ${formattedModelName} value, JsonSerializerOptions options)
@@ -118,14 +133,26 @@ ${renderer.indent(serializeAdditionalProperties)}
 }`;
 } 
 
-function renderDeserializeProperties(model: CommonModel, renderer: CSharpRenderer) {
+function renderDeserializeProperty(type: string, model: CommonModel, inputModel: CommonInputModel) {
+  if (model.$ref) {
+    const resolvedModel = inputModel.models[model.$ref];
+    const propertyModelKind = TypeHelpers.extractKind(resolvedModel);
+    //Referenced enums is the only one who need custom serialization
+    if (propertyModelKind === ModelKind.ENUM) {
+      return `${type}Extension.To${type}(JsonSerializer.Deserialize<dynamic>(ref reader, options))`;
+    }
+  }
+  return `JsonSerializer.Deserialize<${type}>(ref reader, options)`;
+}
+
+function renderDeserializeProperties(model: CommonModel, renderer: CSharpRenderer, inputModel: CommonInputModel) {
   const propertyEntries = Object.entries(model.properties || {});
   const deserializeProperties = propertyEntries.map(([prop, propModel]) => {
     const formattedPropertyName = FormatHelpers.upperFirst(renderer.nameProperty(prop, propModel));
     const propertyModelType = renderer.renderType(propModel);
     return `if (propertyName == "${prop}")
 {
-  var value = JsonSerializer.Deserialize<${propertyModelType}>(ref reader, options);
+  var value = ${renderDeserializeProperty(propertyModelType, propModel, inputModel)};
   instance.${formattedPropertyName} = value;
   continue;
 }`;
@@ -133,7 +160,7 @@ function renderDeserializeProperties(model: CommonModel, renderer: CSharpRendere
   return deserializeProperties.join('\n');
 }
 
-function renderDeserializePatternProperties(model: CommonModel, renderer: CSharpRenderer) {
+function renderDeserializePatternProperties(model: CommonModel, renderer: CSharpRenderer, inputModel: CommonInputModel) {
   if (model.patternProperties === undefined) {
     return '';
   }
@@ -145,7 +172,7 @@ function renderDeserializePatternProperties(model: CommonModel, renderer: CSharp
 var match = Regex.Match(propertyName, @"${pattern}");
 if (match.Success)
 {
-  var deserializedValue = JsonSerializer.Deserialize<${patternPropertyType}>(ref reader, options);
+  var deserializedValue = ${renderDeserializeProperty(patternPropertyType, patternModel, inputModel)};
   instance.${patternPropertyName}.Add(propertyName, deserializedValue);
   continue;
 }`;
@@ -153,7 +180,7 @@ if (match.Success)
   return patternProperties.join('\n');
 }
 
-function renderDeserializeAdditionalProperties(model: CommonModel, renderer: CSharpRenderer) {
+function renderDeserializeAdditionalProperties(model: CommonModel, renderer: CSharpRenderer, inputModel: CommonInputModel) {
   if (model.additionalProperties === undefined) {
     return ''; 
   }
@@ -161,7 +188,7 @@ function renderDeserializeAdditionalProperties(model: CommonModel, renderer: CSh
   additionalPropertyName = FormatHelpers.upperFirst(renderer.nameProperty(additionalPropertyName, model.additionalProperties));
   const additionalPropertyType = renderer.renderType(model.additionalProperties);
   return `if(instance.${additionalPropertyName} == null) { instance.${additionalPropertyName} = new Dictionary<string, ${additionalPropertyType}>(); }
-var deserializedValue = JsonSerializer.Deserialize<${additionalPropertyType}>(ref reader, options);
+var deserializedValue = ${renderDeserializeProperty(additionalPropertyType, model.additionalProperties, inputModel)};
 instance.${additionalPropertyName}.Add(propertyName, deserializedValue);
 continue;`;
 }
@@ -169,14 +196,15 @@ continue;`;
 /**
  * Render `deserialize` function based on model
  */
-function renderDeserialize({ renderer, model }: {
+function renderDeserialize({ renderer, model, inputModel }: {
   renderer: CSharpRenderer,
   model: CommonModel,
+  inputModel: CommonInputModel
 }): string {
   const formattedModelName = renderer.nameType(model.$id);
-  const deserializeProperties = renderDeserializeProperties(model, renderer);
-  const deserializePatternProperties = renderDeserializePatternProperties(model, renderer);
-  const deserializeAdditionalProperties = renderDeserializeAdditionalProperties(model, renderer);
+  const deserializeProperties = renderDeserializeProperties(model, renderer, inputModel);
+  const deserializePatternProperties = renderDeserializePatternProperties(model, renderer, inputModel);
+  const deserializeAdditionalProperties = renderDeserializeAdditionalProperties(model, renderer, inputModel);
   return `public override ${formattedModelName} Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
 {
   if (reader.TokenType != JsonTokenType.StartObject)
@@ -218,14 +246,14 @@ ${renderer.indent(deserializeAdditionalProperties, 4)}
  */
 export const CSHARP_JSON_SERIALIZER_PRESET: CSharpPreset = {
   class: {
-    self({ renderer, model, content }) {
+    self({ renderer, model, content, inputModel}) {
       renderer.addDependency('using System.Text.Json;');
       renderer.addDependency('using System.Text.Json.Serialization;');
       renderer.addDependency('using System.Text.RegularExpressions;');
       
       const formattedModelName = renderer.nameType(model.$id);
-      const deserialize = renderDeserialize({renderer, model});
-      const serialize = renderSerialize({renderer, model});
+      const deserialize = renderDeserialize({renderer, model, inputModel});
+      const serialize = renderSerialize({renderer, model, inputModel});
 
       return `[JsonConverter(typeof(${formattedModelName}Converter))]
 ${content}


Test patch:
diff --git a/test/generators/csharp/presets/JsonSerializerPreset.spec.ts b/test/generators/csharp/presets/JsonSerializerPreset.spec.ts
index 0c0634c4..98499be9 100644
--- a/test/generators/csharp/presets/JsonSerializerPreset.spec.ts
+++ b/test/generators/csharp/presets/JsonSerializerPreset.spec.ts
@@ -7,6 +7,7 @@ const doc = {
   properties: {
     'string prop': { type: 'string' },
     numberProp: { type: 'number' },
+    enumProp: { $id: 'EnumTest', enum: ['Some enum String', true, {test: 'test'}, 2]},
     objectProp: { type: 'object', $id: 'NestedTest', properties: {stringProp: { type: 'string' }}}
   },
   patternProperties: {
@@ -23,13 +24,15 @@ describe('JSON serializer preset', () => {
       ]
     });
     const inputModel = await generator.process(doc);
-    const testModel = inputModel.models['Test'];
     const nestedTestModel = inputModel.models['NestedTest'];
+    const testModel = inputModel.models['Test'];
+    const enumModel = inputModel.models['EnumTest'];
 
-    const testClass = await generator.renderClass(testModel, inputModel);
     const nestedTestClass = await generator.renderClass(nestedTestModel, inputModel);
-
-    expect(testClass.result).toMatchSnapshot();
+    const testClass = await generator.renderClass(testModel, inputModel);
+    const enumEnum = await generator.renderEnum(enumModel, inputModel);
     expect(nestedTestClass.result).toMatchSnapshot();
+    expect(testClass.result).toMatchSnapshot();
+    expect(enumEnum.result).toMatchSnapshot();
   });
 });
diff --git a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap
index 888522d6..c0f02bc9 100644
--- a/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap
+++ b/test/generators/csharp/presets/__snapshots__/JsonSerializerPreset.spec.ts.snap
@@ -1,13 +1,10 @@
 // Jest Snapshot v1, https://goo.gl/fbAQLP
 
 exports[`JSON serializer preset should render serialize and deserialize converters 1`] = `
-"[JsonConverter(typeof(TestConverter))]
-public class Test {
+"[JsonConverter(typeof(NestedTestConverter))]
+public class NestedTest {
   private string stringProp;
-  private float? numberProp;
-  private NestedTest objectProp;
   private Dictionary<string, dynamic> additionalProperties;
-  private Dictionary<string, string> sTestPatternProperties;
 
   public string StringProp 
   {
@@ -15,46 +12,28 @@ public class Test {
     set { stringProp = value; }
   }
 
-  public float? NumberProp 
-  {
-    get { return numberProp; }
-    set { numberProp = value; }
-  }
-
-  public NestedTest ObjectProp 
-  {
-    get { return objectProp; }
-    set { objectProp = value; }
-  }
-
   public Dictionary<string, dynamic> AdditionalProperties 
   {
     get { return additionalProperties; }
     set { additionalProperties = value; }
   }
-
-  public Dictionary<string, string> STestPatternProperties 
-  {
-    get { return sTestPatternProperties; }
-    set { sTestPatternProperties = value; }
-  }
 }
 
-internal class TestConverter : JsonConverter<Test>
+internal class NestedTestConverter : JsonConverter<NestedTest>
 {
   public override bool CanConvert(Type objectType)
   {
     // this converter can be applied to any type
     return true;
   }
-  public override Test Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+  public override NestedTest Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
   {
     if (reader.TokenType != JsonTokenType.StartObject)
     {
       throw new JsonException();
     }
 
-    var instance = new Test();
+    var instance = new NestedTest();
   
     while (reader.Read())
     {
@@ -70,33 +49,14 @@ internal class TestConverter : JsonConverter<Test>
       }
 
       string propertyName = reader.GetString();
-      if (propertyName == \\"string prop\\")
+      if (propertyName == \\"stringProp\\")
       {
         var value = JsonSerializer.Deserialize<string>(ref reader, options);
         instance.StringProp = value;
         continue;
       }
-      if (propertyName == \\"numberProp\\")
-      {
-        var value = JsonSerializer.Deserialize<float?>(ref reader, options);
-        instance.NumberProp = value;
-        continue;
-      }
-      if (propertyName == \\"objectProp\\")
-      {
-        var value = JsonSerializer.Deserialize<NestedTest>(ref reader, options);
-        instance.ObjectProp = value;
-        continue;
-      }
 
-      if(instance.STestPatternProperties == null) { instance.STestPatternProperties = new Dictionary<string, string>(); }
-      var match = Regex.Match(propertyName, @\\"^S(.?)test\\");
-      if (match.Success)
-      {
-        var deserializedValue = JsonSerializer.Deserialize<string>(ref reader, options);
-        instance.STestPatternProperties.Add(propertyName, deserializedValue);
-        continue;
-      }
+    
 
       if(instance.AdditionalProperties == null) { instance.AdditionalProperties = new Dictionary<string, dynamic>(); }
       var deserializedValue = JsonSerializer.Deserialize<dynamic>(ref reader, options);
@@ -106,48 +66,25 @@ internal class TestConverter : JsonConverter<Test>
   
     throw new JsonException();
   }
-  public override void Write(Utf8JsonWriter writer, Test value, JsonSerializerOptions options)
+  public override void Write(Utf8JsonWriter writer, NestedTest value, JsonSerializerOptions options)
   {
     if (value == null)
     {
       JsonSerializer.Serialize(writer, null);
       return;
     }
-    var properties = value.GetType().GetProperties().Where(prop => prop.Name != \\"AdditionalProperties\\" && prop.Name != \\"STestPatternProperties\\");
+    var properties = value.GetType().GetProperties().Where(prop => prop.Name != \\"AdditionalProperties\\");
   
     writer.WriteStartObject();
 
     if(value.StringProp != null) { 
       // write property name and let the serializer serialize the value itself
-      writer.WritePropertyName(\\"string prop\\");
+      writer.WritePropertyName(\\"stringProp\\");
       JsonSerializer.Serialize(writer, value.StringProp);
     }
-    if(value.NumberProp != null) { 
-      // write property name and let the serializer serialize the value itself
-      writer.WritePropertyName(\\"numberProp\\");
-      JsonSerializer.Serialize(writer, value.NumberProp);
-    }
-    if(value.ObjectProp != null) { 
-      // write property name and let the serializer serialize the value itself
-      writer.WritePropertyName(\\"objectProp\\");
-      JsonSerializer.Serialize(writer, value.ObjectProp);
-    }
 
 
-    // Unwrap pattern properties in object
-    if(value.STestPatternProperties != null) { 
-      foreach (var patternProp in value.STestPatternProperties)
-      {
-        //Ignore any pattern properties which might already be part of the core properties
-        if (properties.Any(prop => prop.Name == patternProp.Key))
-        {
-            continue;
-        }
-        // write property name and let the serializer serialize the value itself
-        writer.WritePropertyName(patternProp.Key);
-        JsonSerializer.Serialize(writer, patternProp.Value);
-      }
-    }
+  
 
     // Unwrap additional properties in object
     if (value.AdditionalProperties != null) {
@@ -172,10 +109,14 @@ internal class TestConverter : JsonConverter<Test>
 `;
 
 exports[`JSON serializer preset should render serialize and deserialize converters 2`] = `
-"[JsonConverter(typeof(NestedTestConverter))]
-public class NestedTest {
+"[JsonConverter(typeof(TestConverter))]
+public class Test {
   private string stringProp;
+  private float? numberProp;
+  private EnumTest enumProp;
+  private NestedTest objectProp;
   private Dictionary<string, dynamic> additionalProperties;
+  private Dictionary<string, string> sTestPatternProperties;
 
   public string StringProp 
   {
@@ -183,28 +124,52 @@ public class NestedTest {
     set { stringProp = value; }
   }
 
+  public float? NumberProp 
+  {
+    get { return numberProp; }
+    set { numberProp = value; }
+  }
+
+  public EnumTest EnumProp 
+  {
+    get { return enumProp; }
+    set { enumProp = value; }
+  }
+
+  public NestedTest ObjectProp 
+  {
+    get { return objectProp; }
+    set { objectProp = value; }
+  }
+
   public Dictionary<string, dynamic> AdditionalProperties 
   {
     get { return additionalProperties; }
     set { additionalProperties = value; }
   }
+
+  public Dictionary<string, string> STestPatternProperties 
+  {
+    get { return sTestPatternProperties; }
+    set { sTestPatternProperties = value; }
+  }
 }
 
-internal class NestedTestConverter : JsonConverter<NestedTest>
+internal class TestConverter : JsonConverter<Test>
 {
   public override bool CanConvert(Type objectType)
   {
     // this converter can be applied to any type
     return true;
   }
-  public override NestedTest Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
+  public override Test Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
   {
     if (reader.TokenType != JsonTokenType.StartObject)
     {
       throw new JsonException();
     }
 
-    var instance = new NestedTest();
+    var instance = new Test();
   
     while (reader.Read())
     {
@@ -220,14 +185,39 @@ internal class NestedTestConverter : JsonConverter<NestedTest>
       }
 
       string propertyName = reader.GetString();
-      if (propertyName == \\"stringProp\\")
+      if (propertyName == \\"string prop\\")
       {
         var value = JsonSerializer.Deserialize<string>(ref reader, options);
         instance.StringProp = value;
         continue;
       }
+      if (propertyName == \\"numberProp\\")
+      {
+        var value = JsonSerializer.Deserialize<float?>(ref reader, options);
+        instance.NumberProp = value;
+        continue;
+      }
+      if (propertyName == \\"enumProp\\")
+      {
+        var value = EnumTestExtension.ToEnumTest(JsonSerializer.Deserialize<dynamic>(ref reader, options));
+        instance.EnumProp = value;
+        continue;
+      }
+      if (propertyName == \\"objectProp\\")
+      {
+        var value = JsonSerializer.Deserialize<NestedTest>(ref reader, options);
+        instance.ObjectProp = value;
+        continue;
+      }
 
-    
+      if(instance.STestPatternProperties == null) { instance.STestPatternProperties = new Dictionary<string, string>(); }
+      var match = Regex.Match(propertyName, @\\"^S(.?)test\\");
+      if (match.Success)
+      {
+        var deserializedValue = JsonSerializer.Deserialize<string>(ref reader, options);
+        instance.STestPatternProperties.Add(propertyName, deserializedValue);
+        continue;
+      }
 
       if(instance.AdditionalProperties == null) { instance.AdditionalProperties = new Dictionary<string, dynamic>(); }
       var deserializedValue = JsonSerializer.Deserialize<dynamic>(ref reader, options);
@@ -237,25 +227,53 @@ internal class NestedTestConverter : JsonConverter<NestedTest>
   
     throw new JsonException();
   }
-  public override void Write(Utf8JsonWriter writer, NestedTest value, JsonSerializerOptions options)
+  public override void Write(Utf8JsonWriter writer, Test value, JsonSerializerOptions options)
   {
     if (value == null)
     {
       JsonSerializer.Serialize(writer, null);
       return;
     }
-    var properties = value.GetType().GetProperties().Where(prop => prop.Name != \\"AdditionalProperties\\");
+    var properties = value.GetType().GetProperties().Where(prop => prop.Name != \\"AdditionalProperties\\" && prop.Name != \\"STestPatternProperties\\");
   
     writer.WriteStartObject();
 
     if(value.StringProp != null) { 
       // write property name and let the serializer serialize the value itself
-      writer.WritePropertyName(\\"stringProp\\");
+      writer.WritePropertyName(\\"string prop\\");
       JsonSerializer.Serialize(writer, value.StringProp);
     }
+    if(value.NumberProp != null) { 
+      // write property name and let the serializer serialize the value itself
+      writer.WritePropertyName(\\"numberProp\\");
+      JsonSerializer.Serialize(writer, value.NumberProp);
+    }
+    if(value.EnumProp != null) { 
+      // write property name and let the serializer serialize the value itself
+      writer.WritePropertyName(\\"enumProp\\");
+      JsonSerializer.Serialize(writer, value.EnumProp.GetValue());
+    }
+    if(value.ObjectProp != null) { 
+      // write property name and let the serializer serialize the value itself
+      writer.WritePropertyName(\\"objectProp\\");
+      JsonSerializer.Serialize(writer, value.ObjectProp);
+    }
 
 
-  
+    // Unwrap pattern properties in object
+    if(value.STestPatternProperties != null) { 
+      foreach (var patternProp in value.STestPatternProperties)
+      {
+        //Ignore any pattern properties which might already be part of the core properties
+        if (properties.Any(prop => prop.Name == patternProp.Key))
+        {
+            continue;
+        }
+        // write property name and let the serializer serialize the value itself
+        writer.WritePropertyName(patternProp.Key);
+        JsonSerializer.Serialize(writer, patternProp.Value);
+      }
+    }
 
     // Unwrap additional properties in object
     if (value.AdditionalProperties != null) {
@@ -278,3 +296,36 @@ internal class NestedTestConverter : JsonConverter<NestedTest>
 }
 "
 `;
+
+exports[`JSON serializer preset should render serialize and deserialize converters 3`] = `
+"public enum EnumTest {
+  SomeEnumString, True, TestTest, Number_2
+}
+public static class EnumTestExtensions {
+  public static dynamic GetValue(this EnumTest enumValue)
+  {
+    switch (enumValue)
+    {
+      case EnumTest.SomeEnumString: return \\"Some enum String\\";
+      case EnumTest.True: return true;
+      case EnumTest.TestTest: return \\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\";
+      case EnumTest.Number_2: return 2;
+    }
+    return null;
+  }
+
+  public static EnumTest? ToEnumTest(dynamic value)
+  {
+    switch (value)
+    {
+      case \\"Some enum String\\": return EnumTest.SomeEnumString;
+      case true: return EnumTest.True;
+      case \\"{\\\\\\"test\\\\\\":\\\\\\"test\\\\\\"}\\": return EnumTest.TestTest;
+      case 2: return EnumTest.Number_2;
+    }
+    return null;
+  }
+}
+
+"
+`;