Skip to content

Commit 7be4374

Browse files
committed
fix Nullable Warnings
1 parent d55a25c commit 7be4374

File tree

3 files changed

+26
-14
lines changed

3 files changed

+26
-14
lines changed

src/MicroBatchFramework.WebHosting/BatchEngineHostingExtensions.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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/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;

0 commit comments

Comments
 (0)