Skip to content

Fix GetUnderlyingType() can't get Type when Type is NestedCollection #611

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 5 commits 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 @@ -460,8 +460,8 @@ public static Type GetUnderlyingType(this Type type)
}

if (type.IsOpenApiArray())
{
underlyingType = type.GetElementType() ?? type.GetGenericArguments()[0];
{
underlyingType = type.GetElementType() ?? (type.GetGenericArguments() != null && type.GetGenericArguments().Length > 0 ? type.GetGenericArguments()[0] : (type.BaseType.GetGenericArguments() != null && type.BaseType.GetGenericArguments().Length > 0 ? type.BaseType.GetGenericArguments()[0] :type.GetInterfaces()[0].GetGenericArguments()[0])) ;
}

if (type.IsOpenApiDictionary())
Expand Down Expand Up @@ -557,7 +557,7 @@ public static Type GetOpenApiSubType(this Type type)

if (type.IsArrayType())
{
return type.GetGenericArguments()[0];
return type.GetGenericArguments() != null && type.GetGenericArguments().Length > 0 ? type.GetGenericArguments()[0] : (type.BaseType.GetGenericArguments() != null && type.BaseType.GetGenericArguments().Length > 0 ? type.BaseType.GetGenericArguments()[0] : type.GetInterfaces()[0].GetGenericArguments()[0]);
}

return null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public void Given_OpenApiDocument_Then_It_Should_Return_ComponentSchema(string @
[DataRow("arrayObjectModel", "object", "decimalValue", "array")]
[DataRow("arrayObjectModel", "object", "stringObjectValue", "array")]
[DataRow("arrayObjectModel", "object", "objectArrayValue", "array")]
[DataRow("arrayObjectModel", "object", "nestedCollectionValue", "array")]
public void Given_OpenApiDocument_Then_It_Should_Return_ComponentSchemaProperty(string @ref, string refType, string propertyName, string propertyType)
{
var properties = this._doc["components"]["schemas"][@ref]["properties"];
Expand Down Expand Up @@ -107,6 +108,7 @@ public void Given_OpenApiDocument_Then_It_Should_Return_ComponentSchemaPropertyI
[DataTestMethod]
[DataRow("arrayObjectModel", "object", "stringObjectValue", "array", "stringObjectModel")]
[DataRow("arrayObjectModel", "object", "objectArrayValue", "array", "list_object")]
[DataRow("arrayObjectModel", "object", "nestedCollectionValue", "array", "collection_userDefine")]
public void Given_OpenApiDocument_Then_It_Should_Return_ComponentSchemaPropertyItemReference(string @ref, string refType, string propertyName, string propertyType, string itemRef)
{
var items = this._doc["components"]["schemas"][@ref]["properties"][propertyName]["items"];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.TestApp.Models
{
Expand All @@ -14,5 +15,11 @@ public class ArrayObjectModel
public ISet<StringObjectModel> StringObjectValue { get; set; }

public List<object[]> ObjectArrayValue { get; set; }

public Collection_Udf NestedCollectionValue { get; set; }
}

public class UserDefine { }

public class Collection_Udf : Collection<Collection<UserDefine>> { }
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text;

namespace Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Tests.Fakes
{
public class FakeAliasInheritanceModel : Collection<Collection<FakeModel>>
{

}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using System;
using System.Collections.Generic;

using System.Collections.ObjectModel;
using FluentAssertions;

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
Expand Down Expand Up @@ -119,6 +119,7 @@ public void Given_NullableType_When_GetUnderlyingType_Invoked_Then_It_Should_Ret
[DataRow(typeof(List<int>), typeof(int))]
[DataRow(typeof(List<bool>), typeof(bool))]
[DataRow(typeof(List<FakeModel>), typeof(FakeModel))]
[DataRow(typeof(FakeAliasInheritanceModel), typeof(Collection<FakeModel>))]
public void Given_ListType_When_GetUnderlyingType_Invoked_Then_It_Should_Return_Result(Type type, Type expected)
{
var result = TypeExtensions.GetUnderlyingType(type);
Expand Down