-
-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow renaming discriminator values in JsonInheritanceConverter & sup…
…port that in generator, closes #668
- Loading branch information
Showing
4 changed files
with
106 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,8 +6,11 @@ | |
// <author>Rico Suter, [email protected]</author> | ||
//----------------------------------------------------------------------- | ||
|
||
using System; | ||
using Newtonsoft.Json; | ||
using System.Collections.Generic; | ||
using System.Reflection; | ||
using NJsonSchema.Infrastructure; | ||
|
||
namespace NJsonSchema | ||
{ | ||
|
@@ -21,5 +24,32 @@ public class OpenApiDiscriminator | |
/// <summary>Gets or sets the discriminator mappings.</summary> | ||
[JsonProperty("mapping", DefaultValueHandling = DefaultValueHandling.IgnoreAndPopulate)] | ||
public IDictionary<string, JsonSchema4> Mapping { get; } = new Dictionary<string, JsonSchema4>(); | ||
|
||
/// <summary>The currently used <see cref="JsonInheritanceConverter"/>.</summary> | ||
[JsonIgnore] | ||
public object JsonInheritanceConverter { get; set; } | ||
|
||
/// <summary>Adds a discriminator mapping for the given type and schema based on the used <see cref="JsonInheritanceConverter"/>.</summary> | ||
/// <param name="type">The type.</param> | ||
/// <param name="schema">The schema.</param> | ||
public void AddMapping(Type type, JsonSchema4 schema) | ||
{ | ||
dynamic converter = JsonInheritanceConverter; | ||
|
||
var getDiscriminatorValueMethod = JsonInheritanceConverter.GetType() | ||
#if LEGACY | ||
.GetMethod(nameof(Converters.JsonInheritanceConverter.GetDiscriminatorValue), new Type[] { typeof(Type) }); | ||
#else | ||
.GetRuntimeMethod(nameof(Converters.JsonInheritanceConverter.GetDiscriminatorValue), new Type[] { typeof(Type) }); | ||
#endif | ||
|
||
if (getDiscriminatorValueMethod != null) | ||
{ | ||
var discriminatorValue = converter.GetDiscriminatorValue(type); | ||
Mapping[discriminatorValue] = new JsonSchema4 { Reference = schema.ActualSchema }; | ||
} | ||
else | ||
Mapping[type.Name] = new JsonSchema4 { Reference = schema.ActualSchema }; | ||
} | ||
} | ||
} |