Skip to content

Commit 7d07635

Browse files
committed
Nullable的支持
1 parent d3aa764 commit 7d07635

File tree

9 files changed

+32
-30
lines changed

9 files changed

+32
-30
lines changed

WebApiClientCore.OpenApi.SourceGenerator/CSharpCode.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ private static string TransformCode(string nswagCode)
8585
var cleaned = line
8686
.Replace("partial class", "class")
8787
.Replace("System.Collections.Generic.", null)
88-
.Replace("System.Runtime.Serialization.",null)
88+
.Replace("System.Runtime.Serialization.", null)
8989
.Replace("System.ComponentModel.DataAnnotations.", null);
9090

9191
builder.AppendLine(cleaned);
@@ -99,7 +99,7 @@ private static string TransformCode(string nswagCode)
9999
/// </summary>
100100
/// <param name="code">源代码</param>
101101
/// <returns></returns>
102-
private static string Pretty(string code)
102+
private static string? Pretty(string? code)
103103
{
104104
if (code == null)
105105
{
@@ -115,12 +115,12 @@ private static string Pretty(string code)
115115
var cTab = tab;
116116
if (line == "{")
117117
{
118-
tab = tab + 1;
118+
tab++;
119119
}
120120
else if (line == "}")
121121
{
122122
cTab = tab - 1;
123-
tab = tab - 1;
123+
tab--;
124124
}
125125

126126
var isEndMethod = line.EndsWith(");");
@@ -142,7 +142,7 @@ private static string Pretty(string code)
142142
/// </summary>
143143
/// <param name="code">源代码</param>
144144
/// <returns></returns>
145-
private static string Compact(string code)
145+
private static string? Compact(string? code)
146146
{
147147
if (code == null)
148148
{
@@ -166,18 +166,24 @@ private static string Compact(string code)
166166
/// </summary>
167167
/// <param name="code">源代码</param>
168168
/// <returns></returns>
169-
private static IEnumerable<string> GetLines(string code)
169+
private static IEnumerable<string> GetLines(string? code)
170170
{
171171
if (code == null)
172172
{
173173
yield break;
174174
}
175175

176-
using (var reader = new StringReader(code))
176+
using var reader = new StringReader(code);
177+
while (true)
177178
{
178-
while (reader.Peek() >= 0)
179+
var line = reader.ReadLine();
180+
if (line == null)
179181
{
180-
yield return reader.ReadLine();
182+
yield break;
183+
}
184+
else
185+
{
186+
yield return line;
181187
}
182188
}
183189
}

WebApiClientCore.OpenApi.SourceGenerator/CSharpHtml.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public CSharpHtml(string path)
8686
}
8787

8888
this.FilePath = Path.GetFullPath(path);
89-
this.BlockElements = new HashSet<string>(new[] { "p", "div" }, StringComparer.OrdinalIgnoreCase);
89+
this.BlockElements = new HashSet<string>(["p", "div"], StringComparer.OrdinalIgnoreCase);
9090
}
9191

9292

@@ -110,15 +110,10 @@ public string RenderHtml(T model)
110110
/// <summary>
111111
/// 返回视图文本
112112
/// </summary>
113-
/// <param name="model">模型</param>
114-
/// <exception cref="ArgumentNullException"></exception>
113+
/// <param name="model">模型</param>
115114
/// <returns></returns>
116115
public string RenderText(T model)
117116
{
118-
if (model == null)
119-
{
120-
throw new ArgumentNullException(nameof(model));
121-
}
122117
var html = this.RenderHtml(model);
123118
var doc = XDocument.Parse(html).Root;
124119
var builder = new StringBuilder();
@@ -176,7 +171,7 @@ static class Razor
176171
/// <summary>
177172
/// 模板缓存
178173
/// </summary>
179-
private static readonly ConcurrentDictionary<string, IRazorEngineCompiledTemplate> templateCache = new ConcurrentDictionary<string, IRazorEngineCompiledTemplate>();
174+
private static readonly ConcurrentDictionary<string, IRazorEngineCompiledTemplate> templateCache = new();
180175

181176
/// <summary>
182177
/// 编译并执行

WebApiClientCore.OpenApi.SourceGenerator/HtmlTempate.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class HtmlTempate : RazorEngineTemplateBase
1111
/// html标签转换
1212
/// </summary>
1313
/// <param name="obj"></param>
14-
public override void Write(object obj = null)
14+
public override void Write(object? obj = null)
1515
{
1616
var text = obj?.ToString();
1717
if (text != null)
@@ -28,6 +28,6 @@ public override void Write(object obj = null)
2828
/// <typeparam name="T"></typeparam>
2929
public class HtmlTempate<T> : HtmlTempate
3030
{
31-
public new T Model { get; set; }
31+
public new T? Model { get; set; }
3232
}
3333
}

WebApiClientCore.OpenApi.SourceGenerator/HttpApi.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public class HttpApi : CSharpControllerTemplateModel
2424
/// <summary>
2525
/// 获取文档描述
2626
/// </summary>
27-
public string Summary { get; }
27+
public string? Summary { get; }
2828

2929
/// <summary>
3030
/// 获取是否有文档描述
@@ -44,8 +44,7 @@ public bool HasSummary
4444
public HttpApi(string className, IEnumerable<CSharpOperationModel> operations, OpenApiDocument document, HttpApiSettings settings)
4545
: base(className, operations, document, settings)
4646
{
47-
var tag = document.Tags
48-
.FirstOrDefault(item => string.Equals(item.Name, className, StringComparison.OrdinalIgnoreCase));
47+
var tag = document.Tags.FirstOrDefault(item => string.Equals(item.Name, className, StringComparison.OrdinalIgnoreCase));
4948

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

WebApiClientCore.OpenApi.SourceGenerator/HttpApiMethod.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ protected override string ResolveParameterType(OpenApiParameter parameter)
8080
return base.ResolveParameterType(parameter);
8181
}
8282

83-
private static bool IsFileParameter(JsonSchema parameter)
83+
private static bool IsFileParameter(JsonSchema? parameter)
8484
{
8585
if (parameter == null)
8686
{

WebApiClientCore.OpenApi.SourceGenerator/HttpApiSettings.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ private class OperationNameProvider : MultipleClientsFromOperationIdOperationNam
5252
/// <param name="httpMethod"></param>
5353
/// <param name="operation"></param>
5454
/// <returns></returns>
55-
public override string GetClientName(OpenApiDocument document, string path, string httpMethod, OpenApiOperation operation)
55+
public override string? GetClientName(OpenApiDocument document, string path, string httpMethod, OpenApiOperation operation)
5656
{
5757
return operation.Tags.FirstOrDefault();
5858
}
@@ -85,7 +85,9 @@ public string Generate(OpenApiParameter parameter, IEnumerable<OpenApiParameter>
8585
.Replace("]", string.Empty));
8686

8787
if (allParameters.Count(p => p.Name == parameter.Name) > 1)
88+
{
8889
return variableName + parameter.Kind;
90+
}
8991

9092
return variableName;
9193
}
@@ -164,7 +166,7 @@ private static string PrettyName(string name)
164166
var index = -1;
165167
return Regex.Replace(name, @"\W", m =>
166168
{
167-
index = index + 1;
169+
index++;
168170
return index < matchs.Count / 2 ? "Of" : null;
169171
});
170172
}

WebApiClientCore.OpenApi.SourceGenerator/OpenApiDoc.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ private class HttpApiProvider : CSharpControllerGenerator
119119
/// <summary>
120120
/// api列表
121121
/// </summary>
122-
private readonly List<HttpApi> httpApiList = new List<HttpApi>();
122+
private readonly List<HttpApi> httpApiList = [];
123123

124124
/// <summary>
125125
/// HttpApi提供者
@@ -154,7 +154,7 @@ protected override IEnumerable<CodeArtifact> GenerateClientTypes(string controll
154154
{
155155
var model = new HttpApi(controllerClassName, operations, this.openApi.Document, this.openApi.Settings);
156156
this.httpApiList.Add(model);
157-
return new CodeArtifact[0];
157+
return [];
158158
}
159159

160160

WebApiClientCore.OpenApi.SourceGenerator/OpenApiDocOptions.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using CommandLine;
2-
using CommandLine.Text;
32

43
namespace WebApiClientCore.OpenApi.SourceGenerator
54
{
@@ -12,12 +11,12 @@ public class OpenApiDocOptions
1211
/// openApi的json本地文件路径或远程Uri地址
1312
/// </summary>
1413
[Option('o', "openapi", MetaValue = "OpenApi", Required = true, HelpText = "OpenApi的本地文件路径或远程Uri地址")]
15-
public string OpenApi { get; set; }
14+
public string OpenApi { get; set; } = string.Empty;
1615

1716
/// <summary>
1817
/// 代码的命名空间
1918
/// </summary>
2019
[Option('n', "namespace", MetaValue = "Namespace", Required = false, HelpText = "代码的命名空间,如WebApiClientCore")]
21-
public string Namespace { get; set; }
20+
public string Namespace { get; set; } = string.Empty;
2221
}
2322
}

WebApiClientCore.OpenApi.SourceGenerator/WebApiClientCore.OpenApi.SourceGenerator.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5+
<Nullable>enable</Nullable>
56
<TargetFrameworks>netcoreapp3.1;net6.0;net8.0</TargetFrameworks>
67

78
<Summary>将本地或远程OpenApi文档解析生成WebApiClientCore的接口定义代码文件的工具</Summary>

0 commit comments

Comments
 (0)