From 1f1696967621450d963ecef243749cab5d89ef22 Mon Sep 17 00:00:00 2001 From: Jayasankar Pandyat Date: Thu, 30 Nov 2023 22:27:52 +0200 Subject: [PATCH] Adding support for System.Text.Json.Serialization.JsonConverter --- .../Extensions/TypeExtensions.cs | 19 +++++++++++-------- .../Visitors/ByteEnumTypeVisitor.cs | 5 +++-- .../Visitors/Int16EnumTypeVisitor.cs | 4 ++-- .../Visitors/Int32EnumTypeVisitor.cs | 4 ++-- .../Visitors/Int64EnumTypeVisitor.cs | 4 ++-- .../Visitors/StringEnumTypeVisitor.cs | 4 ++-- 6 files changed, 22 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Extensions/TypeExtensions.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Extensions/TypeExtensions.cs index cf2a7b6f..8c2406ba 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Extensions/TypeExtensions.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Extensions/TypeExtensions.cs @@ -369,20 +369,23 @@ public static bool IsOpenApiNullable(this Type type, out Type underlyingType) } /// - /// Checks whether the given enum type has or not. + /// Checks whether the given enum type has or or not. /// - /// Type of . + /// Type of . + /// Type of . /// Enum type. - /// Returns True, if the given enum type has ; otherwise, returns False. - public static bool HasJsonConverterAttribute(this Type type) where T : JsonConverter + /// Returns True, if the given enum type has ; or otherwise, returns False. + public static bool HasJsonConverterAttribute(this Type type) where T : Newtonsoft.Json.JsonConverter where U : System.Text.Json.Serialization.JsonConverter { - var attribute = type.GetCustomAttribute(inherit: false); - if (attribute.IsNullOrDefault()) + var newtonsoftAttribute = type.GetCustomAttribute(inherit: false); + var systemAttribute = type.GetCustomAttribute(inherit: false); + if (newtonsoftAttribute is null && systemAttribute is null) { return false; } - return typeof(T).IsAssignableFrom(attribute.ConverterType); + return (newtonsoftAttribute != null && typeof(T).IsAssignableFrom(newtonsoftAttribute.ConverterType)) + || (systemAttribute != null && typeof(U).IsAssignableFrom(systemAttribute.ConverterType)); } /// @@ -460,7 +463,7 @@ public static Type GetUnderlyingType(this Type type) } if (type.IsOpenApiArray()) - { + { underlyingType = type.GetElementType() ?? type.GetGenericArguments()[0]; } diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/ByteEnumTypeVisitor.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/ByteEnumTypeVisitor.cs index 59c916e5..3de1008c 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/ByteEnumTypeVisitor.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/ByteEnumTypeVisitor.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.Linq; using System.Text; +using System.Text.Json.Serialization; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; @@ -27,10 +28,10 @@ public ByteEnumTypeVisitor(VisitorCollection visitorCollection) /// public override bool IsVisitable(Type type) { - + var isVisitable = this.IsVisitable(type, TypeCode.Byte) && type.IsUnflaggedEnumType() && - !type.HasJsonConverterAttribute() && + !type.HasJsonConverterAttribute() && Enum.GetUnderlyingType(type) == typeof(byte) ; diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int16EnumTypeVisitor.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int16EnumTypeVisitor.cs index 218d1ed5..d23548dc 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int16EnumTypeVisitor.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int16EnumTypeVisitor.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; - +using System.Text.Json.Serialization; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; @@ -29,7 +29,7 @@ public override bool IsVisitable(Type type) { var isVisitable = this.IsVisitable(type, TypeCode.Int16) && type.IsUnflaggedEnumType() && - !type.HasJsonConverterAttribute() && + !type.HasJsonConverterAttribute() && Enum.GetUnderlyingType(type) == typeof(short) ; diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int32EnumTypeVisitor.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int32EnumTypeVisitor.cs index 0c5913ea..a6008b5a 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int32EnumTypeVisitor.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int32EnumTypeVisitor.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; - +using System.Text.Json.Serialization; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; @@ -29,7 +29,7 @@ public override bool IsVisitable(Type type) { var isVisitable = this.IsVisitable(type, TypeCode.Int32) && type.IsUnflaggedEnumType() && - !type.HasJsonConverterAttribute() && + !type.HasJsonConverterAttribute() && Enum.GetUnderlyingType(type) == typeof(int) ; diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int64EnumTypeVisitor.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int64EnumTypeVisitor.cs index 0065be52..b627cc94 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int64EnumTypeVisitor.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/Int64EnumTypeVisitor.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; - +using System.Text.Json.Serialization; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; @@ -29,7 +29,7 @@ public override bool IsVisitable(Type type) { var isVisitable = this.IsVisitable(type, TypeCode.Int64) && type.IsUnflaggedEnumType() && - !type.HasJsonConverterAttribute() && + !type.HasJsonConverterAttribute() && Enum.GetUnderlyingType(type) == typeof(long) ; diff --git a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/StringEnumTypeVisitor.cs b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/StringEnumTypeVisitor.cs index 4e923a88..cc31acdf 100644 --- a/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/StringEnumTypeVisitor.cs +++ b/src/Microsoft.Azure.WebJobs.Extensions.OpenApi.Core/Visitors/StringEnumTypeVisitor.cs @@ -1,7 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; - +using System.Text.Json.Serialization; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Attributes; using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions; @@ -29,7 +29,7 @@ public override bool IsVisitable(Type type) { var isVisitable = (this.IsVisitable(type, TypeCode.Int16) || this.IsVisitable(type, TypeCode.Int32) || this.IsVisitable(type, TypeCode.Int64) || this.IsVisitable(type, TypeCode.Byte)) && type.IsUnflaggedEnumType() && - type.HasJsonConverterAttribute() + type.HasJsonConverterAttribute(); ; return isVisitable;