Skip to content

Commit

Permalink
[aspnetcore] Improve model inheritance support by including JsonSubTy…
Browse files Browse the repository at this point in the history
…pes to properly handle serialization (#8384)

* Add JsonSubTypes to templates

* Add JsonSubTypes to templates

Co-authored-by: Anders Gilje <agilje@slb.com>
  • Loading branch information
goexplore and Anders Gilje authored Feb 16, 2021
1 parent 53285e5 commit cd0ca65
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="{{newtonsoftVersion}}" />
{{/useNewtonsoft}}
{{/useSwashbuckle}}
<PackageReference Include="JsonSubTypes" Version="1.8.0" />
</ItemGroup>
<ItemGroup>
<!--<DotNetCliToolReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="{{aspnetCoreVersion}}.0" />-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,13 @@ using System.ComponentModel;
using System.ComponentModel.DataAnnotations;
using System.Runtime.Serialization;
using Newtonsoft.Json;
{{#models}}
{{#model}}
{{#discriminator}}
using JsonSubTypes;
{{/discriminator}}
{{/model}}
{{/models}}
using {{packageName}}.Converters;

{{#models}}
Expand All @@ -17,6 +24,12 @@ namespace {{modelPackage}}
/// {{description}}
/// </summary>
[DataContract]
{{#discriminator}}
[JsonConverter(typeof(JsonSubtypes), "{{{discriminatorName}}}")]
{{#mappedModels}}
[JsonSubtypes.KnownSubType(typeof({{{modelName}}}), "{{^vendorExtensions.x-discriminator-value}}{{{mappingName}}}{{/vendorExtensions.x-discriminator-value}}{{#vendorExtensions.x-discriminator-value}}{{{.}}}{{/vendorExtensions.x-discriminator-value}}")]
{{/mappedModels}}
{{/discriminator}}
public {{#modelClassModifier}}{{modelClassModifier}} {{/modelClassModifier}}class {{classname}} : {{#parent}}{{{parent}}}, {{/parent}}IEquatable<{{classname}}>
{
{{#vars}}
Expand Down

0 comments on commit cd0ca65

Please sign in to comment.