Skip to content

Commit

Permalink
Merge pull request #14429 from abpframework/Proxy-Contracts
Browse files Browse the repository at this point in the history
Generate DTOs in the client-side for static c# proxy generation.
  • Loading branch information
realLiangshiwei authored Nov 1, 2022
2 parents ff43bdc + 4b62d77 commit 2bae1cc
Show file tree
Hide file tree
Showing 8 changed files with 444 additions and 165 deletions.
5 changes: 3 additions & 2 deletions framework/src/Volo.Abp.Cli.Core/Volo/Abp/Cli/CliUrls.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using Volo.Abp.Http.Modeling;

namespace Volo.Abp.Cli;

Expand Down Expand Up @@ -26,9 +27,9 @@ public static string GetNuGetPackageInfoUrl(string apiKey, string packageId)
return $"{NuGetRootPath}{apiKey}/v3/package/{packageId}/index.json";
}

public static string GetApiDefinitionUrl(string url)
public static string GetApiDefinitionUrl(string url, ApplicationApiDescriptionModelRequestDto model = null)
{
url = url.EnsureEndsWith('/');
return $"{url}api/abp/api-definition";
return $"{url}api/abp/api-definition{(model != null ? model.IncludeTypes ? "?includeTypes=true" : string.Empty : string.Empty)}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,10 @@ private GenerateProxyArgs BuildArgs(CommandLineArgs commandLineArgs)
var source = commandLineArgs.Options.GetOrNull(Options.Source.Short, Options.Source.Long);
var workDirectory = commandLineArgs.Options.GetOrNull(Options.WorkDirectory.Short, Options.WorkDirectory.Long) ?? Directory.GetCurrentDirectory();
var folder = commandLineArgs.Options.GetOrNull(Options.Folder.Long);
var withoutContracts = commandLineArgs.Options.ContainsKey(Options.WithoutContracts.Short) ||
commandLineArgs.Options.ContainsKey(Options.WithoutContracts.Long);

return new GenerateProxyArgs(CommandName, workDirectory, module, url, output, target, apiName, source, folder, commandLineArgs.Options);
return new GenerateProxyArgs(CommandName, workDirectory, module, url, output, target, apiName, source, folder, withoutContracts, commandLineArgs.Options);
}

public virtual string GetUsageInfo()
Expand Down Expand Up @@ -162,5 +164,11 @@ public static class WorkDirectory
public const string Short = "wd";
public const string Long = "working-directory";
}

public static class WithoutContracts
{
public const string Short = "c";
public const string Long = "without-contracts";
}
}
}

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public class GenerateProxyArgs

public string Folder { get; }

public bool WithoutContracts { get; }

[NotNull]
public Dictionary<string, string> ExtraProperties { get; set; }

Expand All @@ -38,6 +40,7 @@ public GenerateProxyArgs(
string apiName,
string source,
string folder,
bool withoutContracts,
Dictionary<string, string> extraProperties = null)
{
CommandName = Check.NotNullOrWhiteSpace(commandName, nameof(commandName));
Expand All @@ -49,6 +52,7 @@ public GenerateProxyArgs(
ApiName = apiName;
Source = source;
Folder = folder;
WithoutContracts = withoutContracts;
ExtraProperties = extraProperties ?? new Dictionary<string, string>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ protected ServiceProxyGeneratorBase(CliHttpClientFactory cliHttpClientFactory, I

public abstract Task GenerateProxyAsync(GenerateProxyArgs args);

protected virtual async Task<ApplicationApiDescriptionModel> GetApplicationApiDescriptionModelAsync(GenerateProxyArgs args)
protected virtual async Task<ApplicationApiDescriptionModel> GetApplicationApiDescriptionModelAsync(GenerateProxyArgs args, ApplicationApiDescriptionModelRequestDto requestDto = null)
{
Check.NotNull(args.Url, nameof(args.Url));

var client = CliHttpClientFactory.CreateClient();

var apiDefinitionResult = await client.GetStringAsync(CliUrls.GetApiDefinitionUrl(args.Url));
var apiDefinitionResult = await client.GetStringAsync(CliUrls.GetApiDefinitionUrl(args.Url, requestDto));
var apiDefinition = JsonSerializer.Deserialize<ApplicationApiDescriptionModel>(apiDefinitionResult);

var moduleDefinition = apiDefinition.Modules.FirstOrDefault(x => string.Equals(x.Key, args.Module, StringComparison.CurrentCultureIgnoreCase)).Value;
Expand All @@ -43,6 +43,7 @@ protected virtual async Task<ApplicationApiDescriptionModel> GetApplicationApiDe
}

var apiDescriptionModel = ApplicationApiDescriptionModel.Create();
apiDescriptionModel.Types = apiDefinition.Types;
apiDescriptionModel.AddModule(moduleDefinition);

return apiDescriptionModel;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ public class ControllerApiDescriptionModel

public bool IsRemoteService { get; set; }

public bool IntegrationService { get; set; }

public string ApiVersion { get; set; }

public string Type { get; set; }
Expand All @@ -34,6 +36,7 @@ public static ControllerApiDescriptionModel Create(string controllerName, string
ControllerName = controllerName,
ControllerGroupName = groupName,
IsRemoteService = isRemoteService,
IntegrationService = IntegrationServiceAttribute.IsDefinedOrInherited(type),
ApiVersion = apiVersion,
Type = type.FullName,
Actions = new Dictionary<string, ActionApiDescriptionModel>(),
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace Volo.Abp.Http.Modeling;

Expand All @@ -7,16 +10,45 @@ public class ControllerInterfaceApiDescriptionModel
{
public string Type { get; set; }

public string Name { get; set; }

public InterfaceMethodApiDescriptionModel[] Methods { get; set; }

public ControllerInterfaceApiDescriptionModel()
{

}

public static ControllerInterfaceApiDescriptionModel Create(Type type)
{
return new ControllerInterfaceApiDescriptionModel
var model = new ControllerInterfaceApiDescriptionModel
{
Type = type.FullName
Type = type.FullName,
Name = type.Name
};

var methods = new List<InterfaceMethodApiDescriptionModel>();

var methodInfos = new List<MethodInfo>();
foreach (var methodInfo in type.GetMethods())
{
methodInfos.Add(methodInfo);
methods.Add(InterfaceMethodApiDescriptionModel.Create(methodInfo));
}

foreach (var @interface in type.GetInterfaces())
{
foreach (var method in @interface.GetMethods())
{
if (!methodInfos.Contains(method))
{
methods.Add(InterfaceMethodApiDescriptionModel.Create(method));
}
methodInfos.Add(method);
}
}

model.Methods = methods.ToArray();
return model;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using JetBrains.Annotations;

namespace Volo.Abp.Http.Modeling;

[Serializable]
public class InterfaceMethodApiDescriptionModel
{
public string Name { get; set; }

public IList<MethodParameterApiDescriptionModel> ParametersOnMethod { get; set; }

public ReturnValueApiDescriptionModel ReturnValue { get; set; }

public InterfaceMethodApiDescriptionModel()
{

}

public static InterfaceMethodApiDescriptionModel Create([NotNull] MethodInfo method)
{
return new InterfaceMethodApiDescriptionModel
{
Name = method.Name,
ReturnValue = ReturnValueApiDescriptionModel.Create(method.ReturnType),
ParametersOnMethod = method
.GetParameters()
.Select(MethodParameterApiDescriptionModel.Create)
.ToList(),
};
}

public override string ToString()
{
return $"[InterfaceMethodApiDescriptionModel {Name}]";
}
}

0 comments on commit 2bae1cc

Please sign in to comment.