Skip to content

Commit 9a9ecfc

Browse files
committed
fix Warnings caused by NRT
1 parent 1f0b9af commit 9a9ecfc

File tree

7 files changed

+70
-53
lines changed

7 files changed

+70
-53
lines changed

src/MicroBatchFramework.WebHosting/BatchEngineHostingExtensions.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ namespace MicroBatchFramework // .WebHosting
1414
{
1515
public static class BatchEngineHostingExtensions
1616
{
17-
public static IWebHostBuilder PrepareBatchEngineMiddleware(this IWebHostBuilder builder, IBatchInterceptor interceptor = null)
17+
public static IWebHostBuilder PrepareBatchEngineMiddleware(this IWebHostBuilder builder, IBatchInterceptor? interceptor = null)
1818
{
1919
var batchTypes = CollectBatchTypes();
2020
var target = new TargetBatchTypeCollection(batchTypes);
@@ -31,7 +31,7 @@ public static IWebHostBuilder PrepareBatchEngineMiddleware(this IWebHostBuilder
3131
});
3232
}
3333

34-
public static Task RunBatchEngineWebHosting(this IWebHostBuilder builder, string urls, SwaggerOptions swaggerOptions = null, IBatchInterceptor interceptor = null)
34+
public static Task RunBatchEngineWebHosting(this IWebHostBuilder builder, string urls, SwaggerOptions? swaggerOptions = null, IBatchInterceptor? interceptor = null)
3535
{
3636
return builder
3737
.PrepareBatchEngineMiddleware(interceptor)
@@ -43,7 +43,7 @@ public static Task RunBatchEngineWebHosting(this IWebHostBuilder builder, string
4343
var entryAsm = Assembly.GetEntryAssembly()!;
4444
var xmlName = entryAsm.GetName().Name + ".xml";
4545
var xmlPath = Path.Combine(Path.GetDirectoryName(entryAsm.Location) ?? "", xmlName);
46-
swaggerOptions = new SwaggerOptions(entryAsm.GetName().Name, "", "/") { XmlDocumentPath = xmlPath };
46+
swaggerOptions = new SwaggerOptions(entryAsm.GetName().Name!, "", "/") { XmlDocumentPath = xmlPath };
4747
}
4848
services.AddSingleton<SwaggerOptions>(swaggerOptions);
4949
})
@@ -105,7 +105,7 @@ static List<Type> CollectBatchTypes()
105105
if (!(asm.FullName is null)
106106
&& (asm.FullName.StartsWith("System") || asm.FullName.StartsWith("Microsoft.Extensions"))) continue;
107107

108-
Type[] types;
108+
Type[]? types;
109109
try
110110
{
111111
types = asm.GetTypes();

src/MicroBatchFramework.WebHosting/BatchEngineMiddleware.cs

+4-4
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ internal class WebHostingInterceptor : IBatchInterceptor
1414
readonly IBatchInterceptor innerInterceptor;
1515

1616
public bool CompleteSuccessfully { get; private set; }
17-
public string ErrorMessage { get; private set; }
18-
public Exception Exception { get; private set; }
17+
public string? ErrorMessage { get; private set; }
18+
public Exception? Exception { get; private set; }
1919

2020
public WebHostingInterceptor(IBatchInterceptor innerInterceptor)
2121
{
@@ -37,7 +37,7 @@ public ValueTask OnBatchRunBeginAsync(BatchContext context)
3737
return innerInterceptor.OnBatchRunBeginAsync(context);
3838
}
3939

40-
public ValueTask OnBatchRunCompleteAsync(BatchContext context, string errorMessageIfFailed, Exception exceptionIfExists)
40+
public ValueTask OnBatchRunCompleteAsync(BatchContext context, string? errorMessageIfFailed, Exception? exceptionIfExists)
4141
{
4242
this.CompleteSuccessfully = (errorMessageIfFailed == null && exceptionIfExists == null);
4343
this.ErrorMessage = errorMessageIfFailed;
@@ -115,7 +115,7 @@ public async Task Invoke(HttpContext httpContext)
115115
}
116116

117117
// create args
118-
string[] args = null;
118+
string?[] args;
119119
try
120120
{
121121
if (httpContext.Request.HasFormContentType)

src/MicroBatchFramework.WebHosting/Swagger/Schemas/SwaggerDocument.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// This definition is borrowed from Swashbuckle.
2-
2+
#nullable disable annotations
33
using Newtonsoft.Json;
44
using System.Collections.Generic;
55

src/MicroBatchFramework.WebHosting/Swagger/SwaggerDefinitionBuilder.cs

+39-29
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class SwaggerDefinitionBuilder
2020
readonly HttpContext httpContext;
2121
readonly IEnumerable<MethodInfo> handlers;
2222

23-
ILookup<Tuple<string, string>, XmlCommentStructure> xDocLookup;
23+
ILookup<Tuple<string, string>, XmlCommentStructure>? xDocLookup;
2424

2525
public SwaggerDefinitionBuilder(SwaggerOptions options, HttpContext httpContext, IEnumerable<MethodInfo> handlers)
2626
{
@@ -33,15 +33,13 @@ public byte[] BuildSwaggerJson()
3333
{
3434
try
3535
{
36-
if (options.XmlDocumentPath != null && !File.Exists(options.XmlDocumentPath))
36+
if (options.XmlDocumentPath != null && File.Exists(options.XmlDocumentPath))
3737
{
38-
xDocLookup = null;
38+
xDocLookup = BuildXmlMemberCommentStructure(options.XmlDocumentPath);
3939
}
4040
else
4141
{
42-
xDocLookup = (options.XmlDocumentPath != null)
43-
? BuildXmlMemberCommentStructure(options.XmlDocumentPath)
44-
: null;
42+
xDocLookup = null;
4543
}
4644

4745
var doc = new SwaggerDocument();
@@ -54,7 +52,7 @@ public byte[] BuildSwaggerJson()
5452

5553
// tags.
5654
var xmlServiceName = (xDocLookup != null)
57-
? BuildXmlTypeSummary(options.XmlDocumentPath)
55+
? BuildXmlTypeSummary(options.XmlDocumentPath!) // xDocLookup is not null if XmlDocumentPath is not null.
5856
: null;
5957

6058
doc.tags = handlers
@@ -63,7 +61,7 @@ public byte[] BuildSwaggerJson()
6361
.Distinct()
6462
.Select(x =>
6563
{
66-
string desc = null;
64+
string? desc = null;
6765
if (xmlServiceName != null)
6866
{
6967
xmlServiceName.TryGetValue(x, out desc);
@@ -80,7 +78,7 @@ public byte[] BuildSwaggerJson()
8078
{
8179
// MemberInfo.DeclaringType is null only if it is a member of a VB Module.
8280
string declaringTypeName = item.DeclaringType!.Name;
83-
XmlCommentStructure xmlComment = null;
81+
XmlCommentStructure? xmlComment = null;
8482
if (xDocLookup != null)
8583
{
8684
// ParameterInfo.Name will be null only it is ReturnParameter.
@@ -123,7 +121,7 @@ public byte[] BuildSwaggerJson()
123121
}
124122
}
125123

126-
Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, XmlCommentStructure xmlComment, MethodInfo method)
124+
Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, XmlCommentStructure? xmlComment, MethodInfo method)
127125
{
128126
var parameterInfos = method.GetParameters();
129127
var parameters = parameterInfos
@@ -132,7 +130,8 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
132130
var parameterXmlComment = UnwrapTypeName(x.ParameterType);
133131
if (xmlComment != null)
134132
{
135-
xmlComment.Parameters.TryGetValue(x.Name, out parameterXmlComment!);
133+
// Name is null only if Parameter is ReturnParameter.
134+
xmlComment.Parameters.TryGetValue(x.Name!, out parameterXmlComment!);
136135
parameterXmlComment = UnwrapTypeName(x.ParameterType) + " " + parameterXmlComment;
137136
}
138137

@@ -147,8 +146,8 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
147146
? new PartialSchema { type = ToSwaggerDataType(collectionType) }
148147
: null;
149148

150-
string defaultObjectExample = null;
151-
object[] enums = null;
149+
string? defaultObjectExample = null;
150+
object[]? enums = null;
152151
if (x.ParameterType.GetTypeInfo().IsEnum || (collectionType != null && collectionType.GetTypeInfo().IsEnum))
153152
{
154153
// Compiler cannot understand collectionType is not null.
@@ -169,7 +168,7 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
169168
}
170169

171170
var swaggerDataType = ToSwaggerDataType(x.ParameterType);
172-
Schema refSchema = null;
171+
Schema? refSchema = null;
173172
if (swaggerDataType == "object")
174173
{
175174
BuildSchema(definitions, x.ParameterType);
@@ -203,7 +202,7 @@ string BuildSchema(IDictionary<string, Schema> definitions, Type type)
203202
var fullName = type.FullName;
204203
if (fullName == null) return ""; // safety(TODO:IDictionary<> is not supported)
205204

206-
Schema schema;
205+
Schema? schema;
207206
if (definitions.TryGetValue(fullName, out schema)) return "#/definitions/" + fullName;
208207

209208
var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
@@ -229,10 +228,11 @@ string BuildSchema(IDictionary<string, Schema> definitions, Type type)
229228
}
230229
else
231230
{
232-
Schema items = null;
231+
Schema? items = null;
233232
if (swaggerDataType == "array")
234233
{
235-
var collectionType = GetCollectionType(memberType);
234+
// If swaggerDataType is array, it will be Collection.
235+
Type collectionType = GetCollectionType(memberType)!;
236236
var dataType = ToSwaggerDataType(collectionType);
237237
if (dataType == "object")
238238
{
@@ -258,7 +258,7 @@ string BuildSchema(IDictionary<string, Schema> definitions, Type type)
258258
}
259259
}
260260

261-
IList<object> schemaEnum = null;
261+
IList<object>? schemaEnum = null;
262262
if (memberType.GetTypeInfo().IsEnum)
263263
{
264264
schemaEnum = Enum.GetNames(memberType);
@@ -298,7 +298,7 @@ static Type GetMemberType(MemberInfo memberInfo)
298298
throw new Exception();
299299
}
300300

301-
static Type GetCollectionType(Type type)
301+
static Type? GetCollectionType(Type type)
302302
{
303303
if (type.IsArray) return type.GetElementType();
304304

@@ -338,14 +338,14 @@ static ILookup<Tuple<string, string>, XmlCommentStructure> BuildXmlMemberComment
338338
.ToDictionary(e => e.Item1, e => e.Item2.Value.Trim());
339339

340340
return new XmlCommentStructure
341-
{
342-
ClassName = match.Groups[1].Value,
343-
MethodName = match.Groups[2].Value,
344-
Summary = summary.Trim(),
345-
Remarks = remarks.Trim(),
346-
Parameters = parameters,
347-
Returns = returns.Trim()
348-
};
341+
(
342+
className: match.Groups[1].Value,
343+
methodName: match.Groups[2].Value,
344+
summary: summary.Trim(),
345+
remarks: remarks.Trim(),
346+
parameters: parameters,
347+
returns: returns.Trim()
348+
);
349349
})
350350
.ToLookup(x => Tuple.Create(x.ClassName, x.MethodName));
351351

@@ -422,7 +422,7 @@ static string UnwrapTypeName(Type t)
422422
return Regex.Replace(t.GetGenericTypeDefinition().Name, @"`.+$", "") + "&lt;" + innerFormat + "&gt;";
423423
}
424424

425-
class Item1EqualityCompaerer<T1, T2> : EqualityComparer<Tuple<T1, T2>>
425+
class Item1EqualityCompaerer<T1, T2> : EqualityComparer<Tuple<T1, T2>> where T1 : class
426426
{
427427
public override bool Equals(Tuple<T1, T2> x, Tuple<T1, T2> y)
428428
{
@@ -443,6 +443,16 @@ class XmlCommentStructure
443443
public string Remarks { get; set; }
444444
public Dictionary<string, string> Parameters { get; set; }
445445
public string Returns { get; set; }
446+
447+
public XmlCommentStructure(string className, string methodName, string summary, string remarks, Dictionary<string,string> parameters, string returns)
448+
{
449+
ClassName = className;
450+
MethodName = methodName;
451+
Summary = summary;
452+
Remarks = remarks;
453+
Parameters = parameters;
454+
Returns = returns;
455+
}
446456
}
447457
}
448458

@@ -459,7 +469,7 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
459469
{
460470
property.ShouldSerialize = instance =>
461471
{
462-
IEnumerable enumerable = null;
472+
IEnumerable? enumerable = null;
463473

464474
// this value could be in a public field or public property
465475
switch (member.MemberType)

src/MicroBatchFramework.WebHosting/Swagger/SwaggerOptions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ public class SwaggerOptions
1212
/// <summary>
1313
/// (FilePath, LoadedEmbeddedBytes) => CustomBytes)
1414
/// </summary>
15-
public Func<string, byte[], byte[]> ResolveCustomResource { get; set; }
16-
public Func<HttpContext, string> CustomHost { get; set; }
17-
public string XmlDocumentPath { get; set; }
15+
public Func<string, byte[]?, byte[]>? ResolveCustomResource { get; set; }
16+
public Func<HttpContext, string>? CustomHost { get; set; }
17+
public string? XmlDocumentPath { get; set; }
1818
public string JsonName { get; set; }
1919
public string[] ForceSchemas { get; set; }
2020

src/MicroBatchFramework/BatchContext.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ namespace MicroBatchFramework
66
{
77
public class BatchContext
88
{
9-
public string[] Arguments { get; private set; }
9+
public string?[] Arguments { get; private set; }
1010
public DateTime Timestamp { get; private set; }
1111
public CancellationToken CancellationToken { get; private set; }
1212
public ILogger<BatchEngine> Logger { get; private set; }
1313

14-
public BatchContext(string[] arguments, DateTime timestamp, CancellationToken cancellationToken, ILogger<BatchEngine> logger)
14+
public BatchContext(string?[] arguments, DateTime timestamp, CancellationToken cancellationToken, ILogger<BatchEngine> logger)
1515
{
1616
Arguments = arguments;
1717
Timestamp = timestamp;

src/MicroBatchFramework/BatchEngine.cs

+17-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public BatchEngine(ILogger<BatchEngine> logger, IServiceProvider provider, IBatc
2626
this.cancellationToken = cancellationToken;
2727
}
2828

29-
public async Task RunAsync(Type type, MethodInfo method, string[] args)
29+
public async Task RunAsync(Type type, MethodInfo method, string?[] args)
3030
{
3131
logger.LogTrace("BatchEngine.Run Start");
3232
var ctx = new BatchContext(args, DateTime.UtcNow, cancellationToken, logger);
@@ -116,7 +116,7 @@ public async Task RunAsync(Type type, string[] args)
116116
await RunCore(ctx, type, method, args, argsOffset);
117117
}
118118

119-
async Task RunCore(BatchContext ctx, Type type, MethodInfo methodInfo, string[] args, int argsOffset)
119+
async Task RunCore(BatchContext ctx, Type type, MethodInfo methodInfo, string?[] args, int argsOffset)
120120
{
121121
object instance;
122122
object[] invokeArgs;
@@ -199,7 +199,7 @@ async ValueTask SetFailAsync(BatchContext context, string message, Exception ex)
199199
await interceptor.OnBatchRunCompleteAsync(context, message, ex);
200200
}
201201

202-
static bool TryGetInvokeArguments(ParameterInfo[] parameters, string[] args, int argsOffset, out object[] invokeArgs, out string? errorMessage)
202+
static bool TryGetInvokeArguments(ParameterInfo[] parameters, string?[] args, int argsOffset, out object[] invokeArgs, out string? errorMessage)
203203
{
204204
var argumentDictionary = ParseArgument(args, argsOffset);
205205
invokeArgs = new object[parameters.Length];
@@ -294,22 +294,29 @@ static bool TryGetInvokeArguments(ParameterInfo[] parameters, string[] args, int
294294
return true;
295295
}
296296

297-
static ReadOnlyDictionary<string, OptionParameter> ParseArgument(string[] args, int argsOffset)
297+
static ReadOnlyDictionary<string, OptionParameter> ParseArgument(string?[] args, int argsOffset)
298298
{
299299
var dict = new Dictionary<string, OptionParameter>(args.Length, StringComparer.OrdinalIgnoreCase);
300300
for (int i = argsOffset; i < args.Length;)
301301
{
302-
if (!args[i].StartsWith("-"))
302+
var key = args[i++];
303+
if (key is null || !key.StartsWith("-"))
303304
{
304-
i++;
305305
continue; // not key
306306
}
307307

308-
var key = args[i++].TrimStart('-');
309-
if (i < args.Length && !args[i].StartsWith("-"))
308+
key = key.TrimStart('-');
309+
if (i >= args.Length)
310+
{
311+
dict.Add(key, new OptionParameter { BooleanSwitch = true }); // Last parameter
312+
break;
313+
}
314+
315+
var value = args[i];
316+
if (value != null && !value.StartsWith("-"))
310317
{
311-
var value = args[i++];
312318
dict.Add(key, new OptionParameter { Value = value });
319+
i++;
313320
}
314321
else
315322
{
@@ -322,7 +329,7 @@ static ReadOnlyDictionary<string, OptionParameter> ParseArgument(string[] args,
322329

323330
struct OptionParameter
324331
{
325-
public string Value;
332+
public string? Value;
326333
public bool BooleanSwitch;
327334
}
328335

0 commit comments

Comments
 (0)