Skip to content

Commit cfd345a

Browse files
authored
Use [JsonIgnore] to skip properties. (#126)
1 parent 2b82af7 commit cfd345a

3 files changed

Lines changed: 77 additions & 1 deletion

File tree

src/SenseNet.Client.Tests/UnitTests/ContentSavingTests.cs

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,70 @@ private async Task<IDictionary<string, object>> UpdateStronglyTypedTest<T>(Actio
381381

382382
/* =================================================================== CUSTOM PROPERTIES */
383383

384+
private class TestContent_IgnoredProperties : Content
385+
{
386+
public TestContent_IgnoredProperties(IRestCaller restCaller, ILogger<Content> logger) : base(restCaller, logger) { }
387+
388+
public int Index { get; set; }
389+
public string String1 { get; set; }
390+
[JsonIgnore]
391+
public string String2 { get; set; }
392+
[System.Text.Json.Serialization.JsonIgnore]
393+
public string String3 { get; set; }
394+
}
395+
[TestMethod]
396+
public async Task Content_T_IgnoredProperty_Save()
397+
{
398+
var restCaller = CreateRestCallerFor(@"{
399+
""d"": {
400+
""Id"": 999543,
401+
""Name"": ""MyContent"",
402+
""Path"": ""/Root/MyContent"",
403+
""Type"": ""Folder"",
404+
}
405+
}");
406+
407+
var repositories = GetRepositoryCollection(services =>
408+
{
409+
services.RegisterGlobalContentType<TestContent_IgnoredProperties>();
410+
services.AddSingleton(restCaller);
411+
});
412+
var repository = await repositories.GetRepositoryAsync(FakeServer, CancellationToken.None)
413+
.ConfigureAwait(false);
414+
415+
// ACT
416+
var content = repository.CreateContent<TestContent_IgnoredProperties>("/Root/Content", null, "MyContent-1");
417+
content.Index = 9998;
418+
content.String1 = "String1Value";
419+
content.String2 = "String2Value";
420+
content.String3 = "String3Value";
421+
await content.SaveAsync(CancellationToken.None).ConfigureAwait(false);
422+
423+
// ASSERT
424+
var calls = restCaller.ReceivedCalls().ToArray();
425+
Assert.IsNotNull(calls);
426+
Assert.AreEqual(2, calls.Length);
427+
Assert.AreEqual("GetResponseStringAsync", calls[1].GetMethodInfo().Name);
428+
var arguments = calls[1].GetArguments();
429+
Assert.IsTrue(arguments[0].ToString().Contains("Root('Content')"));
430+
Assert.AreEqual(HttpMethod.Post, arguments[1]);
431+
var json = (string)arguments[2]!;
432+
json = json.Substring("models=[".Length).TrimEnd(']');
433+
dynamic data = JsonHelper.Deserialize(json);
434+
Dictionary<string, object> fields = data.ToObject<Dictionary<string, object>>();
435+
436+
var names = string.Join(", ", fields.Keys.OrderBy(x => x));
437+
Assert.AreEqual("__ContentType, Existing, Index, Name, String1", names);
438+
Assert.IsNotNull(data);
439+
Assert.AreEqual("TestContent_IgnoredProperties", data.__ContentType.ToString());
440+
Assert.AreEqual("MyContent-1", data.Name.ToString());
441+
Assert.AreEqual("False", data.Existing.ToString());
442+
Assert.AreEqual("9998", data.Index.ToString());
443+
Assert.AreEqual("String1Value", data.String1.ToString());
444+
}
445+
446+
/* =================================================================== CUSTOM PROPERTIES */
447+
384448
#region Nested classes: CustomType1, TestContent_CustomProperties
385449
private class CustomType1
386450
{

src/SenseNet.Client/Content_Properties.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,7 @@ private void ManagePostData(IDictionary<string, object> postData)
281281
{
282282
var originalFields = (JObject)_responseContent;
283283

284-
foreach (var property in this.GetType().GetProperties())
284+
foreach (var property in this.GetType().GetProperties().Where(p=>!IsIgnored(p)))
285285
{
286286
if (_skippedProperties.Contains(property.Name))
287287
continue;
@@ -309,6 +309,17 @@ private void ManagePostData(IDictionary<string, object> postData)
309309
}
310310
}
311311

312+
private readonly Type[] _ignoreAttributeTypes = {
313+
typeof(JsonIgnoreAttribute), typeof(System.Text.Json.Serialization.JsonIgnoreAttribute)
314+
};
315+
316+
private bool IsIgnored(PropertyInfo property)
317+
{
318+
return property.GetCustomAttributes()
319+
.Select(a => a.GetType())
320+
.Any(t => _ignoreAttributeTypes.Contains(t));
321+
}
322+
312323
private object ConvertFromReferredContents(string propertyName, Type propertyType, object propertyValue)
313324
{
314325
if (propertyValue == null)

src/SenseNet.Client/SenseNet.Client.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
4242
<PackageReference Include="SenseNet.Tools" Version="3.2.12" />
4343
<PackageReference Include="System.IdentityModel.Tokens.Jwt" Version="6.15.1" />
44+
<PackageReference Include="System.Text.Json" Version="8.0.0" />
4445
</ItemGroup>
4546

4647
</Project>

0 commit comments

Comments
 (0)