-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for generic jsonDerivedAttribute (#32)
* Add generic JsonDerivedTypeAttribute * Parse generic JsonDerivedTypeAttribute * Update tests * Add GenericAttributeWorks * Use generic JsonDerivedType in Target tests * Update Readme
- Loading branch information
Showing
21 changed files
with
578 additions
and
4 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
6 changes: 6 additions & 0 deletions
6
...ationexam.GeneratedJsonConverters.SourceGenerator.Target/Contracts/GenericLeafContract.cs
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,6 @@ | ||
namespace Aviationexam.GeneratedJsonConverters.SourceGenerator.Target.Contracts; | ||
|
||
public sealed class GenericLeafContract : BaseContract | ||
{ | ||
public int Property { get; set; } | ||
} |
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
35 changes: 35 additions & 0 deletions
35
...shotTests.GenericAttributeWorks#BaseContractOfInt32JsonPolymorphicConverter.g.verified.cs
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,35 @@ | ||
//HintName: BaseContractOfInt32JsonPolymorphicConverter.g.cs | ||
#nullable enable | ||
|
||
namespace ApplicationNamespace; | ||
|
||
internal class BaseContractOfInt32JsonPolymorphicConverter : Aviationexam.GeneratedJsonConverters.PolymorphicJsonConvertor<ApplicationNamespace.Contracts.BaseContract<System.Int32>> | ||
{ | ||
protected override System.ReadOnlySpan<byte> GetDiscriminatorPropertyName() => "myCustomDiscriminator"u8; | ||
|
||
protected override System.Type GetTypeForDiscriminator( | ||
Aviationexam.GeneratedJsonConverters.IDiscriminatorStruct discriminator | ||
) => discriminator switch | ||
{ | ||
Aviationexam.GeneratedJsonConverters.DiscriminatorStruct<string> { Value: "IntLeafContract" } => typeof(ApplicationNamespace.Contracts.IntLeafContract), | ||
Aviationexam.GeneratedJsonConverters.DiscriminatorStruct<string> { Value: "StringLeafContract" } => typeof(ApplicationNamespace.Contracts.StringLeafContract), | ||
|
||
_ => throw new System.ArgumentOutOfRangeException(nameof(discriminator), discriminator, null), | ||
}; | ||
|
||
protected override Aviationexam.GeneratedJsonConverters.IDiscriminatorStruct GetDiscriminatorForType( | ||
System.Type type | ||
) | ||
{ | ||
if (type == typeof(ApplicationNamespace.Contracts.IntLeafContract)) | ||
{ | ||
return new Aviationexam.GeneratedJsonConverters.DiscriminatorStruct<string>("IntLeafContract"); | ||
} | ||
if (type == typeof(ApplicationNamespace.Contracts.StringLeafContract)) | ||
{ | ||
return new Aviationexam.GeneratedJsonConverters.DiscriminatorStruct<string>("StringLeafContract"); | ||
} | ||
|
||
throw new System.ArgumentOutOfRangeException(nameof(type), type, null); | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
...hotTests.GenericAttributeWorks#BaseContractOfStringJsonPolymorphicConverter.g.verified.cs
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,35 @@ | ||
//HintName: BaseContractOfStringJsonPolymorphicConverter.g.cs | ||
#nullable enable | ||
|
||
namespace ApplicationNamespace; | ||
|
||
internal class BaseContractOfStringJsonPolymorphicConverter : Aviationexam.GeneratedJsonConverters.PolymorphicJsonConvertor<ApplicationNamespace.Contracts.BaseContract<System.String>> | ||
{ | ||
protected override System.ReadOnlySpan<byte> GetDiscriminatorPropertyName() => "myCustomDiscriminator"u8; | ||
|
||
protected override System.Type GetTypeForDiscriminator( | ||
Aviationexam.GeneratedJsonConverters.IDiscriminatorStruct discriminator | ||
) => discriminator switch | ||
{ | ||
Aviationexam.GeneratedJsonConverters.DiscriminatorStruct<string> { Value: "IntLeafContract" } => typeof(ApplicationNamespace.Contracts.IntLeafContract), | ||
Aviationexam.GeneratedJsonConverters.DiscriminatorStruct<string> { Value: "StringLeafContract" } => typeof(ApplicationNamespace.Contracts.StringLeafContract), | ||
|
||
_ => throw new System.ArgumentOutOfRangeException(nameof(discriminator), discriminator, null), | ||
}; | ||
|
||
protected override Aviationexam.GeneratedJsonConverters.IDiscriminatorStruct GetDiscriminatorForType( | ||
System.Type type | ||
) | ||
{ | ||
if (type == typeof(ApplicationNamespace.Contracts.IntLeafContract)) | ||
{ | ||
return new Aviationexam.GeneratedJsonConverters.DiscriminatorStruct<string>("IntLeafContract"); | ||
} | ||
if (type == typeof(ApplicationNamespace.Contracts.StringLeafContract)) | ||
{ | ||
return new Aviationexam.GeneratedJsonConverters.DiscriminatorStruct<string>("StringLeafContract"); | ||
} | ||
|
||
throw new System.ArgumentOutOfRangeException(nameof(type), type, null); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...icConverterGeneratorSnapshotTests.GenericAttributeWorks#DiscriminatorStruct.g.verified.cs
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,16 @@ | ||
//HintName: DiscriminatorStruct.g.cs | ||
// ReSharper disable once RedundantNullableDirective | ||
|
||
#nullable enable | ||
|
||
namespace Aviationexam.GeneratedJsonConverters; | ||
|
||
internal readonly struct DiscriminatorStruct<T> : IDiscriminatorStruct | ||
{ | ||
public T Value { get; init; } | ||
|
||
public DiscriminatorStruct(T value) | ||
{ | ||
Value = value; | ||
} | ||
} |
10 changes: 10 additions & 0 deletions
10
...cConverterGeneratorSnapshotTests.GenericAttributeWorks#IDiscriminatorStruct.g.verified.cs
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,10 @@ | ||
//HintName: IDiscriminatorStruct.g.cs | ||
// ReSharper disable once RedundantNullableDirective | ||
|
||
#nullable enable | ||
|
||
namespace Aviationexam.GeneratedJsonConverters; | ||
|
||
internal interface IDiscriminatorStruct | ||
{ | ||
} |
85 changes: 85 additions & 0 deletions
85
...verterGeneratorSnapshotTests.GenericAttributeWorks#JsonDerivedTypeAttribute.g.verified.cs
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,85 @@ | ||
//HintName: JsonDerivedTypeAttribute.g.cs | ||
#nullable enable | ||
|
||
namespace Aviationexam.GeneratedJsonConverters.Attributes; | ||
|
||
/// <summary> | ||
/// This is a copy of System.Text.Json.Serialization.JsonDerivedTypeAttribute. | ||
/// It's purpose is to replace this attribute to silence System.Text.Json.Serialization.Metadata.PolymorphicTypeResolver{ThrowHelper.ThrowNotSupportedException_BaseConverterDoesNotSupportMetadata} | ||
/// | ||
/// When placed on a type declaration, indicates that the specified subtype should be opted into polymorphic serialization. | ||
/// </summary> | ||
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple = true, Inherited = false)] | ||
internal class JsonDerivedTypeAttribute : System.Text.Json.Serialization.JsonAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new attribute with specified parameters. | ||
/// </summary> | ||
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared based type.</param> | ||
public JsonDerivedTypeAttribute(System.Type derivedType) | ||
{ | ||
DerivedType = derivedType; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new attribute with specified parameters. | ||
/// </summary> | ||
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared base type.</param> | ||
/// <param name="typeDiscriminator">The type discriminator identifier to be used for the serialization of the subtype.</param> | ||
public JsonDerivedTypeAttribute(System.Type derivedType, string typeDiscriminator) | ||
{ | ||
DerivedType = derivedType; | ||
TypeDiscriminator = typeDiscriminator; | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new attribute with specified parameters. | ||
/// </summary> | ||
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared base type.</param> | ||
/// <param name="typeDiscriminator">The type discriminator identifier to be used for the serialization of the subtype.</param> | ||
public JsonDerivedTypeAttribute(System.Type derivedType, int typeDiscriminator) | ||
{ | ||
DerivedType = derivedType; | ||
TypeDiscriminator = typeDiscriminator; | ||
} | ||
|
||
/// <summary> | ||
/// A derived type that should be supported in polymorphic serialization of the declared base type. | ||
/// </summary> | ||
public System.Type DerivedType { get; } | ||
|
||
/// <summary> | ||
/// The type discriminator identifier to be used for the serialization of the subtype. | ||
/// </summary> | ||
public object? TypeDiscriminator { get; } | ||
} | ||
|
||
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple = true, Inherited = false)] | ||
internal class JsonDerivedTypeAttribute<TDerivedType> : JsonDerivedTypeAttribute | ||
{ | ||
/// <summary> | ||
/// Initializes a new attribute with specified parameters. | ||
/// </summary> | ||
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared based type.</param> | ||
public JsonDerivedTypeAttribute() : base(typeof(TDerivedType)) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new attribute with specified parameters. | ||
/// </summary> | ||
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared base type.</param> | ||
/// <param name="typeDiscriminator">The type discriminator identifier to be used for the serialization of the subtype.</param> | ||
public JsonDerivedTypeAttribute(string typeDiscriminator) : base(typeof(TDerivedType), typeDiscriminator) | ||
{ | ||
} | ||
|
||
/// <summary> | ||
/// Initializes a new attribute with specified parameters. | ||
/// </summary> | ||
/// <param name="derivedType">A derived type that should be supported in polymorphic serialization of the declared base type.</param> | ||
/// <param name="typeDiscriminator">The type discriminator identifier to be used for the serialization of the subtype.</param> | ||
public JsonDerivedTypeAttribute(int typeDiscriminator) : base(typeof(TDerivedType), typeDiscriminator) | ||
{ | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...verterGeneratorSnapshotTests.GenericAttributeWorks#JsonPolymorphicAttribute.g.verified.cs
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,32 @@ | ||
//HintName: JsonPolymorphicAttribute.g.cs | ||
#nullable enable | ||
|
||
namespace Aviationexam.GeneratedJsonConverters.Attributes; | ||
|
||
/// <summary> | ||
/// This is a copy of System.Text.Json.Serialization.JsonPolymorphicAttribute. | ||
/// It's purpose is to replace this attribute to silence System.Text.Json.Serialization.Metadata.PolymorphicTypeResolver{ThrowHelper.ThrowNotSupportedException_BaseConverterDoesNotSupportMetadata} | ||
/// | ||
/// When placed on a type, indicates that the type should be serialized polymorphically. | ||
/// </summary> | ||
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Interface, AllowMultiple = false, Inherited = false)] | ||
internal sealed class JsonPolymorphicAttribute : System.Text.Json.Serialization.JsonAttribute | ||
{ | ||
/// <summary> | ||
/// Gets or sets a custom type discriminator property name for the polymorhic type. | ||
/// Uses the default '$type' property name if left unset. | ||
/// </summary> | ||
public string? TypeDiscriminatorPropertyName { get; set; } | ||
|
||
/// <summary> | ||
/// Gets or sets the behavior when serializing an undeclared derived runtime type. | ||
/// </summary> | ||
public System.Text.Json.Serialization.JsonUnknownDerivedTypeHandling UnknownDerivedTypeHandling { get; set; } | ||
|
||
/// <summary> | ||
/// When set to <see langword="true"/>, instructs the deserializer to ignore any | ||
/// unrecognized type discriminator id's and reverts to the contract of the base type. | ||
/// Otherwise, it will fail the deserialization. | ||
/// </summary> | ||
public bool IgnoreUnrecognizedTypeDiscriminators { get; set; } | ||
} |
Oops, something went wrong.