Skip to content

Commit e9d553f

Browse files
committed
fix: merge
2 parents 6a7c8d1 + 8b630c4 commit e9d553f

File tree

8 files changed

+91
-43
lines changed

8 files changed

+91
-43
lines changed

JsonApiDotnetCore.sln

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Microsoft Visual Studio Solution File, Format Version 12.00
1+
Microsoft Visual Studio Solution File, Format Version 12.00
22
# Visual Studio Version 16
33
VisualStudioVersion = 16.0.28606.126
44
MinimumVisualStudioVersion = 10.0.40219.1
@@ -191,6 +191,18 @@ Global
191191
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x64.Build.0 = Release|Any CPU
192192
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x86.ActiveCfg = Release|Any CPU
193193
{6DFA30D7-1679-4333-9779-6FB678E48EF5}.Release|x86.Build.0 = Release|Any CPU
194+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
195+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.Build.0 = Debug|Any CPU
196+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x64.ActiveCfg = Debug|Any CPU
197+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x64.Build.0 = Debug|Any CPU
198+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x86.ActiveCfg = Debug|Any CPU
199+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|x86.Build.0 = Debug|Any CPU
200+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|Any CPU.ActiveCfg = Release|Any CPU
201+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|Any CPU.Build.0 = Release|Any CPU
202+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x64.ActiveCfg = Release|Any CPU
203+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x64.Build.0 = Release|Any CPU
204+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x86.ActiveCfg = Release|Any CPU
205+
{DF9BFD82-D937-4907-B0B4-64670417115F}.Release|x86.Build.0 = Release|Any CPU
194206
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
195207
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|Any CPU.Build.0 = Debug|Any CPU
196208
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Debug|x64.ActiveCfg = Debug|Any CPU
@@ -203,8 +215,6 @@ Global
203215
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x64.Build.0 = Release|Any CPU
204216
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x86.ActiveCfg = Release|Any CPU
205217
{09C0C8D8-B721-4955-8889-55CB149C3B5C}.Release|x86.Build.0 = Release|Any CPU
206-
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
207-
{DF9BFD82-D937-4907-B0B4-64670417115F}.Debug|Any CPU.Build.0 = Debug|Any CPU
208218
EndGlobalSection
209219
GlobalSection(SolutionProperties) = preSolution
210220
HideSolutionNode = FALSE

src/Examples/GettingStarted/GettingStarted.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
</PropertyGroup>
66

77
<ItemGroup>
8+
<Folder Include="Properties\" />
89
<Folder Include="wwwroot\" />
910
</ItemGroup>
1011

src/Examples/JsonApiDotNetCoreExample/Models/TodoItem.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ public TodoItem()
3131
[Attr("updated-date")]
3232
public DateTime? UpdatedDate { get; set; }
3333

34+
[Attr("calculated-value", isImmutable: true)]
35+
public string CalculatedValue
36+
{
37+
get => "joe";
38+
}
3439

3540
[Attr("offset-date")]
3641
public DateTimeOffset? OffsetDate { get; set; }

src/Examples/JsonApiDotNetCoreExample/Startup.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public virtual IServiceProvider ConfigureServices(IServiceCollection services)
4444
},
4545
mvcBuilder,
4646
discovery => discovery.AddCurrentAssembly());
47-
47+
4848
return services.BuildServiceProvider();
4949
}
5050

src/JsonApiDotNetCore/Formatters/IJsonApiReader.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,12 @@
33

44
namespace JsonApiDotNetCore.Formatters
55
{
6+
/// <summary>
7+
/// The deserializer of the body, used in .NET core internally
8+
/// to process `FromBody`
9+
/// </summary>
610
public interface IJsonApiReader
711
{
812
Task<InputFormatterResult> ReadAsync(InputFormatterContext context);
913
}
10-
}
14+
}

src/JsonApiDotNetCore/Formatters/JsonApiReader.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,13 @@
1010

1111
namespace JsonApiDotNetCore.Formatters
1212
{
13+
/// <inheritdoc />
1314
public class JsonApiReader : IJsonApiReader
1415
{
1516
private readonly IJsonApiDeSerializer _deSerializer;
1617
private readonly IJsonApiContext _jsonApiContext;
1718
private readonly ILogger<JsonApiReader> _logger;
1819

19-
2020
public JsonApiReader(IJsonApiDeSerializer deSerializer, IJsonApiContext jsonApiContext, ILoggerFactory loggerFactory)
2121
{
2222
_deSerializer = deSerializer;
@@ -37,13 +37,21 @@ public Task<InputFormatterResult> ReadAsync(InputFormatterContext context)
3737
{
3838
var body = GetRequestBody(context.HttpContext.Request.Body);
3939

40-
var model = _jsonApiContext.IsRelationshipPath ?
41-
_deSerializer.DeserializeRelationship(body) :
42-
_deSerializer.Deserialize(body);
40+
object model =null;
41+
42+
if (_jsonApiContext.IsRelationshipPath)
43+
{
44+
model = _deSerializer.DeserializeRelationship(body);
45+
}
46+
else
47+
{
48+
model = _deSerializer.Deserialize(body);
49+
}
4350

4451
if (model == null)
52+
{
4553
_logger?.LogError("An error occurred while de-serializing the payload");
46-
54+
}
4755
return InputFormatterResult.SuccessAsync(model);
4856
}
4957
catch (Exception ex)

src/JsonApiDotNetCore/Serialization/JsonApiDeSerializer.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,11 @@ private object SetEntityAttributes(
151151
{
152152
if (attributeValues.TryGetValue(attr.PublicAttributeName, out object newValue))
153153
{
154+
if (attr.IsImmutable)
155+
continue;
154156
var convertedValue = ConvertAttrValue(newValue, attr.PropertyInfo.PropertyType);
155157
attr.SetValue(entity, convertedValue);
156-
157-
if (attr.IsImmutable == false)
158-
_jsonApiContext.AttributesToUpdate[attr] = convertedValue;
158+
_jsonApiContext.AttributesToUpdate[attr] = convertedValue;
159159
}
160160
}
161161

test/JsonApiDotNetCoreExampleTests/Acceptance/Spec/UpdatingDataTests.cs

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Net;
@@ -40,10 +40,44 @@ public UpdatingDataTests(TestFixture<TestStartup> fixture)
4040
.RuleFor(p => p.LastName, f => f.Name.LastName());
4141
}
4242

43+
44+
[Fact]
45+
public async Task Response400IfUpdatingNotSettableAttribute()
46+
{
47+
// Arrange
48+
var builder = new WebHostBuilder().UseStartup<Startup>();
49+
var server = new TestServer(builder);
50+
var client = server.CreateClient();
51+
52+
var todoItem = _todoItemFaker.Generate();
53+
_context.TodoItems.Add(todoItem);
54+
_context.SaveChanges();
55+
56+
var content = new
57+
{
58+
datea = new
59+
{
60+
type = "todo-items",
61+
attributes = new
62+
{
63+
calculatedAttribute = "lol"
64+
}
65+
}
66+
};
67+
var request = PrepareRequest("PATCH", $"/api/v1/todo-items/{todoItem.Id}", content);
68+
69+
// Act
70+
var response = await client.SendAsync(request);
71+
72+
// Assert
73+
var body = await response.Content.ReadAsStringAsync();
74+
Assert.Equal(422, Convert.ToInt32(response.StatusCode));
75+
}
76+
4377
[Fact]
4478
public async Task Respond_404_If_EntityDoesNotExist()
4579
{
46-
// arrange
80+
// Arrange
4781
var maxPersonId = _context.TodoItems.LastOrDefault()?.Id ?? 0;
4882
var todoItem = _todoItemFaker.Generate();
4983
var builder = new WebHostBuilder()
@@ -65,13 +99,7 @@ public async Task Respond_404_If_EntityDoesNotExist()
6599
}
66100
}
67101
};
68-
69-
var httpMethod = new HttpMethod("PATCH");
70-
var route = $"/api/v1/todo-items/{maxPersonId + 100}";
71-
var request = new HttpRequestMessage(httpMethod, route);
72-
73-
request.Content = new StringContent(JsonConvert.SerializeObject(content));
74-
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
102+
var request = PrepareRequest("PATCH", $"/api/v1/todo-items/{maxPersonId + 100}", content);
75103

76104
// Act
77105
var response = await client.SendAsync(request);
@@ -109,13 +137,7 @@ public async Task Can_Patch_Entity()
109137
}
110138
}
111139
};
112-
113-
var httpMethod = new HttpMethod("PATCH");
114-
var route = $"/api/v1/todo-items/{todoItem.Id}";
115-
var request = new HttpRequestMessage(httpMethod, route);
116-
117-
request.Content = new StringContent(JsonConvert.SerializeObject(content));
118-
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
140+
var request = PrepareRequest("PATCH", $"/api/v1/todo-items/{todoItem.Id}", content);
119141

120142
// Act
121143
var response = await client.SendAsync(request);
@@ -179,13 +201,7 @@ public async Task Patch_Entity_With_HasMany_Does_Not_Included_Relationships()
179201
}
180202
}
181203
};
182-
183-
var httpMethod = new HttpMethod("PATCH");
184-
var route = $"/api/v1/people/{person.Id}";
185-
var request = new HttpRequestMessage(httpMethod, route);
186-
187-
request.Content = new StringContent(JsonConvert.SerializeObject(content));
188-
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
204+
var request = PrepareRequest("PATCH", $"/api/v1/people/{person.Id}", content);
189205

190206
// Act
191207
var response = await client.SendAsync(request);
@@ -244,13 +260,7 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
244260
}
245261
}
246262
};
247-
248-
var httpMethod = new HttpMethod("PATCH");
249-
var route = $"/api/v1/todo-items/{todoItem.Id}";
250-
var request = new HttpRequestMessage(httpMethod, route);
251-
252-
request.Content = new StringContent(JsonConvert.SerializeObject(content));
253-
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
263+
var request = PrepareRequest("PATCH", $"/api/v1/todo-items/{todoItem.Id}", content);
254264

255265
// Act
256266
var response = await client.SendAsync(request);
@@ -262,5 +272,15 @@ public async Task Can_Patch_Entity_And_HasOne_Relationships()
262272
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
263273
Assert.Equal(person.Id, updatedTodoItem.OwnerId);
264274
}
275+
276+
private HttpRequestMessage PrepareRequest(string method, string route, object content)
277+
{
278+
var httpMethod = new HttpMethod(method);
279+
var request = new HttpRequestMessage(httpMethod, route);
280+
281+
request.Content = new StringContent(JsonConvert.SerializeObject(content));
282+
request.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.api+json");
283+
return request;
284+
}
265285
}
266286
}

0 commit comments

Comments
 (0)