Skip to content

Commit 8f798eb

Browse files
committed
Enable nullable for asplib projects
Enable nullable for asplib core projects and api.core, apply embellishments (early instantiation, throw specific null exceptions, only declare something as nullable when it is meant to be nullable) to minimize compiler warnings. Disable CS0618 obsolete warning for IIE references.
1 parent 16a4ae0 commit 8f798eb

File tree

71 files changed

+387
-248
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

71 files changed

+387
-248
lines changed

api.core.sln

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.29230.61
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.1.32407.343
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "apiservice.core", "src\apiservice.core\apiservice.core.csproj", "{13497EE9-EACF-4F88-9EB0-03DE6BFBA3EE}"
77
ProjectSection(ProjectDependencies) = postProject

minimal.core.sln

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.30011.22
3+
# Visual Studio Version 17
4+
VisualStudioVersion = 17.0.32126.317
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "minimal.core", "src\minimal.core\minimal.core.csproj", "{C9A372A9-BBE9-49A5-9217-F110FE0078A7}"
77
EndProject

src/apicaller.core/Controllers/CallController.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using asplib.Controllers;
33
using Microsoft.AspNetCore.Mvc;
44
using Microsoft.Extensions.Configuration;
5+
using System;
56
using System.Threading.Tasks;
67

78
namespace apicaller.Controllers
@@ -10,8 +11,8 @@ namespace apicaller.Controllers
1011
[ApiController]
1112
public class CallController : PersistentController
1213
{
13-
internal IServiceClient _serviceClient;
14-
internal string[] _serviceClientCookies;
14+
internal IServiceClient _serviceClient = default!;
15+
internal string[] _serviceClientCookies = Array.Empty<string>();
1516

1617
public CallController(
1718
IConfiguration configuration,

src/apicaller.core/Services/Dto/MessageResponseDto.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ namespace apicaller.Services.Dto
44
{
55
public class MessageResponseDto : IMessageResponse
66
{
7-
public string State { get; set; }
8-
public string Message { get; set; }
7+
public string State { get; set; } = default!;
8+
public string Message { get; set; } = default!;
99
}
1010
}

src/apicaller.core/Services/ServiceClient.cs

+11-6
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ namespace apicaller.Services
1717
{
1818
public class ServiceClient : IServiceClient
1919
{
20-
internal IConfiguration _configuration;
21-
internal IHttpClientFactory _clientFactory;
20+
internal IConfiguration _configuration = default!;
21+
internal IHttpClientFactory _clientFactory = default!;
2222

23-
public string[] Cookies { get; set; }
23+
public string[] Cookies { get; set; } = Array.Empty<string>();
2424

2525
internal virtual Uri ServiceHost
2626
{
@@ -63,7 +63,8 @@ public async Task<ActionResult<string>> Authenticate(string phonenumber)
6363
var response = await client.PostAsync(ResouceUri("authenticate"), JsonContent.Serialize(request));
6464
if (response.StatusCode != HttpStatusCode.OK) return new StatusCodeResult((int)HttpStatusCode.ServiceUnavailable);
6565
this.Cookies = response.Headers.GetValues(SetCookie).ToArray();
66-
var result = JsonContent.Deserialize<MessageResponseDto>(response.Content);
66+
var result = JsonContent.Deserialize<MessageResponseDto>(response.Content) ??
67+
throw new Exception("Null response"); ;
6768
return result.Message;
6869
}
6970
}
@@ -75,7 +76,8 @@ public async Task<ActionResult<string>> Verify(string accesscode)
7576
var request = new VerifyRequest() { Accesscode = accesscode };
7677
var response = await client.PostAsync(ResouceUri("verify"), JsonContent.Serialize(request));
7778
if (response.StatusCode != HttpStatusCode.OK) return new StatusCodeResult((int)HttpStatusCode.ServiceUnavailable);
78-
var result = JsonContent.Deserialize<MessageResponseDto>(response.Content);
79+
var result = JsonContent.Deserialize<MessageResponseDto>(response.Content) ??
80+
throw new Exception("Null response"); ;
7981
return result.Message;
8082
}
8183
}
@@ -85,7 +87,10 @@ internal Uri ResouceUri(string command)
8587
Uri retval;
8688
var builder = new UriBuilder(ServiceHost);
8789
builder.Path = _configuration.GetValue<string>("accesscodePath").TrimEnd('/') + "/";
88-
Uri.TryCreate(builder.Uri, command, out retval);
90+
if (!Uri.TryCreate(builder.Uri, command, out retval!))
91+
{
92+
throw new Exception($"Could not create Uri for path {builder.Path} and command {command}");
93+
}
8994
return retval;
9095
}
9196

src/apicaller.core/Startup.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ namespace apicaller
1515
{
1616
public class Startup
1717
{
18-
public IConfiguration Configuration { get; }
19-
public IWebHostEnvironment Environment { get; }
20-
public IHttpContextAccessor HttpContext { get; }
18+
public IConfiguration Configuration { get; } = default!;
19+
public IWebHostEnvironment Environment { get; } = default!;
20+
public IHttpContextAccessor HttpContext { get; } = default!;
2121

2222
public Startup(IConfiguration configuration)
2323
{

src/apicaller.core/apicaller.core.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
56
<RootNamespace>apicaller</RootNamespace>
67
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
78
</PropertyGroup>

src/apiservice.core/Controllers/AccesscodeFsm.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ namespace apiservice.Controllers
1212
[Clsid("10E4F343-877A-422F-A736-4EF32D87D28A")]
1313
public partial class AccesscodeController : SmcController<AccesscodeContext, AccesscodeContext.AccesscodeControllerState>
1414
{
15-
internal IConfiguration _configuration;
16-
internal AspserviceDbContext _DbContext;
17-
internal ISMSService _SMSService;
15+
internal IConfiguration _configuration = default!;
16+
internal AspserviceDbContext _DbContext = default!;
17+
internal ISMSService _SMSService = default!;
1818

19-
internal string _pnonenumber;
20-
internal string _accesscode;
21-
internal int _attempts;
19+
internal string _pnonenumber = default!;
20+
internal string _accesscode = default!;
21+
internal int _attempts = default!;
2222

2323
public AccesscodeController() : base()
2424
{

src/apiservice.core/Model/Db/AspserviceDb.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class AspserviceDb : IAspserviceDb
1414
{
1515
public const int ACCESSCODE_LENGTH = 6; // dbo.Accesscode.accesscode
1616

17-
internal AspserviceDbContext _DbContext;
17+
internal AspserviceDbContext _DbContext = default!;
1818

1919
public AspserviceDb(AspserviceDbContext dbContext)
2020
{

src/apiservice.core/apiservice.core.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
56
<RootNamespace>apiservice</RootNamespace>
67
<EnableUnsafeBinaryFormatterSerialization>true</EnableUnsafeBinaryFormatterSerialization>
78
</PropertyGroup>

src/apitest.core/apicaller/Controllers/CallControllerTest.cs

+3-2
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,10 @@ public class CallControllerTest : CallController, IDeleteNewRows
2828
/// <summary>
2929
/// IDeleteNewRows
3030
/// </summary>
31-
public List<(string, string, object)> MaxIds { get; set; }
31+
public List<(string, string, object)> MaxIds { get; set; } = new();
3232

33-
private TestServer _serviceServer; // indirectly used
33+
// Instantiated by [SetUp]:
34+
private TestServer _serviceServer = default!; // indirectly used
3435

3536
/// <summary>
3637
/// IDeleteNewRows

src/apitest.core/apicaller/ServerTest.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,17 @@ public class ServerTest : IDeleteNewRows
2828
{
2929
#region scaffolding
3030

31-
private IConfiguration _configuration;
31+
// instantiated in [OneTimeSetUp]:
32+
private IConfiguration _configuration = default!;
3233

33-
private TestServer _callerServer; // Our own server, directly used here
34-
35-
private TestServer _serviceServer; // Service server, called indirectly used from _callerServer
36-
37-
private IHttpClientFactory _clientFactory; // for our _callerServer
34+
private TestServer _callerServer = default!; // Our own server, directly used here
35+
private TestServer _serviceServer = default!; // Service server, called indirectly used from _callerServer
36+
private IHttpClientFactory _clientFactory = default!; // for our _callerServer
3837

3938
/// <summary>
4039
/// IDeleteNewRows
4140
/// </summary>
42-
public List<(string, string, object)> MaxIds { get; set; }
41+
public List<(string, string, object)> MaxIds { get; set; } = new();
4342

4443
/// <summary>
4544
/// IDeleteNewRows

src/apitest.core/apicaller/ServiceProvider.cs

+10-3
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@ namespace apitest.apicaller
1313
/// </summary>
1414
public static class ServiceProvider
1515
{
16-
private static IConfiguration _configuration;
17-
private static IServiceProvider _provider;
16+
// Instantiated by lazy CreateMembers on each accessor
17+
private static IConfiguration _configuration = default!;
18+
19+
private static IServiceProvider _provider = default!;
1820

1921
public static IConfiguration Configuration
2022
{
@@ -28,7 +30,12 @@ public static IConfiguration Configuration
2830
public static T Get<T>()
2931
{
3032
CreateMembers();
31-
return (T)_provider.GetService(typeof(T));
33+
var inst = (T?)_provider.GetService(typeof(T));
34+
if (inst == null)
35+
{
36+
throw new Exception($"Service not registered: {typeof(T)}");
37+
}
38+
return (T)inst;
3239
}
3340

3441
public static TestServer CreateTestServer()

src/apitest.core/apicaller/Services/ServiceClientTest.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@ public class ServiceClientTest : ServiceClient, IDeleteNewRows
2626
{
2727
#region scaffolding
2828

29-
private TestServer _server;
29+
// Instantiated by [OneTimeSetUp]
30+
private TestServer _server = default!;
3031

3132
/// <summary>
3233
/// IDeleteNewRows
3334
/// </summary>
34-
public List<(string, string, object)> MaxIds { get; set; }
35+
public List<(string, string, object)> MaxIds { get; set; } = new();
3536

3637
/// <summary>
3738
/// "Foreign" connection string from the service receiving the API calls
@@ -73,7 +74,7 @@ public void DeleteNewRows()
7374
[TearDown]
7475
public void DeleteSession()
7576
{
76-
Cookies = null;
77+
Cookies = Array.Empty<string>();
7778
}
7879

7980
[OneTimeTearDown]

src/apitest.core/apiservice/Controllers/AccesscodeControllerTest.cs

+25-12
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public class AccesscodeControllerTest : AccesscodeController, IGlobalTransaction
2121
{
2222
private const string ACCESSCODE = "123456";
2323

24-
public List<DbContext> DbContexts { get; set; }
24+
public List<DbContext> DbContexts { get; set; } = new();
2525

2626
[OneTimeSetUp]
2727
public void ConfigureServices()
@@ -73,8 +73,9 @@ public void AuthenticateTest()
7373
};
7474
var response = Authenticate(query).Result.Value;
7575

76-
Assert.That(response.State, Is.EqualTo("AuthMap.Unverified"));
77-
Assert.That(response.Phonenumber, Is.EqualTo(DbTestData.PHONENUMBER));
76+
Assert.That(response, Is.Not.Null);
77+
Assert.That(response!.State, Is.EqualTo("AuthMap.Unverified"));
78+
Assert.That(response!.Phonenumber, Is.EqualTo(DbTestData.PHONENUMBER));
7879
}
7980

8081
[Test]
@@ -90,9 +91,13 @@ public void VerifySucceedTest()
9091
};
9192
var response = Verify(query).Result.Value;
9293

93-
Assert.That(response.State, Is.EqualTo("AuthMap.Verified"));
94-
Assert.That(response.Phonenumber, Is.EqualTo(DbTestData.PHONENUMBER));
95-
Assert.That(response.Message, Does.Contain(DbTestData.PHONENUMBER));
94+
Assert.That(response, Is.Not.Null);
95+
Assert.Multiple(() =>
96+
{
97+
Assert.That(response!.State, Is.EqualTo("AuthMap.Verified"));
98+
Assert.That(response.Phonenumber, Is.EqualTo(DbTestData.PHONENUMBER));
99+
Assert.That(response.Message, Does.Contain(DbTestData.PHONENUMBER));
100+
});
96101
}
97102

98103
[Test]
@@ -108,9 +113,13 @@ public void VerifyFailTest()
108113
};
109114
var response = Verify(query).Result.Value;
110115

111-
Assert.That(response.State, Is.EqualTo("AuthMap.Unverified"));
112-
Assert.That(response.Phonenumber, Is.EqualTo(DbTestData.PHONENUMBER));
113-
Assert.That(response.Message, Does.Contain("retry"));
116+
Assert.That(response, Is.Not.Null);
117+
Assert.Multiple(() =>
118+
{
119+
Assert.That(response!.State, Is.EqualTo("AuthMap.Unverified"));
120+
Assert.That(response.Phonenumber, Is.EqualTo(DbTestData.PHONENUMBER));
121+
Assert.That(response.Message, Does.Contain("retry"));
122+
});
114123
}
115124

116125
[Test]
@@ -129,9 +138,13 @@ public void VerifyDeniedTest()
129138
var fail3 = Verify(query).Result.Value;
130139
var response = Verify(query).Result.Value;
131140

132-
Assert.That(response.State, Is.EqualTo("AuthMap.Denied"));
133-
Assert.That(response.Phonenumber, Is.EqualTo(DbTestData.PHONENUMBER));
134-
Assert.That(response.Message, Does.Contain("denied"));
141+
Assert.That(response, Is.Not.Null);
142+
Assert.Multiple(() =>
143+
{
144+
Assert.That(response!.State, Is.EqualTo("AuthMap.Denied"));
145+
Assert.That(response.Phonenumber, Is.EqualTo(DbTestData.PHONENUMBER));
146+
Assert.That(response.Message, Does.Contain("denied"));
147+
});
135148
}
136149
}
137150
}

src/apitest.core/apiservice/Controllers/AccesscodeFsmTest.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ namespace apitest.apiservice.Controllers
1717
[TestFixture]
1818
public class AccesscodeFsmTest : AccesscodeController, IGlobalTransaction
1919
{
20-
public List<DbContext> DbContexts { get; set; }
20+
public List<DbContext> DbContexts { get; set; } = new();
2121

2222
[OneTimeSetUp]
2323
public void ConfigureServices()
@@ -111,7 +111,7 @@ public void SaveAccesscodeTest()
111111
where a.Phonenumber == PHONE
112112
select a).FirstOrDefault();
113113
Assert.That(stored, Is.Not.Null);
114-
Assert.That(stored.Accesscode1, Is.EqualTo(CODE));
114+
Assert.That(stored!.Accesscode1, Is.EqualTo(CODE));
115115
}
116116
}
117117
}

src/apitest.core/apiservice/Model/Db/AspserviceDbContextTest.cs

+6-5
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public void LoadSessionLinqTest()
3535
select m;
3636
var main = query.FirstOrDefault();
3737
Assert.That(main, Is.Not.Null);
38-
Assert.That(main.Session, Is.EqualTo(DbTestData.SESSION));
38+
Assert.That(main!.Session, Is.EqualTo(DbTestData.SESSION));
3939
}
4040

4141
[Test]
@@ -46,7 +46,7 @@ public void LoadSessionRawSQLTest()
4646
WHERE session = {DbTestData.SESSION}
4747
").FirstOrDefault();
4848
Assert.That(main, Is.Not.Null);
49-
Assert.That(main.Session, Is.EqualTo(DbTestData.SESSION));
49+
Assert.That(main!.Session, Is.EqualTo(DbTestData.SESSION));
5050
}
5151

5252
[Test]
@@ -68,7 +68,7 @@ public void TransactionTest()
6868

6969
var fromDb = LoadMain(testSession);
7070
Assert.That(fromDb, Is.Not.Null);
71-
Assert.That(fromDb.Session, Is.EqualTo(testSession));
71+
Assert.That(fromDb!.Session, Is.EqualTo(testSession));
7272
}
7373
finally
7474
{
@@ -79,12 +79,13 @@ public void TransactionTest()
7979
}
8080
}
8181

82-
private Main LoadMain(Guid session)
82+
private Main? LoadMain(Guid session)
8383
{
8484
var query = from m in Main
8585
where m.Session == session
8686
select m;
87-
return query.FirstOrDefault();
87+
var inst = query.FirstOrDefault();
88+
return inst;
8889
}
8990
}
9091
}

0 commit comments

Comments
 (0)