Fixes exception when converting UDIs in a PropertyValueConverter (#20011)
* Changed to use TryParse * Changed to be a null check instead * Update to "is false" syntax and add unit tests. --------- Co-authored-by: Andy Butland <abutland73@gmail.com>
This commit is contained in:
@@ -15,9 +15,14 @@ public sealed class JsonUdiConverter : JsonConverter<Udi>
|
|||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override Udi? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
public override Udi? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
|
||||||
=> reader.GetString() is string value
|
{
|
||||||
? UdiParser.Parse(value)
|
if (reader.GetString() is string value && string.IsNullOrWhiteSpace(value) is false)
|
||||||
: null;
|
{
|
||||||
|
return UdiParser.Parse(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
public override void Write(Utf8JsonWriter writer, Udi value, JsonSerializerOptions options)
|
public override void Write(Utf8JsonWriter writer, Udi value, JsonSerializerOptions options)
|
||||||
|
|||||||
@@ -12,25 +12,37 @@ namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Core.Deploy;
|
|||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class ArtifactBaseTests
|
public class ArtifactBaseTests
|
||||||
{
|
{
|
||||||
[Test]
|
private readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
|
||||||
public void CanSerialize()
|
|
||||||
{
|
|
||||||
var udi = new GuidUdi("test", Guid.Parse("3382d5433b5749d08919bc9961422a1f"));
|
|
||||||
var artifact = new TestArtifact(udi, new List<ArtifactDependency>()) { Name = "Test Name", Alias = "testAlias" };
|
|
||||||
|
|
||||||
var serialized = JsonSerializer.Serialize(artifact, new JsonSerializerOptions()
|
|
||||||
{
|
{
|
||||||
Converters =
|
Converters =
|
||||||
{
|
{
|
||||||
new JsonUdiConverter(),
|
new JsonUdiConverter(),
|
||||||
}
|
}
|
||||||
});
|
};
|
||||||
|
|
||||||
var expected =
|
[Test]
|
||||||
"{\"Udi\":\"umb://test/3382d5433b5749d08919bc9961422a1f\",\"Dependencies\":[],\"Checksum\":\"test checksum value\",\"Name\":\"Test Name\",\"Alias\":\"testAlias\"}";
|
public void Can_Serialize()
|
||||||
|
{
|
||||||
|
var udi = new GuidUdi("document", Guid.Parse("3382d5433b5749d08919bc9961422a1f"));
|
||||||
|
var artifact = new TestArtifact(udi, []) { Name = "Test Name", Alias = "testAlias" };
|
||||||
|
|
||||||
|
string serialized = JsonSerializer.Serialize(artifact, _jsonSerializerOptions);
|
||||||
|
|
||||||
|
var expected = "{\"Udi\":\"umb://document/3382d5433b5749d08919bc9961422a1f\",\"Dependencies\":[],\"Checksum\":\"test checksum value\",\"Name\":\"Test Name\",\"Alias\":\"testAlias\"}";
|
||||||
Assert.AreEqual(expected, serialized);
|
Assert.AreEqual(expected, serialized);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Can_Deserialize()
|
||||||
|
{
|
||||||
|
var serialized = "{\"Udi\":\"umb://document/3382d5433b5749d08919bc9961422a1f\",\"Dependencies\":[],\"Checksum\":\"test checksum value\",\"Name\":\"Test Name\",\"Alias\":\"testAlias\"}";
|
||||||
|
|
||||||
|
TestArtifact? deserialized = JsonSerializer.Deserialize<TestArtifact>(serialized, _jsonSerializerOptions);
|
||||||
|
Assert.IsNotNull(deserialized);
|
||||||
|
Assert.AreEqual("Test Name", deserialized.Name);
|
||||||
|
Assert.AreEqual("testAlias", deserialized.Alias);
|
||||||
|
}
|
||||||
|
|
||||||
[Test]
|
[Test]
|
||||||
public void Dependencies_Are_Correctly_Ordered()
|
public void Dependencies_Are_Correctly_Ordered()
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -0,0 +1,67 @@
|
|||||||
|
using System.Text.Json;
|
||||||
|
using NUnit.Framework;
|
||||||
|
using Umbraco.Cms.Core;
|
||||||
|
using Umbraco.Cms.Infrastructure.Serialization;
|
||||||
|
|
||||||
|
namespace Umbraco.Cms.Tests.UnitTests.Umbraco.Infrastructure.Serialization;
|
||||||
|
|
||||||
|
[TestFixture]
|
||||||
|
public class JsonUdiConverterTests
|
||||||
|
{
|
||||||
|
private readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
Converters =
|
||||||
|
{
|
||||||
|
new JsonUdiConverter(),
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Can_Serialize()
|
||||||
|
{
|
||||||
|
var udi = new GuidUdi("document", Guid.Parse("3382d5433b5749d08919bc9961422a1f"));
|
||||||
|
var artifact = new Test { Udi = udi };
|
||||||
|
|
||||||
|
string serialized = JsonSerializer.Serialize(artifact, _jsonSerializerOptions);
|
||||||
|
|
||||||
|
var expected = "{\"Udi\":\"umb://document/3382d5433b5749d08919bc9961422a1f\"}";
|
||||||
|
Assert.AreEqual(expected, serialized);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Can_Deserialize()
|
||||||
|
{
|
||||||
|
var serialized = "{\"Udi\":\"umb://document/3382d5433b5749d08919bc9961422a1f\"}";
|
||||||
|
|
||||||
|
Test? deserialized = JsonSerializer.Deserialize<Test>(serialized, _jsonSerializerOptions);
|
||||||
|
Assert.IsNotNull(deserialized);
|
||||||
|
Assert.AreEqual(Guid.Parse("3382d5433b5749d08919bc9961422a1f"), deserialized.Udi.Guid);
|
||||||
|
}
|
||||||
|
|
||||||
|
[TestCase(null)]
|
||||||
|
[TestCase("")]
|
||||||
|
[TestCase(" ")]
|
||||||
|
public void Will_Deserialize_To_Null_With_Null_Or_Whitepsace_Udi(string? serializedUdi)
|
||||||
|
{
|
||||||
|
var serializedUdiPart = serializedUdi is null ? "null" : $"\"{serializedUdi}\"";
|
||||||
|
var serialized = "{\"Udi\":" + serializedUdiPart + "}";
|
||||||
|
|
||||||
|
Test? deserialized = JsonSerializer.Deserialize<Test>(serialized, _jsonSerializerOptions);
|
||||||
|
Assert.IsNotNull(deserialized);
|
||||||
|
Assert.IsNull(deserialized.Udi);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Test]
|
||||||
|
public void Throws_On_Invalid_Udi()
|
||||||
|
{
|
||||||
|
var serialized = "{\"Udi\":\"invalid-udi\"}";
|
||||||
|
|
||||||
|
Assert.Throws<FormatException>(() =>
|
||||||
|
JsonSerializer.Deserialize<Test>(serialized, _jsonSerializerOptions));
|
||||||
|
}
|
||||||
|
|
||||||
|
private class Test
|
||||||
|
{
|
||||||
|
public GuidUdi? Udi { get; set; }
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user