|
| 1 | +using AwesomeAssertions; |
| 2 | +using Speckle.Newtonsoft.Json; |
| 3 | +using Speckle.Sdk.Host; |
| 4 | +using Speckle.Sdk.Models; |
| 5 | +using Speckle.Sdk.Pipelines.Send; |
| 6 | + |
| 7 | +namespace Speckle.Sdk.Tests.Unit.Pipelines.Send.Serialization; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Tests that the <see cref="JsonIgnoreAttribute"/> leads to properties being ignored both from the final JSON output, |
| 11 | +/// But also from the id calculation |
| 12 | +/// </summary> |
| 13 | +[Collection(nameof(RequiresTypeLoaderCollection))] |
| 14 | +public sealed class JsonIgnoreRespected |
| 15 | +{ |
| 16 | + private readonly Serializer _sut = new(); |
| 17 | + |
| 18 | + public JsonIgnoreRespected() |
| 19 | + { |
| 20 | + TypeLoader.ReInitialize(typeof(Base).Assembly, typeof(IgnoreTest).Assembly); |
| 21 | + } |
| 22 | + |
| 23 | + public static IEnumerable<object[]> IgnoredTestCases() |
| 24 | + { |
| 25 | + const string EXPECTED_PAYLOAD = "this should have been included"; |
| 26 | + const string EXPECTED_HASH = "c24fe8fad993b1f500e65315e9284afd"; |
| 27 | + yield return ["this should have been ignored", EXPECTED_PAYLOAD, EXPECTED_HASH]; |
| 28 | + yield return ["again, ignored!", EXPECTED_PAYLOAD, EXPECTED_HASH]; |
| 29 | + yield return ["this one is not", EXPECTED_PAYLOAD, EXPECTED_HASH]; |
| 30 | + } |
| 31 | + |
| 32 | + public static IEnumerable<object[]> IgnoredCompoundTestCases() |
| 33 | + { |
| 34 | + const string EXPECTED_PAYLOAD = "this should have been included"; |
| 35 | + const string EXPECTED_HASH = "aa78478569795e0ed8df656e792a4ee8"; |
| 36 | + yield return ["this should have been ignored", EXPECTED_PAYLOAD, EXPECTED_HASH]; |
| 37 | + yield return ["again, ignored!", EXPECTED_PAYLOAD, EXPECTED_HASH]; |
| 38 | + yield return ["this one is not", EXPECTED_PAYLOAD, EXPECTED_HASH]; |
| 39 | + } |
| 40 | + |
| 41 | + [Theory] |
| 42 | + [MemberData(nameof(IgnoredTestCases))] |
| 43 | + public void IgnoredProperties_NotIncludedInJson(string ignoredPayload, string expectedPayload, string expectedHash) |
| 44 | + { |
| 45 | + IgnoreTest testData = new(ignoredPayload, expectedPayload); |
| 46 | + |
| 47 | + UploadItem result = _sut.Serialize(testData).ToArray().First(); |
| 48 | + result.SpeckleType.Should().Be(testData.speckle_type); |
| 49 | + |
| 50 | + string jsonString = result.Json.ToJsonString(); |
| 51 | + jsonString.Should().NotContain(nameof(testData.ShouldBeIgnored)); |
| 52 | + jsonString.Should().NotContain(ignoredPayload); |
| 53 | + |
| 54 | + jsonString.Should().Contain(nameof(testData.ShouldBeIncluded)); |
| 55 | + jsonString.Should().Contain(expectedPayload); |
| 56 | + |
| 57 | + result.Id.Should().Be(expectedHash); |
| 58 | + } |
| 59 | + |
| 60 | + [Theory] |
| 61 | + [MemberData(nameof(IgnoredCompoundTestCases))] |
| 62 | + public void IgnoredProperties_Compound_NotIncludedInJson( |
| 63 | + string ignoredPayload, |
| 64 | + string expectedPayload, |
| 65 | + string expectedHash |
| 66 | + ) |
| 67 | + { |
| 68 | + IgnoredCompoundTest testData = new(ignoredPayload, expectedPayload); |
| 69 | + |
| 70 | + UploadItem[] results = _sut.Serialize(testData).ToArray(); |
| 71 | + UploadItem result = results[0]; |
| 72 | + result.SpeckleType.Should().Be(testData.speckle_type); |
| 73 | + result.Reference.closure.Should().NotBeNull(); |
| 74 | + |
| 75 | + foreach (UploadItem child in results) |
| 76 | + { |
| 77 | + string jsonString = child.Json.ToJsonString(); |
| 78 | + jsonString.Should().NotContain(nameof(testData.ShouldBeIgnored)); |
| 79 | + jsonString.Should().NotContain(ignoredPayload); |
| 80 | + |
| 81 | + jsonString.Should().Contain(nameof(testData.ShouldBeIncluded)); |
| 82 | + jsonString.Should().Contain(expectedPayload); |
| 83 | + } |
| 84 | + |
| 85 | + result.Id.Should().Be(expectedHash); |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +[SpeckleType("Speckle.Sdk.Test.Unit.Serialisation.IgnoredCompoundTest")] |
| 90 | +public sealed class IgnoredCompoundTest(string ignoredPayload, string expectedPayload) : Base |
| 91 | +{ |
| 92 | + [JsonIgnore] |
| 93 | + public Base ShouldBeIgnored => new IgnoreTest(ignoredPayload, expectedPayload) { ["override"] = ignoredPayload }; |
| 94 | + |
| 95 | + public Base ShouldBeIncluded => new IgnoreTest(ignoredPayload, expectedPayload); |
| 96 | + |
| 97 | + [JsonIgnore, DetachProperty] |
| 98 | + public Base ShouldBeIgnoredDetached => ShouldBeIgnored; |
| 99 | + |
| 100 | + [DetachProperty] |
| 101 | + public Base ShouldBeIncludedDetached => ShouldBeIncluded; |
| 102 | + |
| 103 | + [JsonIgnore] |
| 104 | + public List<Base> ShouldBeIgnoredList => [ShouldBeIgnored]; |
| 105 | + |
| 106 | + [JsonIgnore, DetachProperty] |
| 107 | + public List<Base> ShouldBeIgnoredDetachedList => ShouldBeIgnoredList; |
| 108 | + |
| 109 | + public List<Base> ShouldBeIncludedList => [ShouldBeIncluded]; |
| 110 | + |
| 111 | + [DetachProperty] |
| 112 | + public List<Base> ShouldBeIncludedDetachedList => ShouldBeIncludedList; |
| 113 | +} |
| 114 | + |
| 115 | +[SpeckleType("Speckle.Sdk.Tests.Unit.Serialisation.IgnoreTest")] |
| 116 | +public sealed class IgnoreTest(string shouldBeIgnoredPayload, string shouldBeIncludedPayload) : Base |
| 117 | +{ |
| 118 | + [JsonIgnore] |
| 119 | + public string ShouldBeIgnored => shouldBeIgnoredPayload; |
| 120 | + |
| 121 | + public string ShouldBeIncluded => shouldBeIncludedPayload; |
| 122 | +} |
0 commit comments