Skip to content
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

Nested generic types support #678

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 @@ -402,19 +402,22 @@ public static string GetOpenApiReferenceId(this Type type, bool isDictionary, bo

if (isDictionary)
{
var name = type.Name.EndsWith("[]") ? "Dictionary_" + type.GetOpenApiSubTypeName(namingStrategy) : type.Name.Split('`').First() + "_" + type.GetOpenApiSubTypeName(namingStrategy);
var name = type.Name.EndsWith("[]")
? "Dictionary_" + type.GetOpenApiTypeName(namingStrategy)
: type.GetOpenApiTypeName(namingStrategy);
return namingStrategy.GetPropertyName(name, hasSpecifiedName: false);
}
if (isList)
{
var name = type.Name.EndsWith("[]") ? "List_" + type.GetOpenApiSubTypeName(namingStrategy) : type.Name.Split('`').First() + "_" + type.GetOpenApiSubTypeName(namingStrategy);
var name = type.Name.EndsWith("[]")
? "List_" + type.GetOpenApiTypeName(namingStrategy)
: type.GetOpenApiTypeName(namingStrategy);
return namingStrategy.GetPropertyName(name, hasSpecifiedName: false);
}

if (type.IsGenericType)
{
return namingStrategy.GetPropertyName(type.Name.Split('`').First(), false) + "_" +
string.Join("_", type.GenericTypeArguments.Select(a => namingStrategy.GetPropertyName(a.Name, false)));
return type.GetOpenApiTypeName(namingStrategy);
}

return namingStrategy.GetPropertyName(type.Name, hasSpecifiedName: false);
Expand Down Expand Up @@ -532,8 +535,7 @@ public static string GetOpenApiTypeName(this Type type, NamingStrategy namingStr
namingStrategy = new DefaultNamingStrategy();
}

var typeName = type.IsGenericType ? type.GetOpenApiGenericRootName() : type.Name;
var name = namingStrategy.GetPropertyName(typeName, hasSpecifiedName: false);
var name = string.Join("_", GetTypeNames(type, namingStrategy));

return name;
}
Expand Down Expand Up @@ -757,5 +759,32 @@ private static bool IsNullableType(this Type type, out Type underlyingType)

return !underlyingType.IsNullOrDefault();
}

private static IEnumerable<string> GetTypeNames(Type type, NamingStrategy namingStrategy)
{
if (type.IsGenericType)
{
yield return namingStrategy.GetPropertyName(GetOpenApiGenericRootName(type), false);

foreach (var argType in type.GetGenericArguments())
{
foreach (var name in GetTypeNames(argType, namingStrategy))
{
yield return namingStrategy.GetPropertyName(name, false);
}
}
}
else
{
if (type.Name.EndsWith("[]"))
{
yield return namingStrategy.GetPropertyName(type.Name.Substring(0, type.Name.Length - 2), false);
}
else
{
yield return namingStrategy.GetPropertyName(type.Name, false);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;

using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Abstractions;
using Microsoft.Azure.WebJobs.Extensions.OpenApi.Core.Extensions;
using Microsoft.OpenApi.Models;

using Newtonsoft.Json.Serialization;
Expand Down Expand Up @@ -31,11 +32,7 @@ public override bool IsVisitable(Type type)
/// <inheritdoc />
public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type, NamingStrategy namingStrategy, params Attribute[] attributes)
{
var title = type.Value.IsGenericType
? namingStrategy.GetPropertyName(type.Value.Name.Split('`').First(), hasSpecifiedName: false) + "_" +
string.Join("_",
type.Value.GenericTypeArguments.Select(a => namingStrategy.GetPropertyName(a.Name, false)))
: namingStrategy.GetPropertyName(type.Value.Name, hasSpecifiedName: false);
var title = type.Value.GetOpenApiTypeName(namingStrategy);
this.Visit(acceptor, name: type.Key, title: title, dataType: "object", dataFormat: null, attributes: attributes);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,7 @@ public override bool IsVisitable(Type type)
/// <inheritdoc />
public override void Visit(IAcceptor acceptor, KeyValuePair<string, Type> type, NamingStrategy namingStrategy, params Attribute[] attributes)
{
var title = type.Value.IsGenericType
? namingStrategy.GetPropertyName(type.Value.Name.Split('`').First(), hasSpecifiedName: false) + "_" +
string.Join("_",
type.Value.GenericTypeArguments.Select(a => namingStrategy.GetPropertyName(a.Name, false)))
: namingStrategy.GetPropertyName(type.Value.Name, hasSpecifiedName: false);
var title = type.Value.GetOpenApiTypeName(namingStrategy);
var name = this.Visit(acceptor, name: type.Key, title: title, dataType: "object", dataFormat: null, attributes: attributes);

if (name.IsNullOrWhiteSpace())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,11 @@ public void Given_Type_When_IsPayloadVisitable_Invoked_Then_It_Should_Return_Res
[DataRow(typeof(IReadOnlyDictionary<string, string>), "object", null, "string", false, "string", 0)]
[DataRow(typeof(KeyValuePair<string, string>), "object", null, "string", false, "string", 0)]
[DataRow(typeof(Dictionary<string, FakeModel>), "object", null, "object", true, "fakeModel", 1)]
[DataRow(typeof(Dictionary<string, string[]>), "object", null, "array", true, "list_string", 1)] //
[DataRow(typeof(Dictionary<string, string[]>), "object", null, "array", true, "list_string", 1)]
[DataRow(typeof(IDictionary<string, FakeModel>), "object", null, "object", true, "fakeModel", 1)]
[DataRow(typeof(IReadOnlyDictionary<string, FakeModel>), "object", null, "object", true, "fakeModel", 1)]
[DataRow(typeof(KeyValuePair<string, FakeModel>), "object", null, "object", true, "fakeModel", 1)]
[DataRow(typeof(KeyValuePair<string, List<FakeGenericModel<FakeModel>>>), "object", null, "array", true, "list_fakeGenericModel_fakeModel", 1)]
public void Given_Type_When_Visit_Invoked_Then_It_Should_Return_Result(Type dictionaryType, string dataType, string dataFormat, string additionalPropertyType, bool isReferential, string referenceId, int expected)
{
var name = "hello";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ public void Given_Type_When_IsPayloadVisitable_Invoked_Then_It_Should_Return_Res
[DataRow(typeof(FakeModel), "object", null, 3, 3, "fakeModel")]
[DataRow(typeof(FakeRequiredModel), "object", null, 1, 0, "fakeRequiredModel")]
[DataRow(typeof(FakeRecursiveModel), "object", null, 3, 2, "fakeRecursiveModel")]
[DataRow(typeof(FakeGenericModel<List<FakeModel>>), "object", null, 0, 4, "fakeGenericModel_list_fakeModel")]
public void Given_Type_When_Visit_Invoked_Then_It_Should_Return_Result(Type objectType, string dataType, string dataFormat, int requiredCount, int rootSchemaCount, string referenceId)
{
var name = "hello";
Expand Down