Skip to content

Commit fbed1d3

Browse files
Code prep (#35457)
* Code prep * Code prep
1 parent 1c5b676 commit fbed1d3

File tree

9 files changed

+227
-0
lines changed

9 files changed

+227
-0
lines changed
Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
using JsonPatchSample.Models;
2+
using Microsoft.AspNetCore.JsonPatch;
3+
using Microsoft.AspNetCore.Mvc;
4+
using System.Collections.Generic;
5+
using System.Dynamic;
6+
7+
namespace JsonPatchSample.Controllers;
8+
9+
[Route("jsonpatch/[action]")]
10+
[ApiController]
11+
public class HomeController : ControllerBase
12+
{
13+
// <snippet_PatchAction>
14+
[HttpPatch]
15+
public IActionResult JsonPatchWithModelState(
16+
[FromBody] JsonPatchDocument<Customer> patchDoc)
17+
{
18+
if (patchDoc != null)
19+
{
20+
var customer = CreateCustomer();
21+
22+
patchDoc.ApplyTo(customer, ModelState);
23+
24+
if (!ModelState.IsValid)
25+
{
26+
return BadRequest(ModelState);
27+
}
28+
29+
return new ObjectResult(customer);
30+
}
31+
else
32+
{
33+
return BadRequest(ModelState);
34+
}
35+
}
36+
// </snippet_PatchAction>
37+
38+
[HttpPatch]
39+
public IActionResult JsonPatchWithModelStateAndPrefix(
40+
[FromBody] JsonPatchDocument<Customer> patchDoc,
41+
string prefix)
42+
{
43+
var customer = CreateCustomer();
44+
45+
patchDoc.ApplyTo(customer, ModelState, prefix);
46+
47+
if (!ModelState.IsValid)
48+
{
49+
return BadRequest(ModelState);
50+
}
51+
52+
return new ObjectResult(customer);
53+
}
54+
55+
[HttpPatch]
56+
public IActionResult JsonPatchWithoutModelState([FromBody] JsonPatchDocument<Customer> patchDoc)
57+
{
58+
var customer = CreateCustomer();
59+
60+
patchDoc.ApplyTo(customer);
61+
62+
return new ObjectResult(customer);
63+
}
64+
65+
[HttpPatch]
66+
public IActionResult JsonPatchForProduct([FromBody] JsonPatchDocument<Product> patchDoc)
67+
{
68+
var product = new Product();
69+
70+
patchDoc.ApplyTo(product);
71+
72+
return new ObjectResult(product);
73+
}
74+
75+
// <snippet_Dynamic>
76+
[HttpPatch]
77+
public IActionResult JsonPatchForDynamic([FromBody]JsonPatchDocument patch)
78+
{
79+
dynamic obj = new ExpandoObject();
80+
patch.ApplyTo(obj);
81+
82+
return Ok(obj);
83+
}
84+
// </snippet_Dynamic>
85+
86+
private Customer CreateCustomer()
87+
{
88+
return new Customer
89+
{
90+
CustomerName = "John",
91+
Orders = new List<Order>()
92+
{
93+
new Order
94+
{
95+
OrderName = "Order0"
96+
},
97+
new Order
98+
{
99+
OrderName = "Order1"
100+
}
101+
}
102+
};
103+
}
104+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
3+
<PropertyGroup>
4+
<TargetFramework>net6.0</TargetFramework>
5+
<Nullable>enable</Nullable>
6+
<ImplicitUsings>enable</ImplicitUsings>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<Folder Include="Controllers\" />
11+
</ItemGroup>
12+
13+
<ItemGroup>
14+
<PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="6.0.2" />
15+
</ItemGroup>
16+
17+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
using Newtonsoft.Json;
2+
3+
namespace JsonPatchSample.Models
4+
{
5+
public class Category
6+
{
7+
public string CategoryName { get; set; }
8+
}
9+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace JsonPatchSample.Models;
2+
3+
public class Customer
4+
{
5+
public string? CustomerName { get; set; }
6+
public List<Order>? Orders { get; set; }
7+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace JsonPatchSample.Models;
2+
3+
public class Order
4+
{
5+
public string OrderName { get; set; }
6+
public string OrderType { get; set; }
7+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace JsonPatchSample.Models
2+
{
3+
public class Product
4+
{
5+
public string ProductName { get; set; }
6+
public Category ProductCategory { get; set; }
7+
}
8+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using Microsoft.AspNetCore.Mvc;
2+
using Microsoft.AspNetCore.Mvc.Formatters;
3+
using Microsoft.Extensions.Options;
4+
5+
namespace JsonPatchSample;
6+
7+
public static class MyJPIF
8+
{
9+
public static NewtonsoftJsonPatchInputFormatter GetJsonPatchInputFormatter()
10+
{
11+
var builder = new ServiceCollection()
12+
.AddLogging()
13+
.AddMvc()
14+
.AddNewtonsoftJson()
15+
.Services.BuildServiceProvider();
16+
17+
return builder
18+
.GetRequiredService<IOptions<MvcOptions>>()
19+
.Value
20+
.InputFormatters
21+
.OfType<NewtonsoftJsonPatchInputFormatter>()
22+
.First();
23+
}
24+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
#define BOTH // FIRST BOTH
2+
#if NEVER
3+
#elif FIRST
4+
// <snippet1>
5+
var builder = WebApplication.CreateBuilder(args);
6+
7+
builder.Services.AddControllers()
8+
.AddNewtonsoftJson();
9+
10+
var app = builder.Build();
11+
12+
app.UseHttpsRedirection();
13+
14+
app.UseAuthorization();
15+
16+
app.MapControllers();
17+
18+
app.Run();
19+
// </snippet1>
20+
#elif BOTH
21+
// <snippet_both>
22+
using JsonPatchSample;
23+
using Microsoft.AspNetCore.Mvc.Formatters;
24+
25+
var builder = WebApplication.CreateBuilder(args);
26+
27+
builder.Services.AddControllers(options =>
28+
{
29+
options.InputFormatters.Insert(0, MyJPIF.GetJsonPatchInputFormatter());
30+
});
31+
32+
var app = builder.Build();
33+
34+
app.UseHttpsRedirection();
35+
36+
app.UseAuthorization();
37+
38+
app.MapControllers();
39+
40+
app.Run();
41+
// </snippet_both>
42+
#endif
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)