Skip to content

Commit

Permalink
Merge pull request #967 from RicoSuter/master
Browse files Browse the repository at this point in the history
Release v9.14.0
  • Loading branch information
RicoSuter authored May 16, 2019
2 parents e442b6e + 7a02d5f commit 3003a15
Show file tree
Hide file tree
Showing 14 changed files with 137 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1724,8 +1724,8 @@ public void When_schema_has_negative_value_of_enum_it_is_generated_in_CSharp_and
var types = generator.GenerateTypes(schema, "MyEnum");

//// Assert
Assert.Contains("_1 = 1", types.Artifacts.First().Code);
Assert.Contains("__1 = -1", types.Artifacts.First().Code);
Assert.Contains("_1 = 1", types.First().Code);
Assert.Contains("__1 = -1", types.First().Code);
}

private static void AssertCompile(string code)
Expand Down
26 changes: 13 additions & 13 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public CSharpGenerator(object rootObject, CSharpGeneratorSettings settings)
/// <param name="rootObject">The root object to search for all JSON Schemas.</param>
/// <param name="settings">The generator settings.</param>
/// <param name="resolver">The resolver.</param>
public CSharpGenerator(object rootObject, CSharpGeneratorSettings settings, CSharpTypeResolver resolver)
public CSharpGenerator(object rootObject, CSharpGeneratorSettings settings, CSharpTypeResolver resolver)
: base(rootObject, resolver, settings)
{
_resolver = resolver;
Expand All @@ -48,40 +48,40 @@ public CSharpGenerator(object rootObject, CSharpGeneratorSettings settings, CSha
public CSharpGeneratorSettings Settings { get; }

/// <inheritdoc />
public override CodeArtifactCollection GenerateTypes()
public override IEnumerable<CodeArtifact> GenerateTypes()
{
var collection = base.GenerateTypes();
var results = new List<CodeArtifact>();
var baseArtifacts = base.GenerateTypes();
var artifacts = new List<CodeArtifact>();

if (collection.Artifacts.Any(r => r.Code.Contains("JsonInheritanceConverter")))
if (baseArtifacts.Any(r => r.Code.Contains("JsonInheritanceConverter")))
{
if (Settings.ExcludedTypeNames?.Contains("JsonInheritanceAttribute") != true)
{
var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceAttribute", new TemplateModelBase());
results.Add(new CodeArtifact("JsonInheritanceAttribute", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, template));
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());
results.Add(new CodeArtifact("JsonInheritanceConverter", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, template));
artifacts.Add(new CodeArtifact("JsonInheritanceConverter", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template));
}
}

if (collection.Artifacts.Any(r => r.Code.Contains("DateFormatConverter")))
if (baseArtifacts.Any(r => r.Code.Contains("DateFormatConverter")))
{
if (Settings.ExcludedTypeNames?.Contains("DateFormatConverter") != true)
{
var template = Settings.TemplateFactory.CreateTemplate("CSharp", "DateFormatConverter", new TemplateModelBase());
results.Add(new CodeArtifact("DateFormatConverter", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, template));
artifacts.Add(new CodeArtifact("DateFormatConverter", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template));
}
}

return new CodeArtifactCollection(collection.Artifacts.Concat(results), collection.ExtensionCode);
return baseArtifacts.Concat(artifacts);
}

/// <inheritdoc />
protected override string GenerateFile(CodeArtifactCollection artifactCollection)
protected override string GenerateFile(IEnumerable<CodeArtifact> artifactCollection)
{
var model = new FileTemplateModel
{
Expand Down Expand Up @@ -114,7 +114,7 @@ private CodeArtifact GenerateClass(JsonSchema4 schema, string typeName)
RenamePropertyWithSameNameAsClass(typeName, model.Properties);

var template = Settings.TemplateFactory.CreateTemplate("CSharp", "Class", model);
return new CodeArtifact(typeName, model.BaseClassName, CodeArtifactType.Class, CodeArtifactLanguage.CSharp, template);
return new CodeArtifact(typeName, model.BaseClassName, CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Dto, template);
}

private void RenamePropertyWithSameNameAsClass(string typeName, IEnumerable<PropertyModel> properties)
Expand All @@ -134,7 +134,7 @@ private CodeArtifact GenerateEnum(JsonSchema4 schema, string typeName)
{
var model = new EnumTemplateModel(typeName, schema, Settings);
var template = Settings.TemplateFactory.CreateTemplate("CSharp", "Enum", model);
return new CodeArtifact(typeName, CodeArtifactType.Enum, CodeArtifactLanguage.CSharp, template);
return new CodeArtifact(typeName, CodeArtifactType.Enum, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Dto, template);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.37</Version>
<Version>9.14.0</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ public void When_class_order_is_wrong_then_classes_are_correctly_reordered()
//// Arrange
var classes = new List<CodeArtifact>
{
new CodeArtifact("Car", CodeArtifactType.Class, CodeArtifactLanguage.CSharp),
new CodeArtifact("Apple", "List<Fruit>", CodeArtifactType.Class, CodeArtifactLanguage.CSharp),
new CodeArtifact("Professor", "Teacher", CodeArtifactType.Class, CodeArtifactLanguage.CSharp),
new CodeArtifact("Teacher", "Person[]", CodeArtifactType.Class, CodeArtifactLanguage.CSharp),
new CodeArtifact("Fruit", CodeArtifactType.Class, CodeArtifactLanguage.CSharp),
new CodeArtifact("Person", CodeArtifactType.Class, CodeArtifactLanguage.CSharp)
new CodeArtifact("Car", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Undefined, ""),
new CodeArtifact("Apple", "List<Fruit>", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Undefined, ""),
new CodeArtifact("Professor", "Teacher", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Undefined, ""),
new CodeArtifact("Teacher", "Person[]", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Undefined, ""),
new CodeArtifact("Fruit", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Undefined, ""),
new CodeArtifact("Person", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Undefined, "")
};

//// Act
classes = CodeArtifactCollection.OrderByBaseDependency(classes).ToList();
classes = classes.OrderByBaseDependency().ToList();
var order = string.Join(", ", classes.Select(c => c.TypeName));

//// Assert
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;netstandard2.0;net451</TargetFrameworks>
<Description>JSON Schema reader, generator and validator for .NET</Description>
<Version>9.13.37</Version>
<Version>9.14.0</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2018</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand Down
45 changes: 27 additions & 18 deletions src/NJsonSchema.CodeGeneration.TypeScript/TypeScriptGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@
using NJsonSchema.CodeGeneration.TypeScript.Models;
using System.Linq;
using NJsonSchema.CodeGeneration.Models;
using System.Collections.Generic;

namespace NJsonSchema.CodeGeneration.TypeScript
{
/// <summary>The TypeScript interface and enum code generator. </summary>
public class TypeScriptGenerator : GeneratorBase
{
private readonly TypeScriptTypeResolver _resolver;
private TypeScriptExtensionCode _extensionCode;

/// <summary>Initializes a new instance of the <see cref="TypeScriptGenerator"/> class.</summary>
/// <param name="schema">The schema.</param>
Expand Down Expand Up @@ -49,60 +51,67 @@ public TypeScriptGenerator(object rootObject, TypeScriptGeneratorSettings settin

/// <summary>Generates all types from the resolver with extension code from the settings.</summary>
/// <returns>The code.</returns>
public override CodeArtifactCollection GenerateTypes()
public override IEnumerable<CodeArtifact> GenerateTypes()
{
return GenerateTypes(new TypeScriptExtensionCode(Settings.ExtensionCode, Settings.ExtendedClasses));
_extensionCode = _extensionCode ??
new TypeScriptExtensionCode(Settings.ExtensionCode, Settings.ExtendedClasses);

return GenerateTypes(_extensionCode);
}

/// <summary>Generates all types from the resolver with the given extension code.</summary>
/// <returns>The code.</returns>
public CodeArtifactCollection GenerateTypes(ExtensionCode extensionCode)
public IEnumerable<CodeArtifact> GenerateTypes(TypeScriptExtensionCode extensionCode)
{
var collection = base.GenerateTypes();
var artifacts = collection.Artifacts.ToList();

foreach (var artifact in collection.Artifacts)
var artifacts = base.GenerateTypes();
foreach (var artifact in artifacts)
{
if (extensionCode?.ExtensionClasses.ContainsKey(artifact.TypeName) == true)
{
var classCode = artifact.Code;

var index = classCode.IndexOf("constructor(", StringComparison.Ordinal);
if (index != -1)
artifact.Code = classCode.Insert(index, extensionCode.GetExtensionClassBody(artifact.TypeName).Trim() + "\n\n ");
{
var code = classCode.Insert(index, extensionCode.GetExtensionClassBody(artifact.TypeName).Trim() + "\n\n ");
yield return new CodeArtifact(artifact.TypeName, artifact.Type, artifact.Language, artifact.Category, code);
}
else
{
index = classCode.IndexOf("class", StringComparison.Ordinal);
index = classCode.IndexOf("{", index, StringComparison.Ordinal) + 1;

artifact.Code = classCode.Insert(index, "\n " + extensionCode.GetExtensionClassBody(artifact.TypeName).Trim() + "\n");
var code = classCode.Insert(index, "\n " + extensionCode.GetExtensionClassBody(artifact.TypeName).Trim() + "\n");
yield return new CodeArtifact(artifact.TypeName, artifact.Type, artifact.Language, artifact.Category, code);
}
}
else
{
yield return artifact;
}
}

if (artifacts.Any(r => r.Code.Contains("formatDate(")))
{
var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "File.FormatDate", new TemplateModelBase());
artifacts.Add(new CodeArtifact("formatDate", CodeArtifactType.Function, CodeArtifactLanguage.CSharp, template));
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());
artifacts.Add(new CodeArtifact("jsonParse", CodeArtifactType.Function, CodeArtifactLanguage.CSharp, template));
yield return new CodeArtifact("jsonParse", CodeArtifactType.Function, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Utility, template);
}

return new CodeArtifactCollection(artifacts, extensionCode);
}

/// <summary>Generates the file.</summary>
/// <returns>The file contents.</returns>
protected override string GenerateFile(CodeArtifactCollection artifactCollection)
protected override string GenerateFile(IEnumerable<CodeArtifact> artifacts)
{
var model = new FileTemplateModel(Settings)
{
Types = artifactCollection.Concatenate(),
ExtensionCode = (TypeScriptExtensionCode)artifactCollection.ExtensionCode
Types = artifacts.Concatenate(),
ExtensionCode = _extensionCode
};

var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "File", model);
Expand All @@ -121,7 +130,7 @@ protected override CodeArtifact GenerateType(JsonSchema4 schema, string typeName
{
var model = new EnumTemplateModel(typeName, schema, Settings);
var template = Settings.TemplateFactory.CreateTemplate("TypeScript", "Enum", model);
return new CodeArtifact(typeName, CodeArtifactType.Enum, CodeArtifactLanguage.TypeScript, template);
return new CodeArtifact(typeName, CodeArtifactType.Enum, CodeArtifactLanguage.TypeScript, CodeArtifactCategory.Dto, template);
}
else
{
Expand All @@ -132,7 +141,7 @@ protected override CodeArtifact GenerateType(JsonSchema4 schema, string typeName
? CodeArtifactType.Interface
: CodeArtifactType.Class;

return new CodeArtifact(typeName, model.BaseClass, type, CodeArtifactLanguage.TypeScript, template);
return new CodeArtifact(typeName, model.BaseClass, type, CodeArtifactLanguage.TypeScript, CodeArtifactCategory.Dto, template);
}
}
}
Expand Down
36 changes: 22 additions & 14 deletions src/NJsonSchema.CodeGeneration/CodeArtifact.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,33 @@ public class CodeArtifact
/// <param name="typeName">The type name.</param>
/// <param name="type">The artifact type.</param>
/// <param name="language">The artifact language.</param>
public CodeArtifact(string typeName, CodeArtifactType type, CodeArtifactLanguage language)
: this(typeName, null, type, language, null)
/// <param name="category">The category.</param>
/// <param name="code">The code.</param>
public CodeArtifact(string typeName, CodeArtifactType type, CodeArtifactLanguage language, CodeArtifactCategory category, string code)
: this(typeName, null, type, language, category, code)
{
}

/// <summary>Initializes a new instance of the <see cref="CodeArtifact"/> class.</summary>
/// <param name="typeName">The type name.</param>
/// <param name="baseTypeName">The base type name (e.g. base class).</param>
/// <param name="type">The artifact type.</param>
/// <param name="language">The artifact language.</param>
public CodeArtifact(string typeName, string baseTypeName, CodeArtifactType type, CodeArtifactLanguage language)
: this(typeName, baseTypeName, type, language, null)
/// <param name="category">The category.</param>
/// <param name="template">The template to render the code.</param>
public CodeArtifact(string typeName, CodeArtifactType type, CodeArtifactLanguage language, CodeArtifactCategory category, ITemplate template)
: this(typeName, null, type, language, category, template?.Render())
{
BaseTypeName = baseTypeName;
}

/// <summary>Initializes a new instance of the <see cref="CodeArtifact"/> class.</summary>
/// <param name="typeName">The type name.</param>
/// <param name="baseTypeName">The base type name (e.g. base class).</param>
/// <param name="type">The artifact type.</param>
/// <param name="language">The artifact language.</param>
/// <param name="category">The category.</param>
/// <param name="template">The template to render the code.</param>
public CodeArtifact(string typeName, CodeArtifactType type, CodeArtifactLanguage language, ITemplate template)
: this(typeName, null, type, language, template)
public CodeArtifact(string typeName, string baseTypeName, CodeArtifactType type, CodeArtifactLanguage language, CodeArtifactCategory category, ITemplate template)
: this(typeName, baseTypeName, type, language, category, template?.Render())
{
}

Expand All @@ -48,8 +52,9 @@ public CodeArtifact(string typeName, CodeArtifactType type, CodeArtifactLanguage
/// <param name="baseTypeName">The base type name (e.g. base class).</param>
/// <param name="type">The artifact type.</param>
/// <param name="language">The artifact language.</param>
/// <param name="template">The template to render the code.</param>
public CodeArtifact(string typeName, string baseTypeName, CodeArtifactType type, CodeArtifactLanguage language, ITemplate template)
/// <param name="category">The category.</param>
/// <param name="code">The code.</param>
public CodeArtifact(string typeName, string baseTypeName, CodeArtifactType type, CodeArtifactLanguage language, CodeArtifactCategory category, string code)
{
if (typeName == baseTypeName)
{
Expand All @@ -61,8 +66,8 @@ public CodeArtifact(string typeName, string baseTypeName, CodeArtifactType type,

Type = type;
Language = language;

Code = template?.Render();
Category = category;
Code = code;
}

/// <summary>Gets the type name.</summary>
Expand All @@ -77,7 +82,10 @@ public CodeArtifact(string typeName, string baseTypeName, CodeArtifactType type,
/// <summary>Get the artifact language.</summary>
public CodeArtifactLanguage Language { get; }

/// <summary>Gets or sets the generated code.</summary>
public string Code { get; set; }
/// <summary>Gets the category.</summary>
public CodeArtifactCategory Category { get; }

/// <summary>Gets the generated code.</summary>
public string Code { get; }
}
}
26 changes: 26 additions & 0 deletions src/NJsonSchema.CodeGeneration/CodeArtifactCategory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
//-----------------------------------------------------------------------
// <copyright file="CodeArtifactCategory.cs" company="NJsonSchema">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

namespace NJsonSchema.CodeGeneration
{
/// <summary>The code artifact category.</summary>
public enum CodeArtifactCategory
{
/// <summary>Undefined.</summary>
Undefined,

/// <summary>Client.</summary>
Client,

/// <summary>DTO.</summary>
Dto,

/// <summary>Utility.</summary>
Utility
}
}
Loading

0 comments on commit 3003a15

Please sign in to comment.