Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release v9.9.0 #522

Merged
merged 10 commits into from
Oct 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 57 additions & 46 deletions src/NJsonSchema.CodeGeneration.CSharp/CSharpGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,89 +13,107 @@
namespace NJsonSchema.CodeGeneration.CSharp
{
/// <summary>The CSharp code generator.</summary>
public class CSharpGenerator : TypeGeneratorBase
public class CSharpGenerator : GeneratorBase
{
private readonly JsonSchema4 _schema;
private readonly CSharpTypeResolver _resolver;

/// <summary>Initializes a new instance of the <see cref="CSharpGenerator"/> class.</summary>
/// <param name="schema">The schema.</param>
public CSharpGenerator(JsonSchema4 schema)
: this(schema, new CSharpGeneratorSettings())
/// <param name="rootObject">The root object to search for all JSON Schemas.</param>
public CSharpGenerator(object rootObject)
: this(rootObject, new CSharpGeneratorSettings())
{
}

/// <summary>Initializes a new instance of the <see cref="CSharpGenerator"/> class.</summary>
/// <param name="schema">The schema.</param>
/// <param name="rootObject">The root object to search for all JSON Schemas.</param>
/// <param name="settings">The generator settings.</param>
public CSharpGenerator(JsonSchema4 schema, CSharpGeneratorSettings settings)
: this(schema, settings, new CSharpTypeResolver(settings, schema), null)
public CSharpGenerator(object rootObject, CSharpGeneratorSettings settings)
: this(rootObject, settings, new CSharpTypeResolver(settings))
{
}

/// <summary>Initializes a new instance of the <see cref="CSharpGenerator"/> class.</summary>
/// <param name="schema">The schema.</param>
/// <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>
/// <param name="rootObject">The root object to search for all JSON Schemas.</param>
public CSharpGenerator(JsonSchema4 schema, CSharpGeneratorSettings settings, CSharpTypeResolver resolver, object rootObject)
: base(schema, rootObject)
public CSharpGenerator(object rootObject, CSharpGeneratorSettings settings, CSharpTypeResolver resolver)
: base(rootObject, resolver, settings)
{
_schema = schema;
_resolver = resolver;
Settings = settings;
}

/// <summary>Gets the generator settings.</summary>
public CSharpGeneratorSettings Settings { get; }

/// <summary>Generates the file.</summary>
/// <param name="rootTypeNameHint">The root type name hint.</param>
/// <returns>The file contents.</returns>
public override string GenerateFile(string rootTypeNameHint)
/// <inheritdoc />
public override CodeArtifactCollection GenerateTypes()
{
_resolver.Resolve(_schema, false, rootTypeNameHint); // register root type
var collection = base.GenerateTypes();
var results = new List<CodeArtifact>();

if (collection.Artifacts.Any(r => r.Code.Contains("JsonInheritanceConverter")))
{
if (Settings.ExcludedTypeNames?.Contains("JsonInheritanceAttribute") != true)
{
var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceAttribute", null);
results.Add(new CodeArtifact("JsonInheritanceConverter", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, template));
}

if (Settings.ExcludedTypeNames?.Contains("JsonInheritanceConverter") != true)
{
var template = Settings.TemplateFactory.CreateTemplate("CSharp", "JsonInheritanceConverter", null);
results.Add(new CodeArtifact("JsonInheritanceConverter", CodeArtifactType.Class, CodeArtifactLanguage.CSharp, template));
}
}

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

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

/// <inheritdoc />
protected override string GenerateFile(CodeArtifactCollection artifactCollection)
{
var model = new FileTemplateModel
{
Namespace = Settings.Namespace ?? string.Empty,
TypesCode = ConversionUtilities.TrimWhiteSpaces(_resolver.GenerateTypes().Concatenate())
TypesCode = artifactCollection.Concatenate()
};

var template = Settings.TemplateFactory.CreateTemplate("CSharp", "File", model);
return ConversionUtilities.TrimWhiteSpaces(template.Render());
}

/// <summary>Generates the type.</summary>
/// <param name="schema">The schema.</param>
/// <param name="typeNameHint">The type name hint.</param>
/// <returns>The code.</returns>
public override CodeArtifact GenerateType(string typeNameHint)
protected override CodeArtifact GenerateType(JsonSchema4 schema, string typeNameHint)
{
var typeName = _resolver.GetOrGenerateTypeName(_schema, typeNameHint);
var typeName = _resolver.GetOrGenerateTypeName(schema, typeNameHint);

if (_schema.IsEnumeration)
return GenerateEnum(typeName);
if (schema.IsEnumeration)
return GenerateEnum(schema, typeName);
else
return GenerateClass(typeName);
return GenerateClass(schema, typeName);
}

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

RenamePropertyWithSameNameAsClass(typeName, model.Properties);

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

TypeName = typeName,
BaseTypeName = model.BaseClassName,

Code = template.Render()
};
return new CodeArtifact(typeName, model.BaseClassName, CodeArtifactType.Class, CodeArtifactLanguage.CSharp, template);
}

private void RenamePropertyWithSameNameAsClass(string typeName, IEnumerable<PropertyModel> properties)
Expand All @@ -111,18 +129,11 @@ private void RenamePropertyWithSameNameAsClass(string typeName, IEnumerable<Prop
}
}

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

TypeName = typeName,
Code = template.Render()
};
return new CodeArtifact(typeName, CodeArtifactType.Enum, CodeArtifactLanguage.CSharp, template);
}
}
}
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((CodeGeneratorSettingsBase)this);
TemplateFactory = new DefaultTemplateFactory(this);
}

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

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

namespace NJsonSchema.CodeGeneration.CSharp
{
/// <summary>Manages the generated types and converts JSON types to CSharp types. </summary>
public class CSharpTypeResolver : TypeResolverBase<CSharpGenerator>
public class CSharpTypeResolver : TypeResolverBase
{
private readonly object _rootObject;

/// <summary>Initializes a new instance of the <see cref="CSharpTypeResolver"/> class.</summary>
/// <param name="settings">The generator settings.</param>
/// <param name="rootObject">The root object to search for JSON Schemas.</param>
public CSharpTypeResolver(CSharpGeneratorSettings settings, object rootObject)
public CSharpTypeResolver(CSharpGeneratorSettings settings)
: base(settings)
{
_rootObject = rootObject;
Settings = settings;
}

Expand Down Expand Up @@ -74,76 +68,9 @@ public override string Resolve(JsonSchema4 schema, bool isNullable, string typeN
return string.Format(Settings.DictionaryType + "<string, {0}>", valueType);
}

return AddGenerator(schema, typeNameHint);
}

/// <inheritdoc />
public override CodeArtifactCollection GenerateTypes()
{
return GenerateTypes(null);
}

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

if (collection.Artifacts.Any(r => r.Code.Contains("JsonInheritanceConverter")))
{
results.Add(new CodeArtifact
{
Type = CodeArtifactType.Class,
Language = CodeArtifactLanguage.CSharp,

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

if (collection.Artifacts.Any(r => r.Code.Contains("DateFormatConverter")))
{
results.Add(new CodeArtifact
{
Type = CodeArtifactType.Class,
Language = CodeArtifactLanguage.CSharp,

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

return new CodeArtifactCollection(collection.Artifacts.Concat(results));
return GetOrGenerateTypeName(schema, typeNameHint);
}

/// <summary>Adds a generator for the given schema if necessary.</summary>
/// <param name="schema">The schema.</param>
/// <param name="typeNameHint">The type name hint.</param>
/// <returns>The type name of the created generator.</returns>
protected override string AddGenerator(JsonSchema4 schema, string typeNameHint)
{
if (schema.IsEnumeration && schema.Type == JsonObjectType.Integer)
{
// Regenerate generator because it is be better than the current one (defined enum values)
var typeName = GetOrGenerateTypeName(schema, typeNameHint);
var generator = CreateTypeGenerator(schema);
AddOrReplaceTypeGenerator(typeName, generator);
}

return base.AddGenerator(schema, typeNameHint);
}

/// <summary>Creates a type generator.</summary>
/// <param name="schema">The schema.</param>
/// <returns>The generator.</returns>
protected override CSharpGenerator CreateTypeGenerator(JsonSchema4 schema)
{
return new CSharpGenerator(schema, Settings, this, _rootObject);
}


private string ResolveString(JsonSchema4 schema, bool isNullable, string typeNameHint)
{
if (schema.Format == JsonFormatStrings.Date)
Expand All @@ -169,7 +96,7 @@ private string ResolveString(JsonSchema4 schema, bool isNullable, string typeNam
#pragma warning restore 618

if (schema.IsEnumeration)
return AddGenerator(schema, typeNameHint) + (isNullable ? "?" : string.Empty);
return GetOrGenerateTypeName(schema, typeNameHint) + (isNullable ? "?" : string.Empty);

return "string";
}
Expand All @@ -182,7 +109,7 @@ private static string ResolveBoolean(bool isNullable)
private string ResolveInteger(JsonSchema4 schema, bool isNullable, string typeNameHint)
{
if (schema.IsEnumeration)
return AddGenerator(schema, typeNameHint) + (isNullable ? "?" : string.Empty);
return GetOrGenerateTypeName(schema, typeNameHint) + (isNullable ? "?" : string.Empty);

if (schema.Format == JsonFormatStrings.Byte)
return isNullable ? "byte?" : "byte";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CSharpValueGenerator : ValueGeneratorBase
/// <summary>Initializes a new instance of the <see cref="CSharpValueGenerator" /> class.</summary>
/// <param name="typeResolver">The type resolver.</param>
/// <param name="settings">The settings.</param>
public CSharpValueGenerator(ITypeResolver typeResolver, CSharpGeneratorSettings settings)
public CSharpValueGenerator(TypeResolverBase typeResolver, CSharpGeneratorSettings settings)
: base(typeResolver, settings.EnumNameGenerator)
{
_settings = settings;
Expand Down

This file was deleted.

Loading