Skip to content

Commit

Permalink
更新支持openApi3
Browse files Browse the repository at this point in the history
  • Loading branch information
xljiulang committed Aug 28, 2019
1 parent eec2edb commit d90f318
Show file tree
Hide file tree
Showing 10 changed files with 60 additions and 52 deletions.
3 changes: 1 addition & 2 deletions WebApiClient.Tools.Swagger/CSharpCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,8 @@ public CSharpCode(CodeArtifact codeArtifact)
/// <param name="typeName">类型名称</param>
/// <param name="type">类型分类</param>
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);
}

/// <summary>
Expand Down
9 changes: 6 additions & 3 deletions WebApiClient.Tools.Swagger/HttpApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,17 @@ namespace WebApiClient.Tools.Swagger
[DebuggerDisplay("TypeName = {TypeName}")]
public class HttpApi : CSharpControllerTemplateModel
{
public string NameSpace { get; } = "WebApiClient";

/// <summary>
/// 获取接口名称
/// </summary>
public string TypeName { get; private set; }
public string TypeName { get; }

/// <summary>
/// 获取文档描述
/// </summary>
public string Summary { get; private set; }
public string Summary { get; }

/// <summary>
/// 获取是否有文档描述
Expand All @@ -39,14 +41,15 @@ public bool HasSummary
/// <param name="operations">swagger操作</param>
/// <param name="document">swagger文档</param>
/// <param name="settings">设置项</param>
public HttpApi(string className, IEnumerable<CSharpOperationModel> operations, SwaggerDocument document, HttpApiSettings settings)
public HttpApi(string className, IEnumerable<CSharpOperationModel> operations, OpenApiDocument document, HttpApiSettings settings)
: base(className, operations, document, settings)
{
var tag = document.Tags
.FirstOrDefault(item => string.Equals(item.Name, className, StringComparison.OrdinalIgnoreCase));

this.TypeName = $"I{this.Class}Api";
this.Summary = tag?.Description;
this.NameSpace = settings.NameSpace;
}

/// <summary>
Expand Down
11 changes: 5 additions & 6 deletions WebApiClient.Tools.Swagger/HttpApiMethod.cs
Original file line number Diff line number Diff line change
@@ -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
{
Expand All @@ -20,7 +19,7 @@ public class HttpApiMethod : CSharpOperationModel
/// <param name="settings">设置项</param>
/// <param name="generator">代码生成器</param>
/// <param name="resolver">语法解析器</param>
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)
{
}
Expand Down Expand Up @@ -70,12 +69,12 @@ public override string ActualOperationName
/// </summary>
/// <param name="parameter">参数</param>
/// <returns></returns>
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<MulitpartFile>";
}
Expand Down
14 changes: 9 additions & 5 deletions WebApiClient.Tools.Swagger/HttpApiSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ namespace WebApiClient.Tools.Swagger
/// <summary>
/// 表示WebApiClient接口设置模型
/// </summary>
public class HttpApiSettings : SwaggerToCSharpControllerGeneratorSettings
public class HttpApiSettings : CSharpControllerGeneratorSettings
{
/// <summary>
/// 获取或设置命名空间
/// </summary>
public string NameSpace { get; set; } = "WebApiClient";

/// <summary>
/// WebApiClient接口设置模型
/// </summary>
Expand All @@ -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();
Expand All @@ -48,7 +52,7 @@ private class OperationNameProvider : MultipleClientsFromOperationIdOperationNam
/// <param name="httpMethod"></param>
/// <param name="operation"></param>
/// <returns></returns>
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();
}
Expand All @@ -65,7 +69,7 @@ private class ParameterNameProvider : IParameterNameGenerator
/// <param name="parameter"></param>
/// <param name="allParameters"></param>
/// <returns></returns>
public string Generate(SwaggerParameter parameter, IEnumerable<SwaggerParameter> allParameters)
public string Generate(OpenApiParameter parameter, IEnumerable<OpenApiParameter> allParameters)
{
if (string.IsNullOrEmpty(parameter.Name))
{
Expand Down Expand Up @@ -127,7 +131,7 @@ private static string CamelCase(string name)
/// </summary>
private class TypeNameProvider : DefaultTypeNameGenerator
{
public override string Generate(JsonSchema4 schema, string typeNameHint, IEnumerable<string> reservedTypeNames)
public override string Generate(JsonSchema schema, string typeNameHint, IEnumerable<string> reservedTypeNames)
{
var prettyName = PrettyName(typeNameHint);
var typeName = base.Generate(schema, prettyName, reservedTypeNames);
Expand Down
4 changes: 2 additions & 2 deletions WebApiClient.Tools.Swagger/HttpModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public class HttpModel : CSharpCode
/// <summary>
/// 获取使用的命名空间
/// </summary>
public string AspNetNamespace { get; private set; }
public string NameSapce { get; }

/// <summary>
/// WebApiClient的模型描述
Expand All @@ -20,7 +20,7 @@ public class HttpModel : CSharpCode
public HttpModel(CodeArtifact codeArtifact, string nameSpace)
: base(codeArtifact)
{
this.AspNetNamespace = nameSpace;
this.NameSapce = nameSpace;
}

/// <summary>
Expand Down
3 changes: 1 addition & 2 deletions WebApiClient.Tools.Swagger/Properties/launchSettings.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
{
"profiles": {
"WebApiClient.Tools.Swagger": {
"commandName": "Project",
"commandLineArgs": "-s petstore.swagger.json"
"commandName": "Project"
}
}
}
40 changes: 20 additions & 20 deletions WebApiClient.Tools.Swagger/Swagger.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using NJsonSchema.CodeGeneration.CSharp;
using NJsonSchema.CodeGeneration;
using NJsonSchema.CodeGeneration.CSharp;
using NSwag;
using NSwag.CodeGeneration;
using NSwag.CodeGeneration.CSharp;
Expand All @@ -21,7 +22,7 @@ public class Swagger
/// <summary>
/// 获取Swagger文档
/// </summary>
public SwaggerDocument Document { get; private set; }
public OpenApiDocument Document { get; private set; }

/// <summary>
/// 获取Swagger设置项
Expand All @@ -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;
}
}
Expand All @@ -46,12 +47,12 @@ public Swagger(SwaggerOptions options)
/// Swagger描述
/// </summary>
/// <param name="document">Swagger文档</param>
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);
}

Expand All @@ -60,16 +61,16 @@ public Swagger(SwaggerDocument document)
/// </summary>
/// <param name="swagger"></param>
/// <returns></returns>
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;
}
}

Expand All @@ -78,7 +79,7 @@ private static SwaggerDocument GetDocument(string swagger)
/// </summary>
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");

Expand Down Expand Up @@ -108,7 +109,7 @@ public void GenerateFiles()
/// <summary>
/// 表示HttpApi提供者
/// </summary>
private class HttpApiProvider : SwaggerToCSharpControllerGenerator
private class HttpApiProvider : CSharpControllerGenerator
{
/// <summary>
/// swagger
Expand Down Expand Up @@ -148,24 +149,24 @@ public HttpApi[] GetHttpApis()
/// <param name="controllerName"></param>
/// <param name="controllerClassName"></param>
/// <param name="operations"></param>
/// <param name="outputType"></param>
/// <returns></returns>
protected override string GenerateClientClass(string controllerName, string controllerClassName, IList<CSharpOperationModel> operations, ClientGeneratorOutputType outputType)
protected override IEnumerable<CodeArtifact> GenerateClientTypes(string controllerName, string controllerClassName, IEnumerable<CSharpOperationModel> operations)
{
var model = new HttpApi(controllerClassName, operations, this.swagger.Document, this.swagger.Settings);
this.httpApiList.Add(model);
return string.Empty;
return new CodeArtifact[0];
}


/// <summary>
/// 生成文件
/// 这里不生成
/// </summary>
/// <param name="clientCode"></param>
/// <param name="clientClasses"></param>
/// <param name="clientTypes"></param>
/// <param name="dtoTypes"></param>
/// <param name="outputType"></param>
/// <returns></returns>
protected override string GenerateFile(string clientCode, IEnumerable<string> clientClasses, ClientGeneratorOutputType outputType)
protected override string GenerateFile(IEnumerable<CodeArtifact> clientTypes, IEnumerable<CodeArtifact> dtoTypes, ClientGeneratorOutputType outputType)
{
return string.Empty;
}
Expand All @@ -177,9 +178,9 @@ protected override string GenerateFile(string clientCode, IEnumerable<string> cl
/// <param name="operation"></param>
/// <param name="settings"></param>
/// <returns></returns>
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);
}
}

Expand Down Expand Up @@ -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();
}
}
Expand Down
20 changes: 10 additions & 10 deletions WebApiClient.Tools.Swagger/Views/HttpApi.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@
<div>using WebApiClient;</div>
<div>using WebApiClient.Attributes;</div>
<div>using WebApiClient.DataAnnotations;</div>
<div>using WebApiClient.Parameterables;</div>
<div>using WebApiClient.Parameterables;</div>
</usings>

<namespace>
<div>namespace @(Model.AspNetNamespace)</div>
<div>namespace @(Model.NameSpace)</div>
<div>{</div>
<interface>
<summary>
Expand Down Expand Up @@ -92,33 +92,33 @@
<span>[Required]</span>
}

if (parameter.VariableName != parameter.Name&& parameter.Kind != SwaggerParameterKind.Header)
if (parameter.VariableName != parameter.Name&& parameter.Kind != OpenApiParameterKind.Header)
{
<span>[AliasAs("@(parameter.Name)")]</span>
}

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)
{
<span>[PathQuery(CollectionFormat = CollectionFormat.@(schema.CollectionFormat))]</span>
}
}
else if (parameter.Kind == SwaggerParameterKind.Header)
else if (parameter.Kind == OpenApiParameterKind.Header)
{
<span>[Header("@(parameter.Name)")]</span>
}
else if (parameter.IsXmlBodyParameter == true)
{
<span>[XmlContent]</span>
}
else if (parameter.Kind == SwaggerParameterKind.Body)
else if (parameter.Kind == OpenApiParameterKind.Body)
{
<span>[JsonContent]</span>
}
else if (parameter.Kind == SwaggerParameterKind.FormData)
else if (parameter.Kind == OpenApiParameterKind.FormData)
{
if (parameter.IsFile == false)
{
Expand Down
2 changes: 1 addition & 1 deletion WebApiClient.Tools.Swagger/Views/HttpModel.cshtml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
</usings>

<namespace>
<div>namespace @(Model.AspNetNamespace)</div>
<div>namespace @(Model.NameSapce)</div>
<div>{</div>
<class>
@foreach (var line in Model.Lines)
Expand Down
6 changes: 5 additions & 1 deletion WebApiClient.Tools.Swagger/WebApiClient.Tools.Swagger.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@
<Summary>将swagger的本地或远程json文件解析生成WebApiClient的接口定义代码文件的工具</Summary>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
<PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup>

<ItemGroup>
<Compile Remove="output\**" />
<EmbeddedResource Remove="output\**" />
Expand All @@ -19,7 +23,7 @@
<ItemGroup>
<PackageReference Include="CommandLineParser" Version="1.9.71" />
<PackageReference Include="RazorEngine" Version="3.10.0" />
<PackageReference Include="NSwag.CodeGeneration.CSharp" Version="11.20.1" />
<PackageReference Include="NSwag.CodeGeneration.CSharp" Version="13.0.6" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit d90f318

Please sign in to comment.