-
Notifications
You must be signed in to change notification settings - Fork 203
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1603 from microsoft/feature/union-types-serializa…
…tion adds support for deserialization of composed types
- Loading branch information
Showing
71 changed files
with
3,400 additions
and
704 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
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
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 |
---|---|---|
@@ -0,0 +1,78 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Kiota.Builder; | ||
public class DiscriminatorInformation : CodeElement, ICloneable | ||
{ | ||
private ConcurrentDictionary<string, CodeTypeBase> discriminatorMappings = new(StringComparer.OrdinalIgnoreCase); | ||
/// <summary> | ||
/// Gets the discriminator values for the class where the key is the value as represented in the payload. | ||
/// </summary> | ||
public IOrderedEnumerable<KeyValuePair<string, CodeTypeBase>> DiscriminatorMappings | ||
{ | ||
get | ||
{ | ||
return (Parent is not CodeUnionType && | ||
Parent?.GetImmediateParentOfType<CodeClass>() is CodeClass parentClass ? | ||
discriminatorMappings.Where(x => x.Value is not CodeType currentType || currentType.TypeDefinition != parentClass) : | ||
discriminatorMappings) | ||
.OrderBy(static x => x.Key); | ||
} | ||
} | ||
/// <summary> | ||
/// Gets/Sets the name of the property to use for discrimination during deserialization. | ||
/// </summary> | ||
public string DiscriminatorPropertyName | ||
{ | ||
get; set; | ||
} | ||
|
||
public void AddDiscriminatorMapping(string key, CodeTypeBase type) | ||
{ | ||
ArgumentNullException.ThrowIfNull(type); | ||
if (string.IsNullOrEmpty(key)) throw new ArgumentNullException(nameof(key)); | ||
discriminatorMappings.TryAdd(key, type); | ||
} | ||
|
||
public CodeTypeBase GetDiscriminatorMappingValue(string key) | ||
{ | ||
if (string.IsNullOrEmpty(key)) throw new ArgumentNullException(nameof(key)); | ||
if (discriminatorMappings.TryGetValue(key, out var value)) | ||
return value; | ||
return null; | ||
} | ||
|
||
public void RemoveDiscriminatorMapping(params string[] keys) { | ||
ArgumentNullException.ThrowIfNull(keys); | ||
foreach(var key in keys) | ||
discriminatorMappings.TryRemove(key, out var _); | ||
} | ||
|
||
public object Clone() | ||
{ | ||
return new DiscriminatorInformation | ||
{ | ||
DiscriminatorPropertyName = DiscriminatorPropertyName, | ||
discriminatorMappings = discriminatorMappings == null ? null : new(discriminatorMappings), | ||
Parent = Parent, | ||
Name = Name?.Clone() as string, | ||
}; | ||
} | ||
public bool HasBasicDiscriminatorInformation => !string.IsNullOrEmpty(DiscriminatorPropertyName) && discriminatorMappings.Any(); | ||
public bool ShouldWriteDiscriminatorForInheritedType => HasBasicDiscriminatorInformation && IsComplexType; | ||
public bool ShouldWriteDiscriminatorForUnionType => IsUnionType; // if union of scalar types, then we don't always get discriminator information | ||
public bool ShouldWriteDiscriminatorForIntersectionType => IsIntersectionType; // if intersection of scalar types, then we don't always get discriminator information | ||
public bool ShouldWriteParseNodeCheck => ShouldWriteDiscriminatorForInheritedType || ShouldWriteDiscriminatorForUnionType || ShouldWriteDiscriminatorForIntersectionType; | ||
public bool ShouldWriteDiscriminatorBody => ShouldWriteParseNodeCheck && HasBasicDiscriminatorInformation; | ||
private bool IsUnionType => Is<CodeUnionType>(); | ||
private bool IsIntersectionType => Is<CodeIntersectionType>(); | ||
private bool IsComplexType => Parent is CodeClass currentClass && currentClass.OriginalComposedType is null || | ||
Parent is CodeMethod parentMethod && parentMethod.Parent is CodeFunction currentFunction && currentFunction.OriginalMethodParentClass?.OriginalComposedType is null; //static factories outside of classes (TS/Go) | ||
private bool Is<T>() where T : CodeComposedTypeBase | ||
{ | ||
return Parent is CodeClass currentClass && currentClass.OriginalComposedType is T || | ||
Parent is T; | ||
} | ||
} |
Oops, something went wrong.