Skip to content
Merged
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 @@ -107,6 +107,13 @@ public static bool IsReferentialType(this Type type)
return isReferential;
}

private static HashSet<Type> jObjects = new HashSet<Type>
{
typeof(JObject),
typeof(JToken),
typeof(JArray),
};

/// <summary>
/// Checks whether the given type is Json.NET related <see cref="JObject"/>, <see cref="JToken"/> or not.
/// </summary>
Expand All @@ -119,12 +126,7 @@ public static bool IsJObjectType(this Type type)
return false;
}

if (type == typeof(JObject))
{
return true;
}

if (type == typeof(JToken))
if (jObjects.Any(p => p == type))
{
return true;
}
Expand Down Expand Up @@ -657,82 +659,31 @@ public static bool HasInterface(this Type type, string interfaceName)

private static bool IsArrayType(this Type type)
{
if (type.BaseType == typeof(Array))
{
return true;
}

if (type.IsGenericTypeOf(typeof(List<>)))
{
return true;
}

if (type.IsGenericTypeOf(typeof(IList<>)))
{
return true;
}

if (type.IsGenericTypeOf(typeof(ICollection<>)))
{
return true;
}

if (type.IsGenericTypeOf(typeof(IEnumerable<>)))
{
return true;
}

if (type.IsGenericTypeOf(typeof(IReadOnlyList<>)))
{
return true;
}

if (type.IsGenericTypeOf(typeof(IReadOnlyCollection<>)))
{
return true;
}

if (type.IsGenericTypeOf(typeof(HashSet<>)))
{
return true;
}

if (type.IsGenericTypeOf(typeof(ISet<>)))
{
return true;
}

return false;
var isArrayType = type.Name.Equals("String", StringComparison.InvariantCultureIgnoreCase) == false &&
type.GetInterfaces()
.Where(p => p.IsInterface)
.Where(p => p.Name.Equals("IEnumerable", StringComparison.InvariantCultureIgnoreCase) == true)
.Any() &&
type.IsJObjectType() == false &&
type.IsDictionaryType() == false;

return isArrayType;
}

private static bool IsDictionaryType(this Type type)
private static HashSet<string> dictionaries = new HashSet<string>
{
if (!type.IsGenericType)
{
return false;
}
"Dictionary`2",
"IDictionary`2",
"IReadOnlyDictionary`2",
"KeyValuePair`2",
};

if (type.Name.Equals("Dictionary`2", StringComparison.CurrentCultureIgnoreCase))
{
return true;
}

if (type.Name.Equals("IDictionary`2", StringComparison.CurrentCultureIgnoreCase))
{
return true;
}

if (type.Name.Equals("IReadOnlyDictionary`2", StringComparison.CurrentCultureIgnoreCase))
{
return true;
}

if (type.Name.Equals("KeyValuePair`2", StringComparison.CurrentCultureIgnoreCase))
{
return true;
}
private static bool IsDictionaryType(this Type type)
{
var isDictionaryType = type.IsGenericType &&
dictionaries.Any(p => type.Name.Equals(p, StringComparison.InvariantCultureIgnoreCase) == true);

return false;
return isDictionaryType;
}

private static bool IsNullableType(this Type type, out Type underlyingType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,57 @@ namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests.Extensions
[TestClass]
public class TypeExtensionsTests
{
[TestMethod]
public void Given_IList_Should_Return_True() =>
typeof(IList<string>).IsOpenApiArray().Should().BeTrue();

[TestMethod]
public void Given_List_Should_Return_True() =>
typeof(List<string>).IsOpenApiArray().Should().BeTrue();
[DataTestMethod]
[DataRow(typeof(string), false)]
[DataRow(typeof(int), false)]
[DataRow(typeof(double), false)]
[DataRow(typeof(bool), false)]
[DataRow(typeof(Array), true)]
[DataRow(typeof(string[]), true)]
[DataRow(typeof(List<string>), true)]
[DataRow(typeof(IList<string>), true)]
[DataRow(typeof(ICollection<string>), true)]
[DataRow(typeof(IEnumerable<string>), true)]
[DataRow(typeof(IReadOnlyList<string>), true)]
[DataRow(typeof(IReadOnlyCollection<string>), true)]
[DataRow(typeof(HashSet<string>), true)]
[DataRow(typeof(ISet<string>), true)]
[DataRow(typeof(Dictionary<string, string>), false)]
[DataRow(typeof(IDictionary<string, string>), false)]
[DataRow(typeof(IReadOnlyDictionary<string, string>), false)]
[DataRow(typeof(KeyValuePair<string, string>), false)]
public void Given_ArrayTypes_When_IsOpenApiArray_Invoked_Then_It_Should_Return_Result(Type type, bool expected)
{
var result = TypeExtensions.IsOpenApiArray(type);

[TestMethod]
public void Given_Array_Method_Should_Return_True() =>
typeof(string[]).IsOpenApiArray().Should().BeTrue();
result.Should().Be(expected);
}

[TestMethod]
public void Given_Object_That_Extends_List_Should_Return_False() =>
typeof(JObject).IsOpenApiArray().Should().BeFalse();
[DataTestMethod]
[DataRow(typeof(string), false)]
[DataRow(typeof(int), false)]
[DataRow(typeof(double), false)]
[DataRow(typeof(bool), false)]
[DataRow(typeof(Array), false)]
[DataRow(typeof(string[]), false)]
[DataRow(typeof(List<string>), false)]
[DataRow(typeof(IList<string>), false)]
[DataRow(typeof(ICollection<string>), false)]
[DataRow(typeof(IEnumerable<string>), false)]
[DataRow(typeof(IReadOnlyList<string>), false)]
[DataRow(typeof(IReadOnlyCollection<string>), false)]
[DataRow(typeof(HashSet<string>), false)]
[DataRow(typeof(ISet<string>), false)]
[DataRow(typeof(Dictionary<string, string>), true)]
[DataRow(typeof(IDictionary<string, string>), true)]
[DataRow(typeof(IReadOnlyDictionary<string, string>), true)]
[DataRow(typeof(KeyValuePair<string, string>), true)]
public void Given_DictionaryTypes_When_IsOpenApiDictionary_Invoked_Then_It_Should_Return_Result(Type type, bool expected)
{
var result = TypeExtensions.IsOpenApiDictionary(type);

[TestMethod]
public void Given_String_Method_Should_Return_False() =>
typeof(string).IsOpenApiArray().Should().BeFalse();
result.Should().Be(expected);
}

[TestMethod]
public void Given_DefaultNamingStrategy_When_GetOpenApiTypeName_Invoked_Then_It_Should_Return_Result()
Expand Down