Skip to content

Commit f9a778b

Browse files
authored
Merge pull request #20 from nogic1008/feature/NullableWarnings
Enable Nullable Reference Type(only warnings)
2 parents 8a89d76 + 95ae22a commit f9a778b

19 files changed

+55
-65
lines changed

src/MicroBatchFramework.WebHosting/BatchEngineHostingExtensions.cs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
using MicroBatchFramework.WebHosting;
2-
using Microsoft.Extensions.DependencyInjection;
2+
using MicroBatchFramework.WebHosting.Swagger;
33
using Microsoft.AspNetCore.Builder;
44
using Microsoft.AspNetCore.Hosting;
5-
using System.Threading.Tasks;
6-
using System;
5+
using Microsoft.Extensions.DependencyInjection;
76
using Microsoft.Extensions.Logging;
8-
using System.Reflection;
7+
using System;
98
using System.Collections.Generic;
10-
using MicroBatchFramework.WebHosting.Swagger;
119
using System.IO;
10+
using System.Reflection;
11+
using System.Threading.Tasks;
1212

1313
namespace MicroBatchFramework // .WebHosting
1414
{
@@ -39,9 +39,10 @@ public static Task RunBatchEngineWebHosting(this IWebHostBuilder builder, string
3939
{
4040
if (swaggerOptions == null)
4141
{
42-
var entryAsm = Assembly.GetEntryAssembly();
42+
// GetEntryAssembly() never returns null when called from managed code.
43+
var entryAsm = Assembly.GetEntryAssembly()!;
4344
var xmlName = entryAsm.GetName().Name + ".xml";
44-
var xmlPath = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), xmlName);
45+
var xmlPath = Path.Combine(Path.GetDirectoryName(entryAsm.Location) ?? "", xmlName);
4546
swaggerOptions = new SwaggerOptions(entryAsm.GetName().Name, "", "/") { XmlDocumentPath = xmlPath };
4647
}
4748
services.AddSingleton<SwaggerOptions>(swaggerOptions);
@@ -101,7 +102,8 @@ static List<Type> CollectBatchTypes()
101102

102103
foreach (var asm in AppDomain.CurrentDomain.GetAssemblies())
103104
{
104-
if (asm.FullName.StartsWith("System") || asm.FullName.StartsWith("Microsoft.Extensions")) continue;
105+
if (!(asm.FullName is null)
106+
&& (asm.FullName.StartsWith("System") || asm.FullName.StartsWith("Microsoft.Extensions"))) continue;
105107

106108
Type[] types;
107109
try
@@ -110,9 +112,11 @@ static List<Type> CollectBatchTypes()
110112
}
111113
catch (ReflectionTypeLoadException ex)
112114
{
115+
// If Reflection cannot load a class, Types will be null.
113116
types = ex.Types;
114117
}
115118

119+
if (types is null) continue;
116120
foreach (var item in types)
117121
{
118122
if (typeof(BatchBase).IsAssignableFrom(item) && item != typeof(BatchBase))

src/MicroBatchFramework.WebHosting/BatchEngineMiddleware.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,8 @@ public async Task Invoke(HttpContext httpContext)
123123
args = new string[(httpContext.Request.Form.Count * 2) + 1];
124124
{
125125
var i = 0;
126-
args[i++] = methodInfo.DeclaringType.Name + "." + methodInfo.Name;
126+
// MemberInfo.DeclaringType is null only if it is a member of a VB Module.
127+
args[i++] = methodInfo.DeclaringType!.Name + "." + methodInfo.Name;
127128
foreach (var item in httpContext.Request.Form)
128129
{
129130
args[i++] = "-" + item.Key;
@@ -135,7 +136,8 @@ public async Task Invoke(HttpContext httpContext)
135136
}
136137
else
137138
{
138-
args = new[] { methodInfo.DeclaringType.Name + "." + methodInfo.Name };
139+
// MemberInfo.DeclaringType is null only if it is a member of a VB Module.
140+
args = new[] { methodInfo.DeclaringType!.Name + "." + methodInfo.Name };
139141
}
140142
}
141143
catch (Exception ex)

src/MicroBatchFramework.WebHosting/BatchEngineSwaggerMiddleware.cs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,9 @@
11
using MicroBatchFramework.WebHosting.Swagger;
2-
using System.Linq;
32
using Microsoft.AspNetCore.Http;
4-
using Microsoft.Extensions.Options;
5-
using System;
6-
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
75
using System.Reflection;
8-
using System.Text;
96
using System.Threading.Tasks;
10-
using System.IO;
117

128
namespace MicroBatchFramework.WebHosting
139
{

src/MicroBatchFramework.WebHosting/MicroBatchFramework.WebHosting.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
<PropertyGroup>
44
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<LangVersion>8.0</LangVersion>
6+
<Nullable>warnings</Nullable>
57
</PropertyGroup>
68

79
<ItemGroup>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// This definition is borrowed from Swashbuckle.
22

3-
using System.Collections.Generic;
43
using Newtonsoft.Json;
4+
using System.Collections.Generic;
55

66
namespace MicroBatchFramework.WebHosting.Swagger.Schemas
77
{

src/MicroBatchFramework.WebHosting/Swagger/SwaggerDefinitionBuilder.cs

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ public byte[] BuildSwaggerJson()
5858
: null;
5959

6060
doc.tags = handlers
61-
.Select(x => x.DeclaringType.Name)
61+
// MemberInfo.DeclaringType is null only if it is a member of a VB Module.
62+
.Select(x => x.DeclaringType!.Name)
6263
.Distinct()
6364
.Select(x =>
6465
{
@@ -77,16 +78,19 @@ public byte[] BuildSwaggerJson()
7778

7879
foreach (var item in handlers)
7980
{
81+
// MemberInfo.DeclaringType is null only if it is a member of a VB Module.
82+
string declaringTypeName = item.DeclaringType!.Name;
8083
XmlCommentStructure xmlComment = null;
8184
if (xDocLookup != null)
8285
{
83-
xmlComment = xDocLookup[Tuple.Create(item.DeclaringType.Name, item.Name)].FirstOrDefault();
86+
// ParameterInfo.Name will be null only it is ReturnParameter.
87+
xmlComment = xDocLookup[Tuple.Create(declaringTypeName, item.Name!)].FirstOrDefault();
8488
}
8589

8690
var parameters = BuildParameters(doc.definitions, xmlComment, item);
8791
var operation = new Operation
8892
{
89-
tags = new[] { item.DeclaringType.Name },
93+
tags = new[] { declaringTypeName },
9094
summary = (xmlComment != null) ? xmlComment.Summary : "",
9195
description = (xmlComment != null) ? xmlComment.Remarks : "",
9296
parameters = parameters,
@@ -96,7 +100,7 @@ public byte[] BuildSwaggerJson()
96100
}
97101
};
98102

99-
doc.paths.Add("/" + item.DeclaringType.Name + "/" + item.Name, new PathItem { post = operation }); // everything post.
103+
doc.paths.Add("/" + declaringTypeName + "/" + item.Name, new PathItem { post = operation }); // everything post.
100104
}
101105

102106
using (var ms = new MemoryStream())
@@ -128,7 +132,7 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
128132
var parameterXmlComment = UnwrapTypeName(x.ParameterType);
129133
if (xmlComment != null)
130134
{
131-
xmlComment.Parameters.TryGetValue(x.Name, out parameterXmlComment);
135+
xmlComment.Parameters.TryGetValue(x.Name, out parameterXmlComment!);
132136
parameterXmlComment = UnwrapTypeName(x.ParameterType) + " " + parameterXmlComment;
133137
}
134138

@@ -147,7 +151,8 @@ Schemas.Parameter[] BuildParameters(IDictionary<string, Schema> definitions, Xml
147151
object[] enums = null;
148152
if (x.ParameterType.GetTypeInfo().IsEnum || (collectionType != null && collectionType.GetTypeInfo().IsEnum))
149153
{
150-
var enumType = (x.ParameterType.GetTypeInfo().IsEnum) ? x.ParameterType : collectionType;
154+
// Compiler cannot understand collectionType is not null.
155+
var enumType = (x.ParameterType.GetTypeInfo().IsEnum) ? x.ParameterType : collectionType!;
151156

152157
var enumValues = Enum.GetNames(enumType);
153158

@@ -374,7 +379,8 @@ static string ToSwaggerDataType(Type type)
374379

375380
if (type.IsNullable())
376381
{
377-
type = Nullable.GetUnderlyingType(type);
382+
// if type is Nullable<T>, it has UnderlyingType T.
383+
type = Nullable.GetUnderlyingType(type)!;
378384
}
379385

380386
if (type.GetTypeInfo().IsEnum || type == typeof(DateTime) || type == typeof(DateTimeOffset))
@@ -462,13 +468,13 @@ protected override JsonProperty CreateProperty(MemberInfo member, MemberSerializ
462468
enumerable = instance
463469
.GetType()
464470
.GetProperty(member.Name)
465-
.GetValue(instance, null) as IEnumerable;
471+
?.GetValue(instance, null) as IEnumerable;
466472
break;
467473
case MemberTypes.Field:
468474
enumerable = instance
469475
.GetType()
470476
.GetField(member.Name)
471-
.GetValue(instance) as IEnumerable;
477+
?.GetValue(instance) as IEnumerable;
472478
break;
473479
default:
474480
break;

src/MicroBatchFramework/BatchBase.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Text;
2-
3-
namespace MicroBatchFramework
1+
namespace MicroBatchFramework
42
{
53
public abstract class BatchBase
64
{

src/MicroBatchFramework/BatchEngine.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
using Microsoft.Extensions.Logging;
2-
using System.Linq;
32
using System;
43
using System.Collections.Generic;
54
using System.Collections.ObjectModel;
5+
using System.Linq;
66
using System.Reflection;
77
using System.Text;
88
using System.Threading;

src/MicroBatchFramework/BatchEngineHostBuilderExtensions.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
using System.Linq;
2-
using MicroBatchFramework.Logging;
3-
using Microsoft.Extensions.DependencyInjection;
1+
using Microsoft.Extensions.DependencyInjection;
42
using Microsoft.Extensions.Hosting;
53
using System;
6-
using System.Threading.Tasks;
7-
using System.Reflection;
84
using System.Collections.Generic;
9-
using System.Text;
5+
using System.Linq;
6+
using System.Reflection;
7+
using System.Threading.Tasks;
108

119
namespace MicroBatchFramework
1210
{

src/MicroBatchFramework/BatchEngineService.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Microsoft.Extensions.Logging;
33
using System;
44
using System.Reflection;
5-
using System.Runtime.ExceptionServices;
65
using System.Threading;
76
using System.Threading.Tasks;
87
using Microsoft.Extensions.DependencyInjection;

0 commit comments

Comments
 (0)