Skip to content

Commit bd3279d

Browse files
author
Dries Verbeke
committed
Resolving all warnings
The build produced a couple warnings around null references and had some bad project references. In order not to hide real warnings that need to resolve. The idea is to make sure there are zero warnings
1 parent 9c96a2f commit bd3279d

File tree

9 files changed

+174
-161
lines changed

9 files changed

+174
-161
lines changed

Fritz.InstantAPIs.Generators.Helpers.Tests/Fritz.InstantAPIs.Generators.Helpers.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<PropertyGroup>
33
<TargetFramework>net6.0</TargetFramework>
44
<Nullable>enable</Nullable>
5-
</PropertyGroup>
5+
<EnableNETAnalyzers>True</EnableNETAnalyzers>
6+
</PropertyGroup>
67
<ItemGroup>
78
<FrameworkReference Include="Microsoft.AspNetCore.App" />
89
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.1.0" />

Fritz.InstantAPIs.Generators.Helpers/Fritz.InstantAPIs.Generators.Helpers.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
<TargetFramework>net6.0</TargetFramework>
55
<ImplicitUsings>enable</ImplicitUsings>
66
<Nullable>enable</Nullable>
7+
<EnableNETAnalyzers>True</EnableNETAnalyzers>
78
</PropertyGroup>
89

910
</Project>

Fritz.InstantAPIs.Generators/Fritz.InstantAPIs.Generators.csproj

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,10 @@
33
<LangVersion>latest</LangVersion>
44
<Nullable>enable</Nullable>
55
<TargetFramework>netstandard2.0</TargetFramework>
6+
<EnableNETAnalyzers>True</EnableNETAnalyzers>
67
</PropertyGroup>
7-
<ItemGroup>
8-
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
9-
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" />
10-
</ItemGroup>
8+
<ItemGroup>
9+
<PackageReference Include="Microsoft.CodeAnalysis.Analyzers" Version="3.3.3" PrivateAssets="all" />
10+
<PackageReference Include="Microsoft.CodeAnalysis.CSharp" Version="4.0.1" />
11+
</ItemGroup>
1112
</Project>

InstantAPIs/InstantAPIsBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ private void BuildTables()
108108
}
109109

110110
// Remove the Excluded tables
111-
outTables = outTables.Where(t => !_ExcludedTables.Any(e => t.Name.Equals(e, StringComparison.InvariantCultureIgnoreCase))).ToArray();
111+
outTables = outTables.Where(t => !_ExcludedTables.Any(e => e.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase))).ToArray();
112112

113113
if (outTables == null || !outTables.Any()) throw new ArgumentException("All tables were excluded from this configuration");
114114

InstantAPIs/JsonAPIsConfig.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class JsonAPIsConfigBuilder
1616
{
1717

1818
private JsonAPIsConfig _Config = new();
19-
private string _FileName;
19+
private string? _FileName;
2020
private readonly HashSet<TableApiMapping> _IncludedTables = new();
2121
private readonly List<string> _ExcludedTables = new();
2222

@@ -63,12 +63,12 @@ public JsonAPIsConfigBuilder ExcludeTable(string entityName)
6363

6464
private HashSet<string> IdentifyEntities()
6565
{
66-
var writableDoc = JsonNode.Parse(File.ReadAllText(_FileName));
66+
var writableDoc = JsonNode.Parse(File.ReadAllText(_Config.JsonFilename));
6767

6868
// print API
6969
return writableDoc?.Root.AsObject()
7070
.AsEnumerable().Select(x => x.Key)
71-
.ToHashSet();
71+
.ToHashSet() ?? new HashSet<string>();
7272

7373
}
7474

@@ -106,7 +106,7 @@ private void BuildTables()
106106
}
107107

108108
// Remove the Excluded tables
109-
outTables = outTables.Where(t => !_ExcludedTables.Any(e => t.Name.Equals(e, StringComparison.InvariantCultureIgnoreCase))).ToArray();
109+
outTables = outTables.Where(t => !_ExcludedTables.Any(e => e.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase))).ToArray();
110110

111111
if (outTables == null || !outTables.Any()) throw new ArgumentException("All tables were excluded from this configuration");
112112

InstantAPIs/JsonApiExtensions.cs

Lines changed: 123 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -7,123 +7,131 @@ namespace InstantAPIs;
77
public static class JsonApiExtensions
88
{
99

10-
static JsonAPIsConfig _Config;
10+
static JsonAPIsConfig _Config = new JsonAPIsConfig();
1111

12-
public static WebApplication UseJsonRoutes(this WebApplication app, Action<JsonAPIsConfigBuilder> options = null)
13-
{
12+
public static WebApplication UseJsonRoutes(this WebApplication app, Action<JsonAPIsConfigBuilder>? options = null)
13+
{
1414

15-
var builder = new JsonAPIsConfigBuilder();
16-
_Config = new JsonAPIsConfig();
17-
if (options != null)
15+
var builder = new JsonAPIsConfigBuilder();
16+
if (options != null)
1817
{
19-
options(builder);
20-
_Config = builder.Build();
21-
}
22-
23-
var writableDoc = JsonNode.Parse(File.ReadAllText(_Config.JsonFilename));
24-
25-
// print API
26-
foreach (var elem in writableDoc?.Root.AsObject().AsEnumerable())
27-
{
28-
29-
var thisEntity = _Config.Tables.FirstOrDefault(t => t.Name.Equals(elem.Key, StringComparison.InvariantCultureIgnoreCase));
30-
if (thisEntity == null) continue;
31-
32-
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Get) == ApiMethodsToGenerate.Get)
33-
Console.WriteLine(string.Format("GET /{0}", elem.Key.ToLower()));
34-
35-
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.GetById) == ApiMethodsToGenerate.GetById)
36-
Console.WriteLine(string.Format("GET /{0}", elem.Key.ToLower()) + "/id");
37-
38-
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Insert) == ApiMethodsToGenerate.Insert)
39-
Console.WriteLine(string.Format("POST /{0}", elem.Key.ToLower()));
40-
41-
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Update) == ApiMethodsToGenerate.Update)
42-
Console.WriteLine(string.Format("PUT /{0}", elem.Key.ToLower()));
43-
44-
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Delete) == ApiMethodsToGenerate.Delete)
45-
Console.WriteLine(string.Format("DELETE /{0}", elem.Key.ToLower()) + "/id");
46-
47-
Console.WriteLine(" ");
48-
}
49-
50-
// setup routes
51-
foreach (var elem in writableDoc?.Root.AsObject().AsEnumerable())
52-
{
53-
54-
var thisEntity = _Config.Tables.FirstOrDefault(t => t.Name.Equals(elem.Key, StringComparison.InvariantCultureIgnoreCase));
55-
if (thisEntity == null) continue;
56-
57-
var arr = elem.Value.AsArray();
58-
59-
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Get) == ApiMethodsToGenerate.Get)
60-
app.MapGet(string.Format("/{0}", elem.Key), () => elem.Value.ToString());
61-
62-
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.GetById) == ApiMethodsToGenerate.GetById)
63-
app.MapGet(string.Format("/{0}", elem.Key) + "/{id}", (int id) =>
64-
{
65-
var matchedItem = arr.SingleOrDefault(row => row
66-
.AsObject()
67-
.Any(o => o.Key.ToLower() == "id" && int.Parse(o.Value.ToString()) == id)
68-
);
69-
return matchedItem;
70-
});
71-
72-
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Insert) == ApiMethodsToGenerate.Insert)
73-
app.MapPost(string.Format("/{0}", elem.Key), async (HttpRequest request) =>
74-
{
75-
string content = string.Empty;
76-
using (StreamReader reader = new StreamReader(request.Body))
77-
{
78-
content = await reader.ReadToEndAsync();
79-
}
80-
var newNode = JsonNode.Parse(content);
81-
var array = elem.Value.AsArray();
82-
newNode.AsObject().Add("Id", array.Count() + 1);
83-
array.Add(newNode);
84-
85-
File.WriteAllText(_Config.JsonFilename, writableDoc.ToString());
86-
return content;
87-
});
88-
89-
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Update) == ApiMethodsToGenerate.Update)
90-
app.MapPut(string.Format("/{0}", elem.Key), async (HttpRequest request) =>
18+
options(builder);
19+
_Config = builder.Build();
20+
}
21+
22+
var writableDoc = JsonNode.Parse(File.ReadAllText(_Config.JsonFilename))
23+
?? throw new Exception("Missing json file");
24+
var sets = writableDoc.Root?.AsObject()?.AsEnumerable();
25+
if (sets == null || !sets.Any()) return app;
26+
27+
// print API
28+
foreach (var elem in sets)
29+
{
30+
31+
var thisEntity = _Config.Tables.FirstOrDefault(t => elem.Key.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase));
32+
if (thisEntity == null) continue;
33+
34+
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Get) == ApiMethodsToGenerate.Get)
35+
Console.WriteLine(string.Format("GET /{0}", elem.Key.ToLower()));
36+
37+
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.GetById) == ApiMethodsToGenerate.GetById)
38+
Console.WriteLine(string.Format("GET /{0}", elem.Key.ToLower()) + "/id");
39+
40+
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Insert) == ApiMethodsToGenerate.Insert)
41+
Console.WriteLine(string.Format("POST /{0}", elem.Key.ToLower()));
42+
43+
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Update) == ApiMethodsToGenerate.Update)
44+
Console.WriteLine(string.Format("PUT /{0}", elem.Key.ToLower()));
45+
46+
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Delete) == ApiMethodsToGenerate.Delete)
47+
Console.WriteLine(string.Format("DELETE /{0}", elem.Key.ToLower()) + "/id");
48+
49+
Console.WriteLine(" ");
50+
}
51+
52+
// setup routes
53+
foreach (var elem in sets)
9154
{
92-
string content = string.Empty;
93-
using (StreamReader reader = new StreamReader(request.Body))
94-
{
95-
content = await reader.ReadToEndAsync();
96-
}
97-
var newNode = JsonNode.Parse(content);
98-
var array = elem.Value.AsArray();
99-
array.Add(newNode);
100-
101-
File.WriteAllText(_Config.JsonFilename, writableDoc.ToString());
102-
103-
return "OK";
104-
});
105-
106-
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Delete) == ApiMethodsToGenerate.Delete)
107-
app.MapDelete(string.Format("/{0}", elem.Key) + "/{id}", (int id) =>
108-
{
109-
110-
var matchedItem = arr
111-
.Select((value, index) => new { value, index })
112-
.SingleOrDefault(row => row.value
113-
.AsObject()
114-
.Any(o => o.Key.ToLower() == "id" && int.Parse(o.Value.ToString()) == id)
115-
);
116-
if (matchedItem != null)
117-
{
118-
arr.RemoveAt(matchedItem.index);
119-
File.WriteAllText(_Config.JsonFilename, writableDoc.ToString());
120-
}
121-
122-
return "OK";
123-
});
124-
125-
};
126-
127-
return app;
128-
}
55+
56+
var thisEntity = _Config.Tables.FirstOrDefault(t => elem.Key.Equals(t.Name, StringComparison.InvariantCultureIgnoreCase));
57+
if (thisEntity == null) continue;
58+
59+
var arr = elem.Value?.AsArray() ?? new JsonArray();
60+
61+
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Get) == ApiMethodsToGenerate.Get)
62+
app.MapGet(string.Format("/{0}", elem.Key), () => elem.Value?.ToString());
63+
64+
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.GetById) == ApiMethodsToGenerate.GetById)
65+
app.MapGet(string.Format("/{0}", elem.Key) + "/{id}", (int id) =>
66+
{
67+
var matchedItem = arr == null ? null :
68+
arr.SingleOrDefault(row => row != null && row
69+
.AsObject()
70+
.Any(o => o.Key.ToLower() == "id" && o.Value != null && int.Parse(o.Value.ToString()) == id)
71+
);
72+
return matchedItem == null ? Results.NotFound() : Results.Ok(matchedItem);
73+
});
74+
75+
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Insert) == ApiMethodsToGenerate.Insert)
76+
app.MapPost(string.Format("/{0}", elem.Key), async (HttpRequest request) =>
77+
{
78+
string content = string.Empty;
79+
using (StreamReader reader = new StreamReader(request.Body))
80+
{
81+
content = await reader.ReadToEndAsync();
82+
}
83+
var newNode = JsonNode.Parse(content);
84+
var array = elem.Value?.AsArray();
85+
if (newNode == null || array == null) return Results.NotFound();
86+
newNode.AsObject().Add("Id", array.Count() + 1);
87+
array.Add(newNode);
88+
89+
File.WriteAllText(_Config.JsonFilename, writableDoc.ToString());
90+
return Results.Ok(content);
91+
});
92+
93+
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Update) == ApiMethodsToGenerate.Update)
94+
app.MapPut(string.Format("/{0}", elem.Key), async (HttpRequest request) =>
95+
{
96+
string content = string.Empty;
97+
using (StreamReader reader = new StreamReader(request.Body))
98+
{
99+
content = await reader.ReadToEndAsync();
100+
}
101+
var newNode = JsonNode.Parse(content);
102+
var array = elem.Value?.AsArray();
103+
if (array != null)
104+
{
105+
array.Add(newNode);
106+
107+
File.WriteAllText(_Config.JsonFilename, writableDoc.ToString());
108+
}
109+
110+
return "OK";
111+
});
112+
113+
if ((thisEntity.ApiMethodsToGenerate & ApiMethodsToGenerate.Delete) == ApiMethodsToGenerate.Delete)
114+
app.MapDelete(string.Format("/{0}", elem.Key) + "/{id}", (int id) =>
115+
{
116+
117+
var matchedItem = arr
118+
.Select((value, index) => new { value, index })
119+
.SingleOrDefault(row => row.value
120+
?.AsObject()
121+
?.Any(o => o.Key.ToLower() == "id" && o.Value != null && int.Parse(o.Value.ToString()) == id)
122+
?? false
123+
);
124+
if (matchedItem != null)
125+
{
126+
arr.RemoveAt(matchedItem.index);
127+
File.WriteAllText(_Config.JsonFilename, writableDoc.ToString());
128+
}
129+
130+
return "OK";
131+
});
132+
133+
};
134+
135+
return app;
136+
}
129137
}

InstantAPIs/MapApiExtensions.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.ComponentModel.DataAnnotations;
66
using System.Reflection;
77
using Microsoft.Extensions.Logging;
8+
using Microsoft.Extensions.Logging.Abstractions;
89

910
namespace InstantAPIs;
1011

@@ -14,7 +15,7 @@ internal class MapApiExtensions
1415
// TODO: Authentication / Authorization
1516
private static Dictionary<Type, PropertyInfo> _IdLookup = new();
1617

17-
private static ILogger Logger;
18+
private static ILogger Logger = NullLogger.Instance;
1819

1920
internal static void Initialize<D,C>(ILogger logger)
2021
where D: DbContext
@@ -62,7 +63,7 @@ internal static void MapGetById<D,C>(IEndpointRouteBuilder app, string url)
6263
app.MapGet($"{url}/{{id}}", async ([FromServices] D db, [FromRoute] string id) =>
6364
{
6465

65-
C outValue = default(C);
66+
var outValue = default(C);
6667
if (idProp.PropertyType == typeof(Guid))
6768
outValue = await db.Set<C>().FindAsync(Guid.Parse(id));
6869
else if (idProp.PropertyType == typeof(int))
@@ -91,7 +92,7 @@ internal static void MapInstantPost<D, C>(IEndpointRouteBuilder app, string url)
9192
db.Add(newObj);
9293
await db.SaveChangesAsync();
9394
var id = _IdLookup[typeof(C)].GetValue(newObj);
94-
return Results.Created($"{url}/{id.ToString()}", newObj);
95+
return Results.Created($"{url}/{id}", newObj);
9596
});
9697

9798
}

0 commit comments

Comments
 (0)