Browse Source

Handle arrays and dictionaries for type models.

pull/3138/head
Halil İbrahim Kalkan 7 years ago
parent
commit
d8bd4783c0
  1. 8
      framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs
  2. 24
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs

8
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)
{

24
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
};
}
}

Loading…
Cancel
Save