Repository: asyncapi/modelina
Pull request #327 (2021-08-13 14:07:49)

feat: example function for TS class renderer
**Description**
This PR introduces a new TS example renderer, which renders an example object creation with the associated class renderer. This is especially useful when you want to quickly generate an example instance, for example in auto-generated tests.

**Related issue(s)**
Partly solves https://github.com/asyncapi/modelina/issues/128

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


jonaslagoni: My honest opinion here is that we are stretching the use-case of the library a bit, and it should probably not be done... Thoughts @magicmatatjahu ? 
coveralls: ## Pull Request Test Coverage Report for [Build 1127920785](https://coveralls.io/builds/42132336)

* **37** of **43**   **(86.05%)**  changed or added relevant lines in **3** files are covered.
* No unchanged relevant lines lost coverage.
* Overall coverage decreased (**-0.3%**) to **92.998%**

---

|  Changes Missing Coverage | Covered Lines | Changed/Added Lines | % |
| :-----|--------------|--------|---: |
| [src/generators/typescript/renderers/ClassExampleRenderer.ts](https://coveralls.io/builds/42132336/source?filename=src%2Fgenerators%2Ftypescript%2Frenderers%2FClassExampleRenderer.ts#L28) | 31 | 37 | 83.78%
<!-- | **Total:** | **37** | **43** | **86.05%** | -->


|  Totals | [![Coverage Status](https://coveralls.io/builds/42132336/badge)](https://coveralls.io/builds/42132336) |
| :-- | --: |
| Change from base [Build 1124309475](https://coveralls.io/builds/42100985): |  -0.3% |
| Covered Lines: | 1749 |
| Relevant Lines: | 1789 |

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



Patch:
diff --git a/src/generators/typescript/presets/CommonPreset.ts b/src/generators/typescript/presets/CommonPreset.ts
index ea57e244..1b29fed1 100644
--- a/src/generators/typescript/presets/CommonPreset.ts
+++ b/src/generators/typescript/presets/CommonPreset.ts
@@ -2,9 +2,11 @@ import { TypeScriptRenderer } from '../TypeScriptRenderer';
 import { TypeScriptPreset } from '../TypeScriptPreset';
 import { getUniquePropertyName, DefaultPropertyNames } from '../../../helpers';
 import { CommonModel } from '../../../models';
+import renderExampleFunction from './utils/ExampleFunction';
 
 export interface TypeScriptCommonPresetOptions {
   marshalling: boolean;
+  example: boolean;
 }
 
 function realizePropertyFactory(prop: string) {
@@ -157,7 +159,7 @@ ${renderer.indent(unmarshalAdditionalProperties, 4)}
 } 
 
 /**
- * Preset which adds `marshal`, `unmarshal` functions to class. 
+ * Preset which adds `marshal`, `unmarshal`, `example` functions to class. 
  * 
  * @implements {TypeScriptPreset}
  */
@@ -171,6 +173,10 @@ export const TS_COMMON_PRESET: TypeScriptPreset = {
         blocks.push(renderMarshal({ renderer, model }));
         blocks.push(renderUnmarshal({ renderer, model }));
       }
+
+      if (options.example === true) {
+        blocks.push(renderExampleFunction({ renderer, model }));
+      }
       
       return renderer.renderBlock([content, ...blocks], 2);
     },
diff --git a/src/generators/typescript/presets/utils/ExampleFunction.ts b/src/generators/typescript/presets/utils/ExampleFunction.ts
new file mode 100644
index 00000000..0cf6d0f2
--- /dev/null
+++ b/src/generators/typescript/presets/utils/ExampleFunction.ts
@@ -0,0 +1,79 @@
+import { TypeScriptRenderer } from '../../TypeScriptRenderer';
+import { CommonModel } from '../../../../models';
+
+/**
+ * Inferring first acceptable value from the model.
+ * 
+ * @param model 
+ * @param renderer 
+ */
+export function renderValueFromModel(model: CommonModel, renderer: TypeScriptRenderer): string | undefined {
+  if (Array.isArray(model.enum) && model.enum.length > 0) {
+    return JSON.stringify(model.enum[0]);
+  }
+  if (model.$ref !== undefined) {
+    return `${renderer.nameType(model.$ref)}.example()`;
+  }
+  if (Array.isArray(model.type)) {
+    if (model.type.length > 0) {
+      return renderValueFromType(model.type[0], model, renderer);
+    } 
+    return undefined;
+  }
+  return renderValueFromType(model.type, model, renderer);
+}
+
+export function renderValueFromType(modelType: string | undefined, model: CommonModel, renderer: TypeScriptRenderer): string | undefined {
+  if (modelType === undefined) {
+    return undefined;
+  }
+  switch (modelType) {
+  case 'string':
+    return '"string"';
+  case 'integer':
+  case 'number':
+    return '0';
+  case 'boolean':
+    return 'true';
+  case 'array': {
+    if (model.items === undefined) {
+      return '[]';
+    }
+    //Check and see if it should be rendered as tuples 
+    if (Array.isArray(model.items)) {
+      const arrayValues = model.items.map((item) => {
+        return renderValueFromModel(item, renderer);
+      });
+      return `[${arrayValues.join(', ')}]`;
+    }
+    const arrayType = renderValueFromModel(model.items, renderer);
+    return `[${arrayType}]`;
+  }
+  }
+  return undefined;
+}
+/**
+ * Render `example` function based on model properties.
+ */
+export default function renderExampleFunction({ renderer, model }: {
+  renderer: TypeScriptRenderer,
+  model: CommonModel,
+}): string {
+  const properties = model.properties || {};
+  const setProperties = [];
+  for (const [propertyName, property] of Object.entries(properties)) {
+    const formattedPropertyName = renderer.nameProperty(propertyName, property);
+    const potentialRenderedValue = renderValueFromModel(property, renderer);
+    if (potentialRenderedValue === undefined) {
+      //Unable to determine example value, skip property.
+      continue;
+    }
+    setProperties.push(`  instance.${formattedPropertyName} = ${potentialRenderedValue};`);
+  }
+  const formattedModelName = renderer.nameType(model.$id);
+  return `public static example(): ${formattedModelName} {
+  const instance = new ${formattedModelName}({} as any);
+${(setProperties.join('\n'))}
+  return instance;
+}`;
+}


Test patch:
diff --git a/test/blackbox/Dummy.spec.ts b/test/blackbox/Dummy.spec.ts
index 8504ed81..96dd5fe8 100644
--- a/test/blackbox/Dummy.spec.ts
+++ b/test/blackbox/Dummy.spec.ts
@@ -1,5 +1,5 @@
 import * as path from 'path';
-import { TypeScriptGenerator, JavaGenerator, JavaScriptGenerator, GoGenerator, CSharpGenerator } from '../../src';
+import { TypeScriptGenerator, JavaGenerator, JavaScriptGenerator, GoGenerator, CSharpGenerator, TS_COMMON_PRESET } from '../../src';
 import { execCommand, generateModels, renderModels, renderModelsToSeparateFiles } from './utils/Utils';
 const fileToGenerate = path.resolve(__dirname, './docs/dummy.json');
 describe('Dummy JSON Schema file', () => {
diff --git a/test/generators/typescript/preset/ExamplePreset.spec.ts b/test/generators/typescript/preset/ExamplePreset.spec.ts
new file mode 100644
index 00000000..c38b8c02
--- /dev/null
+++ b/test/generators/typescript/preset/ExamplePreset.spec.ts
@@ -0,0 +1,40 @@
+import { TypeScriptGenerator, TS_COMMON_PRESET } from '../../../../src/generators';
+const doc = {
+  $id: 'Test',
+  type: 'object',
+  additionalProperties: true,
+  required: ['string prop'],
+  properties: {
+    'string prop': { type: 'string' },
+    numberProp: { type: 'number' },
+    objectProp: { type: 'object', $id: 'NestedTest', properties: {stringProp: { type: 'string' }}}
+  },
+  patternProperties: {
+    '^S(.?)test': {
+      type: 'string'
+    }
+  },
+};
+describe('Example function generation', () => {
+  test('should render example function for model', async () => {
+    const generator = new TypeScriptGenerator({ 
+      presets: [
+        {
+          preset: TS_COMMON_PRESET,
+          options: {
+            example: true
+          }
+        }
+      ]
+    });
+    const inputModel = await generator.process(doc);
+    const testModel = inputModel.models['Test'];
+    const nestedTestModel = inputModel.models['NestedTest'];
+
+    const testClass = await generator.renderClass(testModel, inputModel);
+    const nestedTestClass = await generator.renderClass(nestedTestModel, inputModel);
+
+    expect(testClass.result).toMatchSnapshot(); 
+    expect(nestedTestClass.result).toMatchSnapshot();
+  });
+});
diff --git a/test/generators/typescript/preset/__snapshots__/ExamplePreset.spec.ts.snap b/test/generators/typescript/preset/__snapshots__/ExamplePreset.spec.ts.snap
new file mode 100644
index 00000000..0a3eb083
--- /dev/null
+++ b/test/generators/typescript/preset/__snapshots__/ExamplePreset.spec.ts.snap
@@ -0,0 +1,69 @@
+// Jest Snapshot v1, https://goo.gl/fbAQLP
+
+exports[`Example function generation should render example function for model 1`] = `
+"export class Test {
+  private _stringProp: string;
+  private _numberProp?: number;
+  private _objectProp?: NestedTest;
+  private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null | number>;
+  private _sTestPatternProperties?: Map<String, string>;
+
+  constructor(input: {
+    stringProp: string,
+    numberProp?: number,
+    objectProp?: NestedTest,
+  }) {
+    this._stringProp = input.stringProp;
+    this._numberProp = input.numberProp;
+    this._objectProp = input.objectProp;
+  }
+
+  get stringProp(): string { return this._stringProp; }
+  set stringProp(stringProp: string) { this._stringProp = stringProp; }
+
+  get numberProp(): number | undefined { return this._numberProp; }
+  set numberProp(numberProp: number | undefined) { this._numberProp = numberProp; }
+
+  get objectProp(): NestedTest | undefined { return this._objectProp; }
+  set objectProp(objectProp: NestedTest | undefined) { this._objectProp = objectProp; }
+
+  get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined { return this._additionalProperties; }
+  set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined) { this._additionalProperties = additionalProperties; }
+
+  get sTestPatternProperties(): Map<String, string> | undefined { return this._sTestPatternProperties; }
+  set sTestPatternProperties(sTestPatternProperties: Map<String, string> | undefined) { this._sTestPatternProperties = sTestPatternProperties; }
+
+  public static example(): Test {
+    const instance = new Test({} as any);
+    instance.stringProp = \\"string\\";
+    instance.numberProp = 0;
+    instance.objectProp = NestedTest.example();
+    return instance;
+  }
+}"
+`;
+
+exports[`Example function generation should render example function for model 2`] = `
+"export class NestedTest {
+  private _stringProp?: string;
+  private _additionalProperties?: Map<String, object | string | number | Array<unknown> | boolean | null | number>;
+
+  constructor(input: {
+    stringProp?: string,
+  }) {
+    this._stringProp = input.stringProp;
+  }
+
+  get stringProp(): string | undefined { return this._stringProp; }
+  set stringProp(stringProp: string | undefined) { this._stringProp = stringProp; }
+
+  get additionalProperties(): Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined { return this._additionalProperties; }
+  set additionalProperties(additionalProperties: Map<String, object | string | number | Array<unknown> | boolean | null | number> | undefined) { this._additionalProperties = additionalProperties; }
+
+  public static example(): NestedTest {
+    const instance = new NestedTest({} as any);
+    instance.stringProp = \\"string\\";
+    return instance;
+  }
+}"
+`;
diff --git a/test/generators/typescript/preset/utils/ExampleFunctions.spec.ts b/test/generators/typescript/preset/utils/ExampleFunctions.spec.ts
new file mode 100644
index 00000000..3c74cb75
--- /dev/null
+++ b/test/generators/typescript/preset/utils/ExampleFunctions.spec.ts
@@ -0,0 +1,83 @@
+import { TypeScriptGenerator } from '../../../../../src/generators';
+import { renderValueFromModel } from '../../../../../src/generators/typescript/presets/utils/ExampleFunction';
+import { TypeScriptRenderer } from '../../../../../src/generators/typescript/TypeScriptRenderer';
+import { CommonInputModel, CommonModel } from '../../../../../src/models';
+class MockTypeScriptRenderer extends TypeScriptRenderer {
+
+}
+const renderer = new MockTypeScriptRenderer(TypeScriptGenerator.defaultOptions, new TypeScriptGenerator(), [], new CommonModel(), new CommonInputModel());
+describe('Marshalling preset', () => {
+  describe('.renderValueFromModel()', () => {
+    describe('enums', () => {
+      test('Should render strings correctly', () => {
+        const input = CommonModel.toCommonModel({enum: ['somevalue']});
+        const output = renderValueFromModel(input, renderer);
+        expect(output).toEqual('"somevalue"');
+      });
+      test('Should render numbers correctly', () => {
+        const input = CommonModel.toCommonModel({enum: [1]});
+        const output = renderValueFromModel(input, renderer);
+        expect(output).toEqual('1');
+      });
+      test('Should ignore multiple values', () => {
+        const input = CommonModel.toCommonModel({enum: [1, 'somevalue']});
+        const output = renderValueFromModel(input, renderer);
+        expect(output).toEqual('1');
+      });
+      test('Should use first value if there is more then one', () => {
+        const input = CommonModel.toCommonModel({enum: []});
+        const output = renderValueFromModel(input, renderer);
+        expect(output).toBeUndefined();
+      });
+    });
+    test('should render refs correctly', () => {
+      const input = CommonModel.toCommonModel({$ref: 'SomeOtherModel'});
+      const output = renderValueFromModel(input, renderer);
+      expect(output).toEqual('SomeOtherModel.example()');
+    });
+    describe('types', () => {
+      test('Should render strings correctly', () => {
+        const input = CommonModel.toCommonModel({type: 'string'});
+        const output = renderValueFromModel(input, renderer);
+        expect(output).toEqual('"string"');
+      });
+      test('Should render numbers correctly', () => {
+        const input = CommonModel.toCommonModel({type: 'number'});
+        const output = renderValueFromModel(input, renderer);
+        expect(output).toEqual('0');
+      });
+      test('Should render booleans correctly', () => {
+        const input = CommonModel.toCommonModel({type: 'boolean'});
+        const output = renderValueFromModel(input, renderer);
+        expect(output).toEqual('true');
+      });
+      test('Should use first value if there is more then one', () => {
+        const input = CommonModel.toCommonModel({type: ['boolean', 'string']});
+        const output = renderValueFromModel(input, renderer);
+        expect(output).toEqual('true');
+      });
+      describe('array', () => {
+        test('should not render anything if no items are defined', () => {
+          const input = CommonModel.toCommonModel({type: 'array'});
+          const output = renderValueFromModel(input, renderer);
+          expect(output).toEqual('[]');
+        });
+        test('should render multiple array values', () => {
+          const input = CommonModel.toCommonModel({type: 'array', items: [{type: 'string'}, {type: 'number'}]});
+          const output = renderValueFromModel(input, renderer);
+          expect(output).toEqual('["string", 0]');
+        });
+        test('should render single array value', () => {
+          const input = CommonModel.toCommonModel({type: 'array', items: {type: 'string'}});
+          const output = renderValueFromModel(input, renderer);
+          expect(output).toEqual('["string"]');
+        });
+      });
+      test('Should ignore if none are present', () => {
+        const input = CommonModel.toCommonModel({type: []});
+        const output = renderValueFromModel(input, renderer);
+        expect(output).toBeUndefined();
+      });
+    });
+  });
+});