Skip to content

Commit

Permalink
Add TypeDescriminator to csharp properties
Browse files Browse the repository at this point in the history
  • Loading branch information
DanSmith committed Oct 2, 2018
1 parent a4377bc commit e677cf3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 10 deletions.
38 changes: 35 additions & 3 deletions Cogs.Publishers/Csharp/CsSchemaPublisher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ public void Publish(CogsModel model)
new XElement("AssemblyName", projName),
new XElement("RootNamespace", projName)),
new XElement("ItemGroup",
new XElement("PackageReference", new XAttribute("Include", "System.ComponentModel.Annotations"), new XAttribute("Version", "4.4.1")),
new XElement("PackageReference", new XAttribute("Include", "Microsoft.CSharp"), new XAttribute("Version", "4.4.1")),
new XElement("PackageReference", new XAttribute("Include", "Newtonsoft.Json"), new XAttribute("Version", "11.0.1")))));
new XElement("PackageReference", new XAttribute("Include", "System.ComponentModel.Annotations"), new XAttribute("Version", "4.5.0")),
new XElement("PackageReference", new XAttribute("Include", "Microsoft.CSharp"), new XAttribute("Version", "4.5.0")),
new XElement("PackageReference", new XAttribute("Include", "Newtonsoft.Json"), new XAttribute("Version", "11.0.2")))));
XmlWriterSettings xws = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Expand Down Expand Up @@ -201,6 +201,38 @@ public void Publish(CogsModel model)
}
else { newClass.AppendLine($"{Environment.NewLine} {{"); }


// insert a type descriminator that will be output for json serialization
if (item.IsSubstitute && item is DataType)
{
bool isFirst = true;
if(item.ParentTypes.Count > 0)
{
var directParent = item.ParentTypes.Last();
if (directParent.IsSubstitute)
{
// not first in inheritance chain with a type descriminator
isFirst = false;
}
}
if (isFirst)
{
newClass.AppendLine();
newClass.AppendLine(" /// <summary>");
newClass.AppendLine(" /// Set the TypeDescriminator");
newClass.AppendLine(" /// <summary>");
newClass.AppendLine(" public " + item.Name + "() { this.TypeDescriminator = this.GetType().Name; }");
newClass.AppendLine();
newClass.AppendLine(" /// <summary>");
newClass.AppendLine(" /// Type descriminator for json serialization");
newClass.AppendLine(" /// <summary>");
newClass.AppendLine(" [JsonProperty(\"$type\")]");
newClass.AppendLine(" public string TypeDescriminator { get; set; }");
newClass.AppendLine();
}
}


foreach (var prop in item.Properties)
{

Expand Down
17 changes: 10 additions & 7 deletions Cogs.Tests.Integration/Cogs.Tests.Integration.csproj
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp2.0</TargetFramework>
<TargetFramework>netcoreapp2.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.5.0-preview-20170923-02" />
<PackageReference Include="Newtonsoft.Json" Version="10.0.3" />
<PackageReference Include="NJsonSchema" Version="9.7.3" />
<PackageReference Include="xunit" Version="2.3.0" />
<PackageReference Include="xunit.analyzers" Version="0.7.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.8.0" />
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
<PackageReference Include="NJsonSchema" Version="9.11.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.analyzers" Version="0.10.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit e677cf3

Please sign in to comment.