Skip to content

Commit

Permalink
Fix toolchain version output (#1461)
Browse files Browse the repository at this point in the history
  • Loading branch information
RicoSuter committed Dec 10, 2021
1 parent e6af8bc commit 76afab6
Show file tree
Hide file tree
Showing 8 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,29 @@ public async Task When_type_is_array_and_items_and_item_is_not_defined_then_any_
AssertCompile(output);
}

[Fact]
public async Task When_code_is_generated_then_toolchain_version_is_printed()
{
//// Arrange
var json = @"{
'required': [ 'emptySchema' ],
'properties': {
'emptySchema': { 'type': 'array' }
}
}";
var schema = await JsonSchema.FromJsonAsync(json);

//// Act
var settings = new CSharpGeneratorSettings { ClassStyle = CSharpClassStyle.Poco, Namespace = "ns", };
var generator = new CSharpGenerator(schema, settings);
var output = generator.GenerateFile("MyClass");

//// Assert
Assert.Contains(" (Newtonsoft.Json ", output);

AssertCompile(output);
}

class CustomPropertyNameGenerator : IPropertyNameGenerator
{
public string Generate(JsonSchemaProperty property)
Expand Down
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,13 @@ public override IEnumerable<CodeArtifact> GenerateTypes()
{
if (Settings.ExcludedTypeNames?.Contains("JsonInheritanceAttribute") != true)
{
var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceAttribute", new TemplateModelBase());
var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceAttribute", new object());
artifacts.Add(new CodeArtifact("JsonInheritanceAttribute", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template));
}

if (Settings.ExcludedTypeNames?.Contains("JsonInheritanceConverter") != true)
{
var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceConverter", new TemplateModelBase());
var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceConverter", new object());
artifacts.Add(new CodeArtifact("JsonInheritanceConverter", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace NJsonSchema.CodeGeneration.CSharp.Models
// TODO: Add base class for CSharp.EnumTemplateModel and TypeScript.EnumTemplateModel

/// <summary>The CSharp enum template model.</summary>
public class EnumTemplateModel : TemplateModelBase
public class EnumTemplateModel
{
private readonly JsonSchema _schema;
private readonly CSharpGeneratorSettings _settings;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{%- if IsDeprecated -%}
[System.Obsolete{% if HasDeprecatedMessage %}({{ DeprecatedMessage | literal }}){% endif %}]
{% endif -%}
{%- template Class.Annotations %}
{%- template Class.Annotations -%}
{{ TypeAccessModifier }} {% if IsAbstract %}abstract {% endif %}partial {% if GenerateNativeRecords %}record{% else %}class{% endif %} {{ClassName}} {%- template Class.Inheritance %}
{
{%- if IsTuple -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ public IEnumerable<CodeArtifact> GenerateTypes(TypeScriptExtensionCode extension

if (artifacts.Any(r => r.Code.Contains("formatDate(")))
{
var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "File.FormatDate", new TemplateModelBase());
var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "File.FormatDate", new object());
yield return new CodeArtifact("formatDate", CodeArtifactType.Function, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template);
}

if (Settings.HandleReferences)
{
var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "File.ReferenceHandling", new TemplateModelBase());
var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "File.ReferenceHandling", new object());
yield return new CodeArtifact("jsonParse", CodeArtifactType.Function, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/NJsonSchema.CodeGeneration/DefaultTemplateFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ public string Render()
{
templateContext = new TemplateContext(_model, _templateOptions);
templateContext.AmbientValues.Add(LiquidParser.SettingsKey, _settings);
templateContext.AmbientValues.Add("ToolchainVersion", _toolchainVersion);
templateContext.SetValue("ToolchainVersion", _toolchainVersion);
}

templateContext.AmbientValues[LiquidParser.LanguageKey] = _language;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace NJsonSchema.CodeGeneration.Models
{
/// <summary>The class template base class.</summary>
public abstract class ClassTemplateModelBase : TemplateModelBase
public abstract class ClassTemplateModelBase
{
private readonly JsonSchema _schema;
private readonly object _rootObject;
Expand Down
17 changes: 0 additions & 17 deletions src/NJsonSchema.CodeGeneration/Models/TemplateModelBase.cs

This file was deleted.

0 comments on commit 76afab6

Please sign in to comment.