Skip to content

Adding support for System.Text.Json.Serialization.JsonConverter #632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -369,20 +369,23 @@ public static bool IsOpenApiNullable(this Type type, out Type underlyingType)
}

/// <summary>
/// Checks whether the given enum type has <see cref="JsonConverterAttribute"/> or not.
/// Checks whether the given enum type has <see cref="Newtonsoft.Json.JsonConverterAttribute"/> or <see cref="System.Text.Json.Serialization.JsonConverterAttribute"/> or not.
/// </summary>
/// <typeparam name="T">Type of <see cref="JsonConverter"/>.</typeparam>
/// <typeparam name="T">Type of <see cref="Newtonsoft.Json.JsonConverter"/>.</typeparam>
/// <typeparam name="U">Type of <see cref="System.Text.Json.Serialization.JsonConverter"/>.</typeparam>
/// <param name="type">Enum type.</param>
/// <returns>Returns <c>True</c>, if the given enum type has <see cref="JsonConverterAttribute"/>; otherwise, returns <c>False</c>.</returns>
public static bool HasJsonConverterAttribute<T>(this Type type) where T : JsonConverter
/// <returns>Returns <c>True</c>, if the given enum type has <see cref="Newtonsoft.Json.JsonConverterAttribute"/>; or <see cref="System.Text.Json.Serialization.JsonConverterAttribute"/> otherwise, returns <c>False</c>.</returns>
public static bool HasJsonConverterAttribute<T, U>(this Type type) where T : Newtonsoft.Json.JsonConverter where U : System.Text.Json.Serialization.JsonConverter
{
var attribute = type.GetCustomAttribute<JsonConverterAttribute>(inherit: false);
if (attribute.IsNullOrDefault())
var newtonsoftAttribute = type.GetCustomAttribute<JsonConverterAttribute>(inherit: false);
var systemAttribute = type.GetCustomAttribute<System.Text.Json.Serialization.JsonConverterAttribute>(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));
}

/// <summary>
Expand Down Expand Up @@ -460,7 +463,7 @@ public static Type GetUnderlyingType(this Type type)
}

if (type.IsOpenApiArray())
{
{
underlyingType = type.GetElementType() ?? type.GetGenericArguments()[0];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -27,10 +28,10 @@ public ByteEnumTypeVisitor(VisitorCollection visitorCollection)
/// <inheritdoc />
public override bool IsVisitable(Type type)
{

var isVisitable = this.IsVisitable(type, TypeCode.Byte) &&
type.IsUnflaggedEnumType() &&
!type.HasJsonConverterAttribute<StringEnumConverter>() &&
!type.HasJsonConverterAttribute<StringEnumConverter, JsonStringEnumConverter>() &&
Enum.GetUnderlyingType(type) == typeof(byte)
;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,7 +29,7 @@ public override bool IsVisitable(Type type)
{
var isVisitable = this.IsVisitable(type, TypeCode.Int16) &&
type.IsUnflaggedEnumType() &&
!type.HasJsonConverterAttribute<StringEnumConverter>() &&
!type.HasJsonConverterAttribute<StringEnumConverter, JsonStringEnumConverter>() &&
Enum.GetUnderlyingType(type) == typeof(short)
;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,7 +29,7 @@ public override bool IsVisitable(Type type)
{
var isVisitable = this.IsVisitable(type, TypeCode.Int32) &&
type.IsUnflaggedEnumType() &&
!type.HasJsonConverterAttribute<StringEnumConverter>() &&
!type.HasJsonConverterAttribute<StringEnumConverter, JsonStringEnumConverter>() &&
Enum.GetUnderlyingType(type) == typeof(int)
;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -29,7 +29,7 @@ public override bool IsVisitable(Type type)
{
var isVisitable = this.IsVisitable(type, TypeCode.Int64) &&
type.IsUnflaggedEnumType() &&
!type.HasJsonConverterAttribute<StringEnumConverter>() &&
!type.HasJsonConverterAttribute<StringEnumConverter, JsonStringEnumConverter>() &&
Enum.GetUnderlyingType(type) == typeof(long)
;

Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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<StringEnumConverter>()
type.HasJsonConverterAttribute<StringEnumConverter, JsonStringEnumConverter>();
;

return isVisitable;
Expand Down