Skip to content

Commit

Permalink
Add GenerateTypeCheckFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Jun 2, 2022
1 parent d3f648e commit 005219a
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,28 @@ public async Task When_class_with_discriminator_has_base_class_then_csharp_is_ge
Assert.Contains("if (data[\"kind\"] === \"MyException\") {", code);
}

[Fact]
public async Task When_interfaces_are_generated_with_inheritance_then_type_check_methods_are_generated()
{
//// Arrange
var schema = JsonSchema.FromType<ExceptionContainer>();
var data = schema.ToJson();

var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
TypeScriptVersion = 2.0m,
TypeStyle = TypeScriptTypeStyle.Interface,
GenerateTypeCheckFunctions = true
});

//// Act
var code = generator.GenerateFile();

//// Assert
Assert.Contains("export function isExceptionBase(object: any): object is ExceptionBase {", code);
Assert.Contains("function isMyException(object: any): object is MyException {", code);
}

[Fact]
public async Task When_discriminator_does_not_match_typename_then_TypeScript_is_correct()
{
Expand Down Expand Up @@ -322,7 +344,8 @@ public async Task When_schema_with_inheritance_and_references_is_generated_then_


[Fact]
public async Task When_class_has_baseclass_and_extension_code_baseclass_is_preserved() {
public async Task When_class_has_baseclass_and_extension_code_baseclass_is_preserved()
{
//// Arrange
string extensionCode = @"
import * as generated from ""./generated"";
Expand All @@ -333,7 +356,8 @@ export class ExceptionBase extends generated.ExceptionBase {
";

var schema = JsonSchema.FromType<ExceptionContainer>();
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings {
var generator = new TypeScriptGenerator(schema, new TypeScriptGeneratorSettings
{
ExtensionCode = extensionCode,
ExtendedClasses = new[] { "ExceptionBase" },
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,9 @@ public string IndexerPropertyValueType
/// <summary>Gets a value indicating whether the export keyword should be added to all classes.</summary>
public bool ExportTypes => _settings.ExportTypes;

/// <summary>Gets a value indicating whether to generate type check functions.</summary>
public bool GenerateTypeCheckFunctions => _settings.GenerateTypeCheckFunctions;

/// <summary>Gets the inherited schema.</summary>
private JsonSchema InheritedSchema => _schema.InheritedSchema?.ActualSchema;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,10 @@

[key: string]: {{ IndexerPropertyValueType }};
{%- endif -%}
}
}
{%- if GenerateTypeCheckFunctions and HasInheritance -%}

{% if ExportTypes %}export {% endif %}function is{{ ClassName }}(object: any): object is {{ ClassName }} {
return object && object['{{ BaseDiscriminator }}'] === '{{ DiscriminatorName }}';
}
{%- endif -%}
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public TypeScriptGeneratorSettings()
ExtendedClasses = Array.Empty<string>();

InlineNamedDictionaries = false;
GenerateTypeCheckFunctions = false;
}

/// <summary>Gets or sets the target TypeScript version (default: 2.7).</summary>
Expand Down Expand Up @@ -108,6 +109,9 @@ public TypeScriptGeneratorSettings()
/// <summary>Gets or sets a value indicating whether named/referenced dictionaries should be inlined or generated as class with an indexer.</summary>
public bool InlineNamedDictionaries { get; set; }

/// <summary>Gets a value indicating whether to generate type check functions (for type style interface only, default: false).</summary>
public bool GenerateTypeCheckFunctions { get; set; }

internal ITemplate CreateTemplate(string typeName, object model)
{
if (ClassTypes != null && ClassTypes.Contains(typeName))
Expand Down

0 comments on commit 005219a

Please sign in to comment.