From d8bd4783c00f9a0cf7c14e0e06bf75ee79259ea1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Wed, 20 Nov 2019 16:52:34 +0300 Subject: [PATCH] Handle arrays and dictionaries for type models. --- .../Volo/Abp/Reflection/TypeHelper.cs | 8 ++++++- .../Modeling/PropertyApiDescriptionModel.cs | 24 +++++++++++++++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs index cab8c41b2c..a9f21c5a0d 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs @@ -57,8 +57,14 @@ namespace Volo.Abp.Reflection return t; } - public static bool IsEnumerable(Type type, out Type itemType) + public static bool IsEnumerable(Type type, out Type itemType, bool includePrimitives = true) { + if (!includePrimitives && IsPrimitiveExtended(type)) + { + itemType = null; + return false; + } + var enumerableTypes = ReflectionHelper.GetImplementedGenericTypes(type, typeof(IEnumerable<>)); if (enumerableTypes.Count == 1) { diff --git a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs index 8c6ea9e678..ca8af7f22a 100644 --- a/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs +++ b/framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs @@ -1,5 +1,6 @@ using System; using System.Reflection; +using Volo.Abp.Reflection; namespace Volo.Abp.Http.Modeling { @@ -15,11 +16,30 @@ namespace Volo.Abp.Http.Modeling //TODO: Validation rules for this property public static PropertyApiDescriptionModel Create(PropertyInfo propertyInfo) { + string typeName; + string simpleTypeName; + + if (TypeHelper.IsEnumerable(propertyInfo.PropertyType, out var itemType, includePrimitives: false)) + { + typeName = $"[{ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(itemType)}]"; + simpleTypeName = $"[{ModelingTypeHelper.GetSimplifiedName(itemType)}]"; + } + else if (TypeHelper.IsDictionary(propertyInfo.PropertyType, out var keyType, out var valueType)) + { + typeName = $"{{{ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(keyType)}:{ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(valueType)}}}"; + simpleTypeName = $"{{{ModelingTypeHelper.GetSimplifiedName(keyType)}:{ModelingTypeHelper.GetSimplifiedName(valueType)}}}"; + } + else + { + typeName = ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(propertyInfo.PropertyType); + simpleTypeName = ModelingTypeHelper.GetSimplifiedName(propertyInfo.PropertyType); + } + return new PropertyApiDescriptionModel { Name = propertyInfo.Name, - Type = ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(propertyInfo.PropertyType), - TypeSimple = ModelingTypeHelper.GetSimplifiedName(propertyInfo.PropertyType) + Type = typeName, + TypeSimple = simpleTypeName }; } }