Skip to content

Commit

Permalink
Merge pull request #516 from RSuter/master
Browse files Browse the repository at this point in the history
Release v9.8.0
  • Loading branch information
RicoSuter authored Oct 16, 2017
2 parents f1b6a62 + 36292c2 commit be4d4ba
Show file tree
Hide file tree
Showing 70 changed files with 1,359 additions and 313 deletions.
21 changes: 14 additions & 7 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public override string GenerateFile(string rootTypeNameHint)
var model = new FileTemplateModel
{
Namespace = Settings.Namespace ?? string.Empty,
Classes = ConversionUtilities.TrimWhiteSpaces(_resolver.GenerateClasses())
TypesCode = ConversionUtilities.TrimWhiteSpaces(_resolver.GenerateTypes().Concatenate())
};

var template = Settings.TemplateFactory.CreateTemplate("CSharp", "File", model);
Expand All @@ -69,7 +69,7 @@ public override string GenerateFile(string rootTypeNameHint)
/// <summary>Generates the type.</summary>
/// <param name="typeNameHint">The type name hint.</param>
/// <returns>The code.</returns>
public override TypeGeneratorResult GenerateType(string typeNameHint)
public override CodeArtifact GenerateType(string typeNameHint)
{
var typeName = _resolver.GetOrGenerateTypeName(_schema, typeNameHint);

Expand All @@ -79,17 +79,21 @@ public override TypeGeneratorResult GenerateType(string typeNameHint)
return GenerateClass(typeName);
}

private TypeGeneratorResult GenerateClass(string typeName)
private CodeArtifact GenerateClass(string typeName)
{
var model = new ClassTemplateModel(typeName, Settings, _resolver, _schema, RootObject);

RenamePropertyWithSameNameAsClass(typeName, model.Properties);

var template = Settings.TemplateFactory.CreateTemplate("CSharp", "Class", model);
return new TypeGeneratorResult
return new CodeArtifact
{
Type = CodeArtifactType.Class,
Language = CodeArtifactLanguage.CSharp,

TypeName = typeName,
BaseTypeName = model.BaseClass,
BaseTypeName = model.BaseClassName,

Code = template.Render()
};
}
Expand All @@ -107,12 +111,15 @@ private void RenamePropertyWithSameNameAsClass(string typeName, IEnumerable<Prop
}
}

private TypeGeneratorResult GenerateEnum(string typeName)
private CodeArtifact GenerateEnum(string typeName)
{
var model = new EnumTemplateModel(typeName, _schema, Settings);
var template = Settings.TemplateFactory.CreateTemplate("CSharp", "Enum", model);
return new TypeGeneratorResult
return new CodeArtifact
{
Type = CodeArtifactType.Enum,
Language = CodeArtifactLanguage.CSharp,

TypeName = typeName,
Code = template.Render()
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public CSharpGeneratorSettings()
TypeAccessModifier = "public";

PropertyNameGenerator = new CSharpPropertyNameGenerator();
TemplateFactory = new DefaultTemplateFactory();
TemplateFactory = new DefaultTemplateFactory((CodeGeneratorSettingsBase)this);
}

/// <summary>Gets or sets the .NET namespace of the generated types.</summary>
Expand Down
42 changes: 28 additions & 14 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpTypeResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using NJsonSchema.CodeGeneration.CSharp.Models;
using NJsonSchema.CodeGeneration.CSharp.Templates;

namespace NJsonSchema.CodeGeneration.CSharp
{
Expand Down Expand Up @@ -77,26 +77,40 @@ public override string Resolve(JsonSchema4 schema, bool isNullable, string typeN
return AddGenerator(schema, typeNameHint);
}

/// <summary>Generates all necessary classes.</summary>
/// <summary>Generates the code for all described types (e.g. interfaces, classes, enums, etc).</summary>
/// <returns>The code.</returns>
public string GenerateClasses()
public override CodeArtifactCollection GenerateTypes()
{
var classes = GenerateTypes(null);
var collection = base.GenerateTypes();
var results = new List<CodeArtifact>();

// TODO: Move utility classes to FileTemplate.tt
if (classes.Contains("JsonInheritanceConverter"))
if (collection.Artifacts.Any(r => r.Code.Contains("JsonInheritanceConverter")))
{
var templateModel = new JsonInheritanceConverterTemplateModel(Settings);
var template = new JsonInheritanceConverterTemplate(templateModel);
classes += "\n\n" + template.Render();
results.Add(new CodeArtifact
{
Type = CodeArtifactType.Class,
Language = CodeArtifactLanguage.CSharp,

TypeName = "JsonInheritanceConverter",
Code = Settings.TemplateFactory.CreateTemplate(
"CSharp", "JsonInheritanceConverter", new JsonInheritanceConverterTemplateModel(Settings)).Render()
});
}
if (classes.Contains("DateFormatConverter"))

if (collection.Artifacts.Any(r => r.Code.Contains("DateFormatConverter")))
{
var templateModel = new DateFormatConverterTemplateModel(Settings);
var template = new DateFormatConverterTemplate(templateModel);
classes += "\n\n" + template.Render();
results.Add(new CodeArtifact
{
Type = CodeArtifactType.Class,
Language = CodeArtifactLanguage.CSharp,

TypeName = "DateFormatConverter",
Code = Settings.TemplateFactory.CreateTemplate(
"CSharp", "DateFormatConverter", new DateFormatConverterTemplateModel(Settings)).Render()
});
}
return classes;

return new CodeArtifactCollection(collection.Artifacts.Concat(results));
}

/// <summary>Adds a generator for the given schema if necessary.</summary>
Expand Down
26 changes: 16 additions & 10 deletions src/NJsonSchema.CodeGeneration.CSharp/Models/ClassTemplateModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ public class ClassTemplateModel : ClassTemplateModelBase
/// <param name="resolver">The resolver.</param>
/// <param name="schema">The schema.</param>
/// <param name="rootObject">The root object.</param>
public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
CSharpTypeResolver resolver, JsonSchema4 schema, object rootObject)
: base(resolver, schema, rootObject)
{
_resolver = resolver;
_schema = schema;
_settings = settings;

Class = typeName;
ClassName = typeName;
Properties = _schema.ActualProperties.Values
.Where(p => !p.IsInheritanceDiscriminator)
.Select(property => new PropertyModel(this, property, _resolver, _settings))
.ToList();
}

/// <summary>Gets or sets the class name.</summary>
public override string Class { get; }
public override string ClassName { get; }

/// <summary>Gets the namespace.</summary>
public string Namespace => _settings.Namespace;
Expand All @@ -50,10 +50,10 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
public bool HasAdditionalPropertiesType => _schema.AdditionalPropertiesSchema != null;

/// <summary>Gets the type of the additional properties.</summary>
public string AdditionalPropertiesType => _resolver.Resolve(
public string AdditionalPropertiesType => HasAdditionalPropertiesType ? _resolver.Resolve(
_schema.AdditionalPropertiesSchema,
_schema.AdditionalPropertiesSchema.IsNullable(_settings.SchemaType),
string.Empty);
string.Empty) : null;

/// <summary>Gets the property models.</summary>
public IEnumerable<PropertyModel> Properties { get; }
Expand All @@ -66,7 +66,7 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,

/// <summary>Gets a value indicating whether the class style is INPC.</summary>
/// <value><c>true</c> if inpc; otherwise, <c>false</c>.</value>
public bool Inpc => _settings.ClassStyle == CSharpClassStyle.Inpc;
public bool RenderInpc => _settings.ClassStyle == CSharpClassStyle.Inpc;

/// <summary>Gets a value indicating whether the class has discriminator property.</summary>
public bool HasDiscriminator => !string.IsNullOrEmpty(_schema.Discriminator);
Expand All @@ -78,7 +78,7 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
public bool HasInheritance => _schema.InheritedSchema != null;

/// <summary>Gets the base class name.</summary>
public string BaseClass => HasInheritance ? _resolver.Resolve(_schema.InheritedSchema, false, string.Empty) : null;
public string BaseClassName => HasInheritance ? _resolver.Resolve(_schema.InheritedSchema, false, string.Empty) : null;

/// <summary>Gets or sets the access modifier of generated classes and interfaces.</summary>
public string TypeAccessModifier => _settings.TypeAccessModifier;
Expand All @@ -87,14 +87,20 @@ public ClassTemplateModel(string typeName, CSharpGeneratorSettings settings,
public string JsonSerializerParameterCode => CSharpJsonSerializerGenerator.GenerateJsonSerializerParameterCode(_settings, null);

/// <summary>Gets the inheritance code.</summary>
public string Inheritance
public string InheritanceCode
{
get
{
if (HasInheritance)
return ": " + BaseClass + (_settings.ClassStyle == CSharpClassStyle.Inpc ? ", System.ComponentModel.INotifyPropertyChanged" : "");
{
return ": " + BaseClassName + (_settings.ClassStyle == CSharpClassStyle.Inpc ?
", System.ComponentModel.INotifyPropertyChanged" : "");
}
else
return _settings.ClassStyle == CSharpClassStyle.Inpc ? ": System.ComponentModel.INotifyPropertyChanged" : "";
{
return _settings.ClassStyle == CSharpClassStyle.Inpc ?
": System.ComponentModel.INotifyPropertyChanged" : "";
}
}
}
}
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
public class EnumTemplateModel : TemplateModelBase
{
private readonly JsonSchema4 _schema;
private readonly CSharpGeneratorSettings _settings;
Expand Down Expand Up @@ -52,7 +52,7 @@ public IEnumerable<EnumerationItemModel> Enums
get
{
var entries = new List<EnumerationItemModel>();
for (int i = 0; i < _schema.Enumeration.Count; i++)
for (var i = 0; i < _schema.Enumeration.Count; i++)
{
var value = _schema.Enumeration.ElementAt(i);
if (value != null)
Expand All @@ -69,6 +69,7 @@ public IEnumerable<EnumerationItemModel> Enums
});
}
}

return entries;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class FileTemplateModel
/// <summary>Gets or sets the namespace.</summary>
public string Namespace { get; set; }

/// <summary>Gets or sets the classes code.</summary>
public string Classes { get; set; }
/// <summary>Gets or sets the types code.</summary>
public string TypesCode { get; set; }
}
}
4 changes: 2 additions & 2 deletions src/NJsonSchema.CodeGeneration.CSharp/Models/PropertyModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public PropertyModel(ClassTemplateModel classTemplateModel, JsonProperty propert
)) == false;

/// <summary>Gets the json property required.</summary>
public string JsonPropertyRequired
public string JsonPropertyRequiredCode
{
get
{
Expand Down Expand Up @@ -178,7 +178,7 @@ public bool RenderRegularExpressionAttribute
}

/// <summary>Gets the regular expression value for the regular expression attribute.</summary>
public string RegularExpressionValue => _property.Pattern.Replace("\"", "\"\"");
public string RegularExpressionValue => _property.Pattern?.Replace("\"", "\"\"");

/// <summary>Gets a value indicating whether the property type is string enum.</summary>
public bool IsStringEnum => _property.ActualPropertySchema.IsEnumeration && _property.ActualPropertySchema.Type == JsonObjectType.String;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<PropertyGroup>
<TargetFrameworks>netstandard1.3;net451</TargetFrameworks>
<Description>JSON Schema draft v4 reader, generator and validator for .NET</Description>
<Version>9.7.7</Version>
<Version>9.8.0</Version>
<PackageTags>json schema validation generator .net</PackageTags>
<Copyright>Copyright © Rico Suter, 2017</Copyright>
<PackageLicenseUrl>https://github.com/rsuter/NJsonSchema/blob/master/LICENSE.md</PackageLicenseUrl>
Expand All @@ -20,6 +20,25 @@
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<WarningsAsErrors />
</PropertyGroup>
<ItemGroup>
<None Remove="Templates\Class.ToJson.liquid" />
<None Remove="Templates\Liquid\Class.FromJson.liquid" />
<None Remove="Templates\Liquid\Class.Inpc.liquid" />
<None Remove="Templates\Liquid\DateFormatConverter.liquid" />
<None Remove="Templates\Liquid\Enum.liquid" />
<None Remove="Templates\Liquid\File.liquid" />
<None Remove="Templates\Liquid\JsonInheritanceConverter.liquid" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Templates\Liquid\Class.FromJson.liquid" />
<EmbeddedResource Include="Templates\Liquid\Class.Inpc.liquid" />
<EmbeddedResource Include="Templates\Liquid\Class.liquid" />
<EmbeddedResource Include="Templates\Liquid\Class.ToJson.liquid" />
<EmbeddedResource Include="Templates\Liquid\DateFormatConverter.liquid" />
<EmbeddedResource Include="Templates\Liquid\Enum.liquid" />
<EmbeddedResource Include="Templates\Liquid\File.liquid" />
<EmbeddedResource Include="Templates\Liquid\JsonInheritanceConverter.liquid" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="9.0.1" />
</ItemGroup>
Expand Down
18 changes: 9 additions & 9 deletions src/NJsonSchema.CodeGeneration.CSharp/Templates/ClassTemplate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -109,21 +109,21 @@ public virtual string TransformText()
this.Write(" partial class ");

#line 12 "C:\Data\Projects\NJsonSchema\src\NJsonSchema.CodeGeneration.CSharp\Templates\ClassTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class));
this.Write(this.ToStringHelper.ToStringWithCulture(Model.ClassName));

#line default
#line hidden
this.Write(" ");

#line 12 "C:\Data\Projects\NJsonSchema\src\NJsonSchema.CodeGeneration.CSharp\Templates\ClassTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(Model.Inheritance));
this.Write(this.ToStringHelper.ToStringWithCulture(Model.InheritanceCode));

#line default
#line hidden
this.Write("\r\n{\r\n");

#line 14 "C:\Data\Projects\NJsonSchema\src\NJsonSchema.CodeGeneration.CSharp\Templates\ClassTemplate.tt"
if(Model.Inpc){
if(Model.RenderInpc){

#line default
#line hidden
Expand Down Expand Up @@ -216,7 +216,7 @@ public virtual string TransformText()
this.Write("\", Required = ");

#line 24 "C:\Data\Projects\NJsonSchema\src\NJsonSchema.CodeGeneration.CSharp\Templates\ClassTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(property.JsonPropertyRequired));
this.Write(this.ToStringHelper.ToStringWithCulture(property.JsonPropertyRequiredCode));

#line default
#line hidden
Expand Down Expand Up @@ -376,7 +376,7 @@ public virtual string TransformText()
#line hidden

#line 43 "C:\Data\Projects\NJsonSchema\src\NJsonSchema.CodeGeneration.CSharp\Templates\ClassTemplate.tt"
if(!Model.Inpc){
if(!Model.RenderInpc){

#line default
#line hidden
Expand Down Expand Up @@ -509,7 +509,7 @@ public virtual string TransformText()
#line hidden

#line 74 "C:\Data\Projects\NJsonSchema\src\NJsonSchema.CodeGeneration.CSharp\Templates\ClassTemplate.tt"
if(Model.Inpc){
if(Model.RenderInpc){

#line default
#line hidden
Expand All @@ -532,15 +532,15 @@ public virtual string TransformText()
this.Write(");\r\n }\r\n \r\n public static ");

#line 83 "C:\Data\Projects\NJsonSchema\src\NJsonSchema.CodeGeneration.CSharp\Templates\ClassTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class));
this.Write(this.ToStringHelper.ToStringWithCulture(Model.ClassName));

#line default
#line hidden
this.Write(" FromJson(string data)\r\n {\r\n return Newtonsoft.Json.JsonConvert.Deseria" +
"lizeObject<");

#line 85 "C:\Data\Projects\NJsonSchema\src\NJsonSchema.CodeGeneration.CSharp\Templates\ClassTemplate.tt"
this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class));
this.Write(this.ToStringHelper.ToStringWithCulture(Model.ClassName));

#line default
#line hidden
Expand All @@ -554,7 +554,7 @@ public virtual string TransformText()
this.Write(");\r\n }\r\n");

#line 87 "C:\Data\Projects\NJsonSchema\src\NJsonSchema.CodeGeneration.CSharp\Templates\ClassTemplate.tt"
if(Model.Inpc){
if(Model.RenderInpc){

#line default
#line hidden
Expand Down
Loading

0 comments on commit be4d4ba

Please sign in to comment.