diff --git a/WebApiClient.Tools.Swagger/CSharpCode.cs b/WebApiClient.Tools.Swagger/CSharpCode.cs index 5c80989..d2e14af 100644 --- a/WebApiClient.Tools.Swagger/CSharpCode.cs +++ b/WebApiClient.Tools.Swagger/CSharpCode.cs @@ -31,9 +31,8 @@ public CSharpCode(CodeArtifact codeArtifact) /// 类型名称 /// 类型分类 public CSharpCode(string source, string typeName, CodeArtifactType type) - : base(typeName, type, CodeArtifactLanguage.CSharp) + : base(typeName, type, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Undefined, Pretty(source)) { - this.Code = Pretty(source); } /// diff --git a/WebApiClient.Tools.Swagger/HttpApi.cs b/WebApiClient.Tools.Swagger/HttpApi.cs index 2f912ba..b64c376 100644 --- a/WebApiClient.Tools.Swagger/HttpApi.cs +++ b/WebApiClient.Tools.Swagger/HttpApi.cs @@ -14,15 +14,17 @@ namespace WebApiClient.Tools.Swagger [DebuggerDisplay("TypeName = {TypeName}")] public class HttpApi : CSharpControllerTemplateModel { + public string NameSpace { get; } = "WebApiClient"; + /// /// 获取接口名称 /// - public string TypeName { get; private set; } + public string TypeName { get; } /// /// 获取文档描述 /// - public string Summary { get; private set; } + public string Summary { get; } /// /// 获取是否有文档描述 @@ -39,7 +41,7 @@ public bool HasSummary /// swagger操作 /// swagger文档 /// 设置项 - public HttpApi(string className, IEnumerable operations, SwaggerDocument document, HttpApiSettings settings) + public HttpApi(string className, IEnumerable operations, OpenApiDocument document, HttpApiSettings settings) : base(className, operations, document, settings) { var tag = document.Tags @@ -47,6 +49,7 @@ public HttpApi(string className, IEnumerable operations, S this.TypeName = $"I{this.Class}Api"; this.Summary = tag?.Description; + this.NameSpace = settings.NameSpace; } /// diff --git a/WebApiClient.Tools.Swagger/HttpApiMethod.cs b/WebApiClient.Tools.Swagger/HttpApiMethod.cs index 2237abb..972995e 100644 --- a/WebApiClient.Tools.Swagger/HttpApiMethod.cs +++ b/WebApiClient.Tools.Swagger/HttpApiMethod.cs @@ -1,10 +1,9 @@ -using System.Collections.Generic; -using System.Text.RegularExpressions; -using NJsonSchema; +using NJsonSchema; using NJsonSchema.CodeGeneration.CSharp; using NSwag; using NSwag.CodeGeneration.CSharp; using NSwag.CodeGeneration.CSharp.Models; +using System.Text.RegularExpressions; namespace WebApiClient.Tools.Swagger { @@ -20,7 +19,7 @@ public class HttpApiMethod : CSharpOperationModel /// 设置项 /// 代码生成器 /// 语法解析器 - public HttpApiMethod(SwaggerOperation operation, SwaggerToCSharpGeneratorSettings settings, SwaggerToCSharpGeneratorBase generator, CSharpTypeResolver resolver) + public HttpApiMethod(OpenApiOperation operation, CSharpGeneratorBaseSettings settings, CSharpGeneratorBase generator, CSharpTypeResolver resolver) : base(operation, settings, generator, resolver) { } @@ -70,12 +69,12 @@ public override string ActualOperationName /// /// 参数 /// - protected override string ResolveParameterType(SwaggerParameter parameter) + protected override string ResolveParameterType(OpenApiParameter parameter) { var schema = parameter.ActualSchema; if (schema.Type == JsonObjectType.File) { - if (parameter.CollectionFormat == SwaggerParameterCollectionFormat.Multi && !schema.Type.HasFlag(JsonObjectType.Array)) + if (parameter.CollectionFormat == OpenApiParameterCollectionFormat.Multi && !schema.Type.HasFlag(JsonObjectType.Array)) { return "IEnumerable"; } diff --git a/WebApiClient.Tools.Swagger/HttpApiSettings.cs b/WebApiClient.Tools.Swagger/HttpApiSettings.cs index 86bc95a..f5ef500 100644 --- a/WebApiClient.Tools.Swagger/HttpApiSettings.cs +++ b/WebApiClient.Tools.Swagger/HttpApiSettings.cs @@ -14,8 +14,13 @@ namespace WebApiClient.Tools.Swagger /// /// 表示WebApiClient接口设置模型 /// - public class HttpApiSettings : SwaggerToCSharpControllerGeneratorSettings + public class HttpApiSettings : CSharpControllerGeneratorSettings { + /// + /// 获取或设置命名空间 + /// + public string NameSpace { get; set; } = "WebApiClient"; + /// /// WebApiClient接口设置模型 /// @@ -26,7 +31,6 @@ public HttpApiSettings() this.ParameterArrayType = "IEnumerable"; this.ParameterDictionaryType = "IDictionary"; - this.AspNetNamespace = this.GetType().Namespace; this.OperationNameGenerator = new OperationNameProvider(); this.ParameterNameGenerator = new ParameterNameProvider(); this.CSharpGeneratorSettings.TypeNameGenerator = new TypeNameProvider(); @@ -48,7 +52,7 @@ private class OperationNameProvider : MultipleClientsFromOperationIdOperationNam /// /// /// - public override string GetClientName(SwaggerDocument document, string path, SwaggerOperationMethod httpMethod, SwaggerOperation operation) + public override string GetClientName(OpenApiDocument document, string path, string httpMethod, OpenApiOperation operation) { return operation.Tags.FirstOrDefault(); } @@ -65,7 +69,7 @@ private class ParameterNameProvider : IParameterNameGenerator /// /// /// - public string Generate(SwaggerParameter parameter, IEnumerable allParameters) + public string Generate(OpenApiParameter parameter, IEnumerable allParameters) { if (string.IsNullOrEmpty(parameter.Name)) { @@ -127,7 +131,7 @@ private static string CamelCase(string name) /// private class TypeNameProvider : DefaultTypeNameGenerator { - public override string Generate(JsonSchema4 schema, string typeNameHint, IEnumerable reservedTypeNames) + public override string Generate(JsonSchema schema, string typeNameHint, IEnumerable reservedTypeNames) { var prettyName = PrettyName(typeNameHint); var typeName = base.Generate(schema, prettyName, reservedTypeNames); diff --git a/WebApiClient.Tools.Swagger/HttpModel.cs b/WebApiClient.Tools.Swagger/HttpModel.cs index d1f097f..89449e0 100644 --- a/WebApiClient.Tools.Swagger/HttpModel.cs +++ b/WebApiClient.Tools.Swagger/HttpModel.cs @@ -10,7 +10,7 @@ public class HttpModel : CSharpCode /// /// 获取使用的命名空间 /// - public string AspNetNamespace { get; private set; } + public string NameSapce { get; } /// /// WebApiClient的模型描述 @@ -20,7 +20,7 @@ public class HttpModel : CSharpCode public HttpModel(CodeArtifact codeArtifact, string nameSpace) : base(codeArtifact) { - this.AspNetNamespace = nameSpace; + this.NameSapce = nameSpace; } /// diff --git a/WebApiClient.Tools.Swagger/Properties/launchSettings.json b/WebApiClient.Tools.Swagger/Properties/launchSettings.json index 692f4fc..5a0b13c 100644 --- a/WebApiClient.Tools.Swagger/Properties/launchSettings.json +++ b/WebApiClient.Tools.Swagger/Properties/launchSettings.json @@ -1,8 +1,7 @@ { "profiles": { "WebApiClient.Tools.Swagger": { - "commandName": "Project", - "commandLineArgs": "-s petstore.swagger.json" + "commandName": "Project" } } } \ No newline at end of file diff --git a/WebApiClient.Tools.Swagger/Swagger.cs b/WebApiClient.Tools.Swagger/Swagger.cs index 6b2e75f..646eef5 100644 --- a/WebApiClient.Tools.Swagger/Swagger.cs +++ b/WebApiClient.Tools.Swagger/Swagger.cs @@ -1,4 +1,5 @@ -using NJsonSchema.CodeGeneration.CSharp; +using NJsonSchema.CodeGeneration; +using NJsonSchema.CodeGeneration.CSharp; using NSwag; using NSwag.CodeGeneration; using NSwag.CodeGeneration.CSharp; @@ -21,7 +22,7 @@ public class Swagger /// /// 获取Swagger文档 /// - public SwaggerDocument Document { get; private set; } + public OpenApiDocument Document { get; private set; } /// /// 获取Swagger设置项 @@ -37,7 +38,7 @@ public Swagger(SwaggerOptions options) { if (string.IsNullOrEmpty(options.Namespace) == false) { - this.Settings.AspNetNamespace = options.Namespace; + this.Settings.NameSpace = options.Namespace; this.Settings.CSharpGeneratorSettings.Namespace = options.Namespace; } } @@ -46,12 +47,12 @@ public Swagger(SwaggerOptions options) /// Swagger描述 /// /// Swagger文档 - public Swagger(SwaggerDocument document) + public Swagger(OpenApiDocument document) { this.Document = document; this.Settings = new HttpApiSettings(); - this.resolver = SwaggerToCSharpGeneratorBase + this.resolver = CSharpGeneratorBase .CreateResolverWithExceptionSchema(this.Settings.CSharpGeneratorSettings, document); } @@ -60,16 +61,16 @@ public Swagger(SwaggerDocument document) /// /// /// - private static SwaggerDocument GetDocument(string swagger) + private static OpenApiDocument GetDocument(string swagger) { Console.WriteLine($"正在分析swagger:{swagger}"); if (Uri.TryCreate(swagger, UriKind.Absolute, out var _) == true) { - return SwaggerDocument.FromUrlAsync(swagger).Result; + return OpenApiDocument.FromUrlAsync(swagger).Result; } else { - return SwaggerDocument.FromFileAsync(swagger).Result; + return OpenApiDocument.FromFileAsync(swagger).Result; } } @@ -78,7 +79,7 @@ private static SwaggerDocument GetDocument(string swagger) /// public void GenerateFiles() { - var dir = Path.Combine("output", this.Settings.AspNetNamespace); + var dir = Path.Combine("output", this.Settings.NameSpace); var apisPath = Path.Combine(dir, "HttpApis"); var modelsPath = Path.Combine(dir, "HttpModels"); @@ -108,7 +109,7 @@ public void GenerateFiles() /// /// 表示HttpApi提供者 /// - private class HttpApiProvider : SwaggerToCSharpControllerGenerator + private class HttpApiProvider : CSharpControllerGenerator { /// /// swagger @@ -148,24 +149,24 @@ public HttpApi[] GetHttpApis() /// /// /// - /// /// - protected override string GenerateClientClass(string controllerName, string controllerClassName, IList operations, ClientGeneratorOutputType outputType) + protected override IEnumerable GenerateClientTypes(string controllerName, string controllerClassName, IEnumerable operations) { var model = new HttpApi(controllerClassName, operations, this.swagger.Document, this.swagger.Settings); this.httpApiList.Add(model); - return string.Empty; + return new CodeArtifact[0]; } + /// /// 生成文件 /// 这里不生成 /// - /// - /// + /// + /// /// /// - protected override string GenerateFile(string clientCode, IEnumerable clientClasses, ClientGeneratorOutputType outputType) + protected override string GenerateFile(IEnumerable clientTypes, IEnumerable dtoTypes, ClientGeneratorOutputType outputType) { return string.Empty; } @@ -177,9 +178,9 @@ protected override string GenerateFile(string clientCode, IEnumerable cl /// /// /// - protected override CSharpOperationModel CreateOperationModel(SwaggerOperation operation, ClientGeneratorBaseSettings settings) + protected override CSharpOperationModel CreateOperationModel(OpenApiOperation operation, ClientGeneratorBaseSettings settings) { - return new HttpApiMethod(operation, (SwaggerToCSharpGeneratorSettings)settings, this, (CSharpTypeResolver)Resolver); + return new HttpApiMethod(operation, (CSharpGeneratorBaseSettings)settings, this, (CSharpTypeResolver)Resolver); } } @@ -210,8 +211,7 @@ public HttpModelProvider(Swagger swagger) public HttpModel[] GetHttpModels() { return this.GenerateTypes() - .Artifacts - .Select(item => new HttpModel(item, this.swagger.Settings.AspNetNamespace)) + .Select(item => new HttpModel(item, this.swagger.Settings.NameSpace)) .ToArray(); } } diff --git a/WebApiClient.Tools.Swagger/Views/HttpApi.cshtml b/WebApiClient.Tools.Swagger/Views/HttpApi.cshtml index 9de4d6c..d3e6bb0 100644 --- a/WebApiClient.Tools.Swagger/Views/HttpApi.cshtml +++ b/WebApiClient.Tools.Swagger/Views/HttpApi.cshtml @@ -17,11 +17,11 @@
using WebApiClient;
using WebApiClient.Attributes;
using WebApiClient.DataAnnotations;
-
using WebApiClient.Parameterables;
+
using WebApiClient.Parameterables;
-
namespace @(Model.AspNetNamespace)
+
namespace @(Model.NameSpace)
{
@@ -92,21 +92,21 @@ [Required] } - if (parameter.VariableName != parameter.Name&& parameter.Kind != SwaggerParameterKind.Header) + if (parameter.VariableName != parameter.Name&& parameter.Kind != OpenApiParameterKind.Header) { [AliasAs("@(parameter.Name)")] } - if (parameter.Kind == SwaggerParameterKind.Path || parameter.Kind == SwaggerParameterKind.Query) + if (parameter.Kind == OpenApiParameterKind.Path || parameter.Kind == OpenApiParameterKind.Query) { - var schema = parameter.Schema as NSwag.SwaggerParameter; - if (schema != null && schema.CollectionFormat != SwaggerParameterCollectionFormat.Undefined - && schema.CollectionFormat != SwaggerParameterCollectionFormat.Multi) + var schema = parameter.Schema as NSwag.OpenApiParameter; + if (schema != null && schema.CollectionFormat != OpenApiParameterCollectionFormat.Undefined + && schema.CollectionFormat != OpenApiParameterCollectionFormat.Multi) { [PathQuery(CollectionFormat = CollectionFormat.@(schema.CollectionFormat))] } } - else if (parameter.Kind == SwaggerParameterKind.Header) + else if (parameter.Kind == OpenApiParameterKind.Header) { [Header("@(parameter.Name)")] } @@ -114,11 +114,11 @@ { [XmlContent] } - else if (parameter.Kind == SwaggerParameterKind.Body) + else if (parameter.Kind == OpenApiParameterKind.Body) { [JsonContent] } - else if (parameter.Kind == SwaggerParameterKind.FormData) + else if (parameter.Kind == OpenApiParameterKind.FormData) { if (parameter.IsFile == false) { diff --git a/WebApiClient.Tools.Swagger/Views/HttpModel.cshtml b/WebApiClient.Tools.Swagger/Views/HttpModel.cshtml index f4188ee..f4899e2 100644 --- a/WebApiClient.Tools.Swagger/Views/HttpModel.cshtml +++ b/WebApiClient.Tools.Swagger/Views/HttpModel.cshtml @@ -12,7 +12,7 @@ -
namespace @(Model.AspNetNamespace)
+
namespace @(Model.NameSapce)
{
@foreach (var line in Model.Lines) diff --git a/WebApiClient.Tools.Swagger/WebApiClient.Tools.Swagger.csproj b/WebApiClient.Tools.Swagger/WebApiClient.Tools.Swagger.csproj index 8e3afdc..a207132 100644 --- a/WebApiClient.Tools.Swagger/WebApiClient.Tools.Swagger.csproj +++ b/WebApiClient.Tools.Swagger/WebApiClient.Tools.Swagger.csproj @@ -10,6 +10,10 @@ 将swagger的本地或远程json文件解析生成WebApiClient的接口定义代码文件的工具 + + AnyCPU + + @@ -19,7 +23,7 @@ - +