Skip to content

Commit 55c982f

Browse files
authored
Merge pull request #25 from nogic1008/feature/NullableAnnotations
Enable Nullable Reference Type(annotations)
2 parents 3f864cd + 040d11e commit 55c982f

16 files changed

+101
-79
lines changed

src/MicroBatchFramework.WebHosting/BatchEngineHostingExtensions.cs

Lines changed: 4 additions & 4 deletions
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

Lines changed: 4 additions & 4 deletions
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/MicroBatchFramework.WebHosting.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.0</TargetFramework>
55
<LangVersion>8.0</LangVersion>
6-
<Nullable>warnings</Nullable>
6+
<Nullable>enable</Nullable>
77
</PropertyGroup>
88

99
<ItemGroup>

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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 39 additions & 29 deletions
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

Lines changed: 3 additions & 3 deletions
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/BatchBase.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
{
33
public abstract class BatchBase
44
{
5+
// Context will be set non-null value by BatchEngine,
6+
// but it might be null because it has public setter.
7+
#nullable disable warnings
58
public BatchContext Context { get; set; }
9+
#nullable restore warnings
610
}
711
}

src/MicroBatchFramework/BatchContext.cs

Lines changed: 2 additions & 2 deletions
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;

0 commit comments

Comments
 (0)