Browse Source

Revised api description model.

pull/3138/head
Halil İbrahim Kalkan 7 years ago
parent
commit
224494d0aa
  1. 4
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApiExploring/AbpApiDefinitionController.cs
  2. 21
      framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs
  3. 2
      framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs
  4. 7
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ApplicationApiDescriptionModelRequestDto.cs
  5. 8
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ControllerApiDescriptionModel.cs
  6. 4
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ControllerInterfaceApiDescriptionModel.cs
  7. 2
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/IApiDescriptionModelProvider.cs
  8. 7
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/MethodParameterApiDescriptionModel.cs
  9. 127
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ModelingTypeHelper.cs
  10. 7
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs
  11. 7
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs
  12. 9
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ReturnValueApiDescriptionModel.cs
  13. 4
      framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/TypeApiDescriptionModel.cs
  14. 4
      framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs
  15. 2
      framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptManager.cs

4
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/ApiExploring/AbpApiDefinitionController.cs

@ -14,9 +14,9 @@ namespace Volo.Abp.AspNetCore.Mvc.ApiExploring
} }
[HttpGet] [HttpGet]
public ApplicationApiDescriptionModel Get() public ApplicationApiDescriptionModel Get(ApplicationApiDescriptionModelRequestDto model)
{ {
return _modelProvider.CreateApiModel(); return _modelProvider.CreateApiModel(model);
} }
} }
} }

21
framework/src/Volo.Abp.AspNetCore.Mvc/Volo/Abp/AspNetCore/Mvc/AspNetCoreApiDescriptionModelProvider.cs

@ -13,6 +13,7 @@ using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Logging.Abstractions; using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options; using Microsoft.Extensions.Options;
using Volo.Abp.Application.Services; using Volo.Abp.Application.Services;
using Volo.Abp.AspNetCore.Mvc.ApiExploring;
using Volo.Abp.AspNetCore.Mvc.Conventions; using Volo.Abp.AspNetCore.Mvc.Conventions;
using Volo.Abp.AspNetCore.Mvc.Utils; using Volo.Abp.AspNetCore.Mvc.Utils;
using Volo.Abp.DependencyInjection; using Volo.Abp.DependencyInjection;
@ -42,7 +43,7 @@ namespace Volo.Abp.AspNetCore.Mvc
Logger = NullLogger<AspNetCoreApiDescriptionModelProvider>.Instance; Logger = NullLogger<AspNetCoreApiDescriptionModelProvider>.Instance;
} }
public ApplicationApiDescriptionModel CreateApiModel() public ApplicationApiDescriptionModel CreateApiModel(ApplicationApiDescriptionModelRequestDto input)
{ {
//TODO: Can cache the model? //TODO: Can cache the model?
@ -57,14 +58,17 @@ namespace Volo.Abp.AspNetCore.Mvc
continue; continue;
} }
AddApiDescriptionToModel(apiDescription, model); AddApiDescriptionToModel(apiDescription, model, input);
} }
} }
return model; return model;
} }
private void AddApiDescriptionToModel(ApiDescription apiDescription, ApplicationApiDescriptionModel applicationModel) private void AddApiDescriptionToModel(
ApiDescription apiDescription,
ApplicationApiDescriptionModel applicationModel,
ApplicationApiDescriptionModelRequestDto input)
{ {
var controllerType = apiDescription.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType(); var controllerType = apiDescription.ActionDescriptor.AsControllerActionDescriptor().ControllerTypeInfo.AsType();
var setting = FindSetting(controllerType); var setting = FindSetting(controllerType);
@ -92,7 +96,10 @@ namespace Volo.Abp.AspNetCore.Mvc
GetSupportedVersions(controllerType, method, setting) GetSupportedVersions(controllerType, method, setting)
)); ));
AddCustomTypesToModel(applicationModel, method); if (input.IncludeTypes)
{
AddCustomTypesToModel(applicationModel, method);
}
AddParameterDescriptionsToModel(actionModel, method, apiDescription); AddParameterDescriptionsToModel(actionModel, method, apiDescription);
} }
@ -199,15 +206,15 @@ namespace Volo.Abp.AspNetCore.Mvc
/* TODO: Add interfaces /* TODO: Add interfaces
*/ */
var typeAsString = type.FullName; var typeName = ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(type);
if (applicationModel.Types.ContainsKey(typeAsString)) if (applicationModel.Types.ContainsKey(typeName))
{ {
return; return;
} }
var typeModel = TypeApiDescriptionModel.Create(type); var typeModel = TypeApiDescriptionModel.Create(type);
applicationModel.Types[typeAsString] = typeModel; applicationModel.Types[typeName] = typeModel;
AddCustomTypesToModel(applicationModel, type.BaseType); AddCustomTypesToModel(applicationModel, type.BaseType);

2
framework/src/Volo.Abp.Http.Client/Volo/Abp/Http/Client/DynamicProxying/ApiDescriptionFinder.cs

@ -57,7 +57,7 @@ namespace Volo.Abp.Http.Client.DynamicProxying
for (int i = 0; i < methodParameters.Length; i++) for (int i = 0; i < methodParameters.Length; i++)
{ {
if (action.ParametersOnMethod[i].TypeAsString != methodParameters[i].ParameterType.FullName) if (action.ParametersOnMethod[i].Type != ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(methodParameters[i].ParameterType))
{ {
found = false; found = false;
break; break;

7
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ApplicationApiDescriptionModelRequestDto.cs

@ -0,0 +1,7 @@
namespace Volo.Abp.Http.Modeling
{
public class ApplicationApiDescriptionModelRequestDto
{
public bool IncludeTypes { get; set; }
}
}

8
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ControllerApiDescriptionModel.cs

@ -10,7 +10,7 @@ namespace Volo.Abp.Http.Modeling
{ {
public string ControllerName { get; set; } public string ControllerName { get; set; }
public string TypeAsString { get; set; } public string Type { get; set; }
public List<ControllerInterfaceApiDescriptionModel> Interfaces { get; set; } public List<ControllerInterfaceApiDescriptionModel> Interfaces { get; set; }
@ -26,7 +26,7 @@ namespace Volo.Abp.Http.Modeling
return new ControllerApiDescriptionModel return new ControllerApiDescriptionModel
{ {
ControllerName = controllerName, ControllerName = controllerName,
TypeAsString = type.FullName, Type = type.FullName,
Actions = new Dictionary<string, ActionApiDescriptionModel>(), Actions = new Dictionary<string, ActionApiDescriptionModel>(),
Interfaces = type Interfaces = type
.GetInterfaces() .GetInterfaces()
@ -52,7 +52,7 @@ namespace Volo.Abp.Http.Modeling
{ {
var subModel = new ControllerApiDescriptionModel var subModel = new ControllerApiDescriptionModel
{ {
TypeAsString = TypeAsString, Type = Type,
Interfaces = Interfaces, Interfaces = Interfaces,
ControllerName = ControllerName, ControllerName = ControllerName,
Actions = new Dictionary<string, ActionApiDescriptionModel>() Actions = new Dictionary<string, ActionApiDescriptionModel>()
@ -71,7 +71,7 @@ namespace Volo.Abp.Http.Modeling
public bool Implements(Type interfaceType) public bool Implements(Type interfaceType)
{ {
return Interfaces.Any(i => i.TypeAsString == interfaceType.FullName); return Interfaces.Any(i => i.Type == interfaceType.FullName);
} }
public override string ToString() public override string ToString()

4
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ControllerInterfaceApiDescriptionModel.cs

@ -5,7 +5,7 @@ namespace Volo.Abp.Http.Modeling
[Serializable] [Serializable]
public class ControllerInterfaceApiDescriptionModel public class ControllerInterfaceApiDescriptionModel
{ {
public string TypeAsString { get; set; } public string Type { get; set; }
private ControllerInterfaceApiDescriptionModel() private ControllerInterfaceApiDescriptionModel()
{ {
@ -16,7 +16,7 @@ namespace Volo.Abp.Http.Modeling
{ {
return new ControllerInterfaceApiDescriptionModel return new ControllerInterfaceApiDescriptionModel
{ {
TypeAsString = type.FullName Type = type.FullName
}; };
} }
} }

2
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/IApiDescriptionModelProvider.cs

@ -2,6 +2,6 @@ namespace Volo.Abp.Http.Modeling
{ {
public interface IApiDescriptionModelProvider public interface IApiDescriptionModelProvider
{ {
ApplicationApiDescriptionModel CreateApiModel(); ApplicationApiDescriptionModel CreateApiModel(ApplicationApiDescriptionModelRequestDto input);
} }
} }

7
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/MethodParameterApiDescriptionModel.cs

@ -8,7 +8,9 @@ namespace Volo.Abp.Http.Modeling
{ {
public string Name { get; set; } public string Name { get; set; }
public string TypeAsString { get; set; } public string Type { get; set; }
public string TypeSimple { get; set; }
public bool IsOptional { get; set; } public bool IsOptional { get; set; }
@ -24,7 +26,8 @@ namespace Volo.Abp.Http.Modeling
return new MethodParameterApiDescriptionModel return new MethodParameterApiDescriptionModel
{ {
Name = parameterInfo.Name, Name = parameterInfo.Name,
TypeAsString = parameterInfo.ParameterType.FullName, Type = ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(parameterInfo.ParameterType),
TypeSimple = ModelingTypeHelper.GetSimplifiedName(parameterInfo.ParameterType),
IsOptional = parameterInfo.IsOptional, IsOptional = parameterInfo.IsOptional,
DefaultValue = parameterInfo.HasDefaultValue ? parameterInfo.DefaultValue : null DefaultValue = parameterInfo.HasDefaultValue ? parameterInfo.DefaultValue : null
}; };

127
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ModelingTypeHelper.cs

@ -0,0 +1,127 @@
using System;
using System.Collections.Generic;
using System.Linq;
using JetBrains.Annotations;
namespace Volo.Abp.Http.Modeling
{
public static class ModelingTypeHelper
{
public static string GetFullNameHandlingNullableAndGenerics([NotNull] Type type)
{
Check.NotNull(type, nameof(type));
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return type.GenericTypeArguments[0].FullName + "?";
}
if (type.IsGenericType)
{
var genericType = type.GetGenericTypeDefinition();
return $"{genericType.FullName.Left(genericType.FullName.IndexOf('`'))}<{type.GenericTypeArguments.Select(GetFullNameHandlingNullableAndGenerics).JoinAsString(",")}>";
}
return type.FullName;
}
public static string GetSimplifiedName([NotNull] Type type)
{
Check.NotNull(type, nameof(type));
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return GetSimplifiedName(type.GenericTypeArguments[0]) + "?";
}
if (type.IsGenericType)
{
var genericType = type.GetGenericTypeDefinition();
return $"{genericType.FullName.Left(genericType.FullName.IndexOf('`'))}<{type.GenericTypeArguments.Select(GetSimplifiedName).JoinAsString(",")}>";
}
if (type == typeof(string))
{
return "string";
}
else if (type == typeof(int))
{
return "number";
}
else if (type == typeof(long))
{
return "number";
}
else if (type == typeof(bool))
{
return "boolean";
}
else if (type == typeof(char))
{
return "string";
}
else if (type == typeof(double))
{
return "number";
}
else if (type == typeof(float))
{
return "number";
}
else if (type == typeof(decimal))
{
return "number";
}
else if (type == typeof(DateTime))
{
return "string";
}
else if (type == typeof(DateTimeOffset))
{
return "string";
}
else if (type == typeof(TimeSpan))
{
return "string";
}
else if (type == typeof(Guid))
{
return "string";
}
else if (type == typeof(byte))
{
return "number";
}
else if (type == typeof(sbyte))
{
return "number";
}
else if (type == typeof(short))
{
return "number";
}
else if (type == typeof(ushort))
{
return "number";
}
else if (type == typeof(uint))
{
return "number";
}
else if (type == typeof(ulong))
{
return "number";
}
else if (type == typeof(IntPtr))
{
return "number";
}
else if (type == typeof(UIntPtr))
{
return "number";
}
return type.FullName;
}
}
}

7
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ParameterApiDescriptionModel.cs

@ -9,7 +9,9 @@ namespace Volo.Abp.Http.Modeling
public string Name { get; set; } public string Name { get; set; }
public string TypeAsString { get; set; } public string Type { get; set; }
public string TypeSimple { get; set; }
public bool IsOptional { get; set; } public bool IsOptional { get; set; }
@ -30,7 +32,8 @@ namespace Volo.Abp.Http.Modeling
{ {
Name = name, Name = name,
NameOnMethod = nameOnMethod, NameOnMethod = nameOnMethod,
TypeAsString = type?.FullName, Type = type != null ? ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(type) : null,
TypeSimple = type != null ? ModelingTypeHelper.GetSimplifiedName(type) : null,
IsOptional = isOptional, IsOptional = isOptional,
DefaultValue = defaultValue, DefaultValue = defaultValue,
ConstraintTypes = constraintTypes, ConstraintTypes = constraintTypes,

7
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/PropertyApiDescriptionModel.cs

@ -8,7 +8,9 @@ namespace Volo.Abp.Http.Modeling
{ {
public string Name { get; set; } public string Name { get; set; }
public string TypeAsString { get; set; } public string Type { get; set; }
public string TypeSimple { get; set; }
//TODO: Validation rules for this property //TODO: Validation rules for this property
public static PropertyApiDescriptionModel Create(PropertyInfo propertyInfo) public static PropertyApiDescriptionModel Create(PropertyInfo propertyInfo)
@ -16,7 +18,8 @@ namespace Volo.Abp.Http.Modeling
return new PropertyApiDescriptionModel return new PropertyApiDescriptionModel
{ {
Name = propertyInfo.Name, Name = propertyInfo.Name,
TypeAsString = propertyInfo.PropertyType.FullName Type = ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(propertyInfo.PropertyType),
TypeSimple = ModelingTypeHelper.GetSimplifiedName(propertyInfo.PropertyType)
}; };
} }
} }

9
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/ReturnValueApiDescriptionModel.cs

@ -6,7 +6,9 @@ namespace Volo.Abp.Http.Modeling
[Serializable] [Serializable]
public class ReturnValueApiDescriptionModel public class ReturnValueApiDescriptionModel
{ {
public string TypeAsString { get; set; } public string Type { get; set; }
public string TypeSimple { get; set; }
private ReturnValueApiDescriptionModel() private ReturnValueApiDescriptionModel()
{ {
@ -15,9 +17,12 @@ namespace Volo.Abp.Http.Modeling
public static ReturnValueApiDescriptionModel Create(Type type) public static ReturnValueApiDescriptionModel Create(Type type)
{ {
var unwrappedType = AsyncHelper.UnwrapTask(type);
return new ReturnValueApiDescriptionModel return new ReturnValueApiDescriptionModel
{ {
TypeAsString = AsyncHelper.UnwrapTask(type).FullName Type = ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(unwrappedType),
TypeSimple = ModelingTypeHelper.GetSimplifiedName(unwrappedType)
}; };
} }
} }

4
framework/src/Volo.Abp.Http/Volo/Abp/Http/Modeling/TypeApiDescriptionModel.cs

@ -6,7 +6,7 @@ namespace Volo.Abp.Http.Modeling
[Serializable] [Serializable]
public class TypeApiDescriptionModel public class TypeApiDescriptionModel
{ {
public string BaseTypeAsString { get; set; } public string BaseType { get; set; }
public bool IsEnum { get; set; } public bool IsEnum { get; set; }
@ -32,7 +32,7 @@ namespace Volo.Abp.Http.Modeling
var typeModel = new TypeApiDescriptionModel var typeModel = new TypeApiDescriptionModel
{ {
IsEnum = type.IsEnum, IsEnum = type.IsEnum,
BaseTypeAsString = baseType?.FullName BaseType = baseType != null ? ModelingTypeHelper.GetFullNameHandlingNullableAndGenerics(baseType) : null
}; };
if (typeModel.IsEnum) if (typeModel.IsEnum)

4
framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/Generators/JQuery/JQueryProxyScriptGenerator.cs

@ -56,7 +56,7 @@ namespace Volo.Abp.Http.ProxyScripting.Generators.JQuery
private static void AddControllerScript(StringBuilder script, ControllerApiDescriptionModel controller) private static void AddControllerScript(StringBuilder script, ControllerApiDescriptionModel controller)
{ {
var controllerName = GetNormalizedTypeName(controller.TypeAsString); var controllerName = GetNormalizedTypeName(controller.Type);
script.AppendLine($" // controller {controllerName}"); script.AppendLine($" // controller {controllerName}");
script.AppendLine(); script.AppendLine();
@ -125,7 +125,7 @@ namespace Volo.Abp.Http.ProxyScripting.Generators.JQuery
script.AppendLine(" url: abp.appPath + '" + ProxyScriptingHelper.GenerateUrlWithParameters(action) + "',"); script.AppendLine(" url: abp.appPath + '" + ProxyScriptingHelper.GenerateUrlWithParameters(action) + "',");
script.Append(" type: '" + httpMethod + "'"); script.Append(" type: '" + httpMethod + "'");
if (action.ReturnValue.TypeAsString == typeof(void).FullName) if (action.ReturnValue.Type == typeof(void).FullName)
{ {
script.AppendLine(","); script.AppendLine(",");
script.Append(" dataType: null"); script.Append(" dataType: null");

2
framework/src/Volo.Abp.Http/Volo/Abp/Http/ProxyScripting/ProxyScriptManager.cs

@ -48,7 +48,7 @@ namespace Volo.Abp.Http.ProxyScripting
private string CreateScript(ProxyScriptingModel scriptingModel) private string CreateScript(ProxyScriptingModel scriptingModel)
{ {
var apiModel = _modelProvider.CreateApiModel(); var apiModel = _modelProvider.CreateApiModel(new ApplicationApiDescriptionModelRequestDto {IncludeTypes = false});
if (scriptingModel.IsPartialRequest()) if (scriptingModel.IsPartialRequest())
{ {

Loading…
Cancel
Save