Skip to content

Commit

Permalink
Introduce ApiTypeNameHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Jul 28, 2020
1 parent 8007029 commit 226d584
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 28 deletions.
4 changes: 4 additions & 0 deletions framework/src/Volo.Abp.Core/Volo/Abp/Reflection/TypeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,10 @@ public static string GetSimplifiedName([NotNull] Type type)
{
return "number";
}
else if (type == typeof(object))
{
return "object";
}

return type.FullName ?? type.Name;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System;
using Volo.Abp.Reflection;

namespace Volo.Abp.Http.Modeling
{
public static class ApiTypeNameHelper
{
public static string GetTypeName(Type type)
{
if (TypeHelper.IsDictionary(type, out var keyType, out var valueType))
{
return $"{{{GetTypeName(keyType)}:{GetTypeName(valueType)}}}";
}

if (TypeHelper.IsEnumerable(type, out var itemType, includePrimitives: false))
{
return $"[{GetTypeName(itemType)}]";
}

return TypeHelper.GetFullNameHandlingNullableAndGenerics(type);
}

public static string GetSimpleTypeName(Type type)
{
if (TypeHelper.IsDictionary(type, out var keyType, out var valueType))
{
return $"{{{GetSimpleTypeName(keyType)}:{GetSimpleTypeName(valueType)}}}";
}

if (TypeHelper.IsEnumerable(type, out var itemType, includePrimitives: false))
{
return $"[{GetSimpleTypeName(itemType)}]";
}

return TypeHelper.GetSimplifiedName(type);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static MethodParameterApiDescriptionModel Create(ParameterInfo parameterI
Name = parameterInfo.Name,
TypeAsString = parameterInfo.ParameterType.GetFullNameWithAssemblyName(),
Type = TypeHelper.GetFullNameHandlingNullableAndGenerics(parameterInfo.ParameterType),
TypeSimple = TypeHelper.GetSimplifiedName(parameterInfo.ParameterType),
TypeSimple = ApiTypeNameHelper.GetSimpleTypeName(parameterInfo.ParameterType),
IsOptional = parameterInfo.IsOptional,
DefaultValue = parameterInfo.HasDefaultValue ? parameterInfo.DefaultValue : null
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ParameterApiDescriptionModel

private ParameterApiDescriptionModel()
{

}

public static ParameterApiDescriptionModel Create(string name, string nameOnMethod, Type type, bool isOptional = false, object defaultValue = null, string[] constraintTypes = null, string bindingSourceId = null, string descriptorName = null)
Expand All @@ -36,7 +36,7 @@ public static ParameterApiDescriptionModel Create(string name, string nameOnMeth
Name = name,
NameOnMethod = nameOnMethod,
Type = type != null ? TypeHelper.GetFullNameHandlingNullableAndGenerics(type) : null,
TypeSimple = type != null ? TypeHelper.GetSimplifiedName(type) : null,
TypeSimple = type != null ? ApiTypeNameHelper.GetSimpleTypeName(type) : null,
IsOptional = isOptional,
DefaultValue = defaultValue,
ConstraintTypes = constraintTypes,
Expand All @@ -45,4 +45,4 @@ public static ParameterApiDescriptionModel Create(string name, string nameOnMeth
};
}
}
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Reflection;
using Volo.Abp.Reflection;

namespace Volo.Abp.Http.Modeling
{
Expand All @@ -16,30 +15,11 @@ public class PropertyApiDescriptionModel
//TODO: Validation rules for this property
public static PropertyApiDescriptionModel Create(PropertyInfo propertyInfo)
{
string typeName;
string simpleTypeName;

if (TypeHelper.IsDictionary(propertyInfo.PropertyType, out var keyType, out var valueType))
{
typeName = $"{{{TypeHelper.GetFullNameHandlingNullableAndGenerics(keyType)}:{TypeHelper.GetFullNameHandlingNullableAndGenerics(valueType)}}}";
simpleTypeName = $"{{{TypeHelper.GetSimplifiedName(keyType)}:{TypeHelper.GetSimplifiedName(valueType)}}}";
}
else if (TypeHelper.IsEnumerable(propertyInfo.PropertyType, out var itemType, includePrimitives: false))
{
typeName = $"[{TypeHelper.GetFullNameHandlingNullableAndGenerics(itemType)}]";
simpleTypeName = $"[{TypeHelper.GetSimplifiedName(itemType)}]";
}
else
{
typeName = TypeHelper.GetFullNameHandlingNullableAndGenerics(propertyInfo.PropertyType);
simpleTypeName = TypeHelper.GetSimplifiedName(propertyInfo.PropertyType);
}

return new PropertyApiDescriptionModel
{
Name = propertyInfo.Name,
Type = typeName,
TypeSimple = simpleTypeName
Type = ApiTypeNameHelper.GetTypeName(propertyInfo.PropertyType),
TypeSimple = ApiTypeNameHelper.GetSimpleTypeName(propertyInfo.PropertyType)
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ public static ReturnValueApiDescriptionModel Create(Type type)
return new ReturnValueApiDescriptionModel
{
Type = TypeHelper.GetFullNameHandlingNullableAndGenerics(unwrappedType),
TypeSimple = TypeHelper.GetSimplifiedName(unwrappedType)
TypeSimple = ApiTypeNameHelper.GetSimpleTypeName(unwrappedType)
};
}
}
}
}

0 comments on commit 226d584

Please sign in to comment.