Skip to content

Commit c126635

Browse files
committed
Simplified examples
1 parent d647e27 commit c126635

File tree

12 files changed

+16
-110
lines changed

12 files changed

+16
-110
lines changed

docs

Submodule docs updated from 0d6f549 to 54f216e

samples/Thinktecture.Runtime.Extensions.AspNetCore.Samples/BoundaryWithJsonConverter.cs

Lines changed: 0 additions & 16 deletions
This file was deleted.

samples/Thinktecture.Runtime.Extensions.AspNetCore.Samples/Controllers/DemoController.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,12 @@ public IActionResult RoundTrip(ProductCategory category)
2222
return RoundTripValidatableEnum(category);
2323
}
2424

25-
[HttpGet("categoryWithConverter/{category}")]
26-
public IActionResult RoundTrip(ProductCategoryWithJsonConverter category)
27-
{
28-
return RoundTripValidatableEnum(category);
29-
}
30-
3125
[HttpGet("group/{group}")]
3226
public IActionResult RoundTrip(ProductGroup group)
3327
{
3428
return RoundTripValidatableEnum(group);
3529
}
3630

37-
[HttpGet("groupWithConverter/{group}")]
38-
public IActionResult RoundTrip(ProductGroupWithJsonConverter group)
39-
{
40-
return RoundTripValidatableEnum(group);
41-
}
42-
4331
[HttpGet("productType/{productType}")]
4432
public IActionResult RoundTrip(ProductType productType)
4533
{
@@ -75,12 +63,6 @@ public IActionResult RoundTripPost([FromBody] ProductTypeWrapper productType)
7563
return RoundTripInternal(productType.ProductType);
7664
}
7765

78-
[HttpGet("productTypeWithJsonConverter/{productType}")]
79-
public IActionResult RoundTrip(ProductTypeWithJsonConverter productType)
80-
{
81-
return RoundTripInternal(productType);
82-
}
83-
8466
[HttpGet("productName/{name}")]
8567
public IActionResult RoundTrip(ProductName? name)
8668
{
@@ -125,17 +107,6 @@ public IActionResult RoundTripPost([FromBody] OtherProductName? name)
125107
return Json(name);
126108
}
127109

128-
[HttpPost("boundary")]
129-
public IActionResult RoundTrip([FromBody] BoundaryWithJsonConverter boundary)
130-
{
131-
if (!ModelState.IsValid)
132-
return BadRequest(ModelState);
133-
134-
_logger.LogInformation("Round trip test with {Type}: {Boundary}", boundary.GetType().Name, boundary);
135-
136-
return Json(boundary);
137-
}
138-
139110
[HttpGet("enddate/{endDate}")]
140111
public IActionResult RoundTripGet(OpenEndDate endDate)
141112
{

samples/Thinktecture.Runtime.Extensions.AspNetCore.Samples/ProductCategoryWithJsonConverter.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

samples/Thinktecture.Runtime.Extensions.AspNetCore.Samples/ProductGroupWithJsonConverter.cs

Lines changed: 0 additions & 29 deletions
This file was deleted.

samples/Thinktecture.Runtime.Extensions.AspNetCore.Samples/ProductTypeWithJsonConverter.cs

Lines changed: 0 additions & 8 deletions
This file was deleted.

samples/Thinktecture.Runtime.Extensions.AspNetCore.Samples/Program.cs

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@ private static async Task DoHttpRequestsAsync(ILogger logger, bool forMinimalWeb
4545
using var client = new HttpClient();
4646

4747
await DoRequestAsync(logger, client, "category/fruits");
48-
await DoRequestAsync(logger, client, "categoryWithConverter/fruits");
4948
await DoRequestAsync(logger, client, "group/1");
5049
await DoRequestAsync(logger, client, "group/42"); // invalid
5150
await DoRequestAsync(logger, client, "group/invalid"); // invalid
52-
await DoRequestAsync(logger, client, "groupWithConverter/1");
53-
await DoRequestAsync(logger, client, "groupWithConverter/42"); // invalid
5451
await DoRequestAsync(logger, client, "productType/groceries");
5552
await DoRequestAsync(logger, client, "productType?productType=groceries");
5653
await DoRequestAsync(logger, client, "productType", "groceries");
@@ -63,8 +60,6 @@ private static async Task DoHttpRequestsAsync(ILogger logger, bool forMinimalWeb
6360

6461
await DoRequestAsync(logger, client, "productType", "invalid"); // invalid
6562
await DoRequestAsync(logger, client, "productTypeWrapper", new { ProductType = "invalid" }); // invalid
66-
await DoRequestAsync(logger, client, "productTypeWithJsonConverter/groceries");
67-
await DoRequestAsync(logger, client, "productTypeWithJsonConverter/invalid"); // invalid
6863

6964
await DoRequestAsync(logger, client, "productName/bread");
7065
await DoRequestAsync(logger, client, "productName/a"); // invalid (Product name cannot be 1 character long.)
@@ -79,7 +74,7 @@ private static async Task DoHttpRequestsAsync(ILogger logger, bool forMinimalWeb
7974
await DoRequestAsync(logger, client, "otherProductName", "bread");
8075
await DoRequestAsync(logger, client, "otherProductName", "a"); // invalid (Product name cannot be 1 character long.)
8176

82-
await DoRequestAsync(logger, client, "boundary", BoundaryWithJsonConverter.Create(1, 2));
77+
await DoRequestAsync(logger, client, "boundary", Boundary.Create(1, 2));
8378
await DoRequestAsync(logger, client, "boundary", jsonBody: "{ \"lower\": 2, \"upper\": 1 }");
8479

8580
await DoRequestAsync(logger, client, $"enddate/{DateOnly.FromDateTime(DateTime.Now):O}");
@@ -150,9 +145,7 @@ private static Task StartMinimalWebApiAsync(ILoggerFactory loggerFactory)
150145
var routeGroup = app.MapGroup("/api");
151146

152147
routeGroup.MapGet("category/{category}", (ProductCategory category) => new { Value = category, category.IsValid });
153-
routeGroup.MapGet("categoryWithConverter/{category}", (ProductCategoryWithJsonConverter category) => new { Value = category, category.IsValid });
154148
routeGroup.MapGet("group/{group}", (ProductGroup group) => new { Value = group, group.IsValid });
155-
routeGroup.MapGet("groupWithConverter/{group}", (ProductGroupWithJsonConverter group) => new { Value = group, group.IsValid });
156149
routeGroup.MapGet("productType/{productType}", (ProductType productType) => productType);
157150
routeGroup.MapGet("productType", (ProductType productType) => productType);
158151
routeGroup.MapGet("productTypeWithFilter", (BoundValueObject<ProductType, ProductTypeValidationError> productType) => ValueTask.FromResult(productType.Value))
@@ -168,12 +161,11 @@ private static Task StartMinimalWebApiAsync(ILoggerFactory loggerFactory)
168161
routeGroup.MapGet("boundaryWithFactories/{boundary}", (BoundaryWithFactories boundary) => boundary);
169162
routeGroup.MapPost("productType", ([FromBody] ProductType productType) => productType);
170163
routeGroup.MapPost("productTypeWrapper", ([FromBody] ProductTypeWrapper productType) => productType);
171-
routeGroup.MapGet("productTypeWithJsonConverter/{productType}", (ProductTypeWithJsonConverter productType) => productType);
172164
routeGroup.MapGet("productName/{name}", (ProductName name) => name);
173165
routeGroup.MapPost("productName", ([FromBody] ProductName name) => name);
174166
routeGroup.MapGet("otherProductName/{name}", (OtherProductName? name) => name);
175167
routeGroup.MapPost("otherProductName", ([FromBody] OtherProductName name) => name);
176-
routeGroup.MapPost("boundary", ([FromBody] BoundaryWithJsonConverter boundary) => boundary);
168+
routeGroup.MapPost("boundary", ([FromBody] Boundary boundary) => boundary);
177169

178170
routeGroup.MapGet("enddate/{date}", (OpenEndDate date) => date);
179171
routeGroup.MapPost("enddate", ([FromBody] OpenEndDate date) => date);

samples/Thinktecture.Runtime.Extensions.Samples/SmartEnums/ProductGroup.cs

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,6 @@ public int Do(string foo)
2020
return 42;
2121
}
2222

23-
// ReSharper disable UnusedParameterInPartialMethod
24-
static partial void ValidateConstructorArguments(ref int key, bool isValid, ref string displayName, ref ProductCategory category)
25-
{
26-
}
27-
// ReSharper restore UnusedParameterInPartialMethod
28-
2923
private static ProductGroup CreateInvalidItem(int key)
3024
{
3125
// the values can be anything besides the key,

samples/Thinktecture.Runtime.Extensions.Samples/Thinktecture.Runtime.Extensions.Samples.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
</PropertyGroup>
88
<ItemGroup>
99
<ProjectReference Include="..\..\src\Thinktecture.Runtime.Extensions\Thinktecture.Runtime.Extensions.csproj" />
10+
<ProjectReference Include="..\..\src\Thinktecture.Runtime.Extensions.Json\Thinktecture.Runtime.Extensions.Json.csproj" />
1011
<ProjectReference Include="..\..\src\Thinktecture.Runtime.Extensions.SourceGenerator\Thinktecture.Runtime.Extensions.SourceGenerator.csproj" SetTargetFramework="TargetFramework=netstandard2.0" ReferenceOutputAssembly="false" OutputItemType="Analyzer" />
1112
</ItemGroup>
1213
<ItemGroup>

samples/Thinktecture.Runtime.Extensions.Samples/Unions/DiscriminatedUnionsDemos.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
using System;
21
using System.ComponentModel.DataAnnotations;
32
using System.Text.Json;
43
using Serilog;
@@ -223,5 +222,14 @@ private static void DemoForJurisdiction(ILogger logger)
223222
);
224223

225224
logger.Information(description);
225+
226+
// Json serialization
227+
var json = JsonSerializer.Serialize<Jurisdiction>(district);
228+
logger.Information(json); // {"$type":"District","value":"District 42"}
229+
230+
var deserializedJurisdiction = JsonSerializer.Deserialize<Jurisdiction>(json);
231+
logger.Information("Deserialized jurisdiction: {Jurisdiction} ({Type})",
232+
deserializedJurisdiction,
233+
deserializedJurisdiction?.GetType().Name);
226234
}
227235
}

0 commit comments

Comments
 (0)