Skip to content

Commit

Permalink
Nested generic types support
Browse files Browse the repository at this point in the history
  • Loading branch information
vstr committed Dec 14, 2024
1 parent 877a1db commit fd6ab1b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 17 deletions.
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

0 comments on commit fd6ab1b

Please sign in to comment.