Skip to content

Commit 273d292

Browse files
committed
MessagePack serializers check for "AllowDefaultStructs" and for null + fixed typo
1 parent 6981699 commit 273d292

File tree

98 files changed

+841
-234
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

98 files changed

+841
-234
lines changed

docs

Submodule docs updated from a480f5d to 40fcf07

samples/Thinktecture.Runtime.Extensions.MessagePack.Samples/TextOrNumberSerializableWithFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Thinktecture;
1313
[MessagePackFormatter(typeof(Formatters.ValueObjectMessagePackFormatter<TextOrNumberSerializableWithFormatter, string, ValidationError>))] // Optional: otherwise ValueObjectMessageFormatterResolver is required
1414
public partial class TextOrNumberSerializableWithFormatter :
1515
IValueObjectFactory<TextOrNumberSerializableWithFormatter, string, ValidationError>, // For deserialization
16-
IValueObjectConvertable<string>, // For serialization
16+
IValueObjectConvertible<string>, // For serialization
1717
IParsable<TextOrNumberSerializableWithFormatter> // For Minimal API and ASP.NET Core model binding validation
1818
{
1919
// For serialization

samples/Thinktecture.Runtime.Extensions.Samples/Unions/TextOrNumberSerializable.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Thinktecture.Unions;
1313
[JsonConverter(typeof(ValueObjectJsonConverterFactory<TextOrNumberSerializable, string, ValidationError>))] // Optional: otherwise ValueObjectJsonConverterFactory is required
1414
public partial class TextOrNumberSerializable :
1515
IValueObjectFactory<TextOrNumberSerializable, string, ValidationError>, // For deserialization
16-
IValueObjectConvertable<string>, // For serialization
16+
IValueObjectConvertible<string>, // For serialization
1717
IParsable<TextOrNumberSerializable> // For Minimal API and ASP.NET Core model binding validation
1818
{
1919
// For serialization

samples/Thinktecture.Runtime.Extensions.Samples/ValueObjects/BoundaryWithFactories.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ static partial void ValidateFactoryArguments(ref ValidationError? validationErro
4141
}
4242

4343
/// <summary>
44-
/// Custom implementation of "IValueObjectConvertable&amp;lt;string&amp;gt;"
44+
/// Custom implementation of "IValueObjectConvertible&amp;lt;string&amp;gt;"
4545
/// requested by "ValueObjectFactory&lt;string&gt;".
4646
/// </summary>
4747
public string ToValue()

src/Thinktecture.Runtime.Extensions.EntityFrameworkCore.Sources/EntityFrameworkCore/Storage/ValueConversion/ValueObjectValueConverterFactory.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ public sealed class ValueObjectValueConverterFactory
1717
/// <typeparam name="TKey">Type of the key.</typeparam>
1818
/// <returns>An instance of <see cref="ValueConverter{T,TKey}"/>></returns>
1919
public static ValueConverter<T, TKey> Create<T, TKey>(bool useConstructorForRead = true)
20-
where T : IValueObjectFactory<TKey>, IValueObjectConvertable<TKey>
20+
where T : IValueObjectFactory<TKey>, IValueObjectConvertible<TKey>
2121
where TKey : notnull
2222
{
2323
return new ValueObjectValueConverter<T, TKey>(useConstructorForRead);
@@ -31,7 +31,7 @@ public static ValueConverter<T, TKey> Create<T, TKey>(bool useConstructorForRead
3131
/// <typeparam name="TKey">Type of the key.</typeparam>
3232
/// <returns>An instance of <see cref="ValueConverter{TEnum,TKey}"/>></returns>
3333
public static ValueConverter<TEnum, TKey> CreateForValidatableEnum<TEnum, TKey>(bool validateOnWrite)
34-
where TEnum : IValidatableEnum, IValueObjectFactory<TKey>, IValueObjectConvertable<TKey>
34+
where TEnum : IValidatableEnum, IValueObjectFactory<TKey>, IValueObjectConvertible<TKey>
3535
where TKey : notnull
3636
{
3737
return new ValidatableEnumValueConverter<TEnum, TKey>(validateOnWrite);
@@ -81,7 +81,7 @@ internal static ValueConverter Create(
8181
}
8282

8383
private static Expression<Func<TKey, T>> GetConverterFromKey<T, TKey>(bool useConstructor)
84-
where T : IValueObjectFactory<TKey>, IValueObjectConvertable<TKey>
84+
where T : IValueObjectFactory<TKey>, IValueObjectConvertible<TKey>
8585
where TKey : notnull
8686
{
8787
var metadata = KeyedValueObjectMetadataLookup.Find(typeof(T));
@@ -102,7 +102,7 @@ private static T Convert<T>(object value)
102102
}
103103

104104
private sealed class ValueObjectValueConverter<T, TKey> : ValueConverter<T, TKey>
105-
where T : IValueObjectFactory<TKey>, IValueObjectConvertable<TKey>
105+
where T : IValueObjectFactory<TKey>, IValueObjectConvertible<TKey>
106106
where TKey : notnull
107107
{
108108
/// <inheritdoc />
@@ -122,7 +122,7 @@ public ValueObjectValueConverter(bool useConstructor)
122122
}
123123

124124
private sealed class ValidatableEnumValueConverter<TEnum, TKey> : ValueConverter<TEnum, TKey>
125-
where TEnum : IValidatableEnum, IValueObjectFactory<TKey>, IValueObjectConvertable<TKey>
125+
where TEnum : IValidatableEnum, IValueObjectFactory<TKey>, IValueObjectConvertible<TKey>
126126
where TKey : notnull
127127
{
128128
/// <inheritdoc />

src/Thinktecture.Runtime.Extensions.Json/Text/Json/Serialization/ValueObjectJsonConverter.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Thinktecture.Text.Json.Serialization;
1010
/// <typeparam name="TKey">Type of the key.</typeparam>
1111
/// <typeparam name="TValidationError">Type of the validation error.</typeparam>
1212
public sealed class ValueObjectJsonConverter<T, TKey, TValidationError> : JsonConverter<T>
13-
where T : IValueObjectFactory<T, TKey, TValidationError>, IValueObjectConvertable<TKey>
13+
where T : IValueObjectFactory<T, TKey, TValidationError>, IValueObjectConvertible<TKey>
1414
where TKey : notnull
1515
where TValidationError : class, IValidationError<TValidationError>
1616
{
@@ -71,7 +71,7 @@ public override void Write(Utf8JsonWriter writer, T value, JsonSerializerOptions
7171
/// <typeparam name="T">Type of the value object.</typeparam>
7272
/// <typeparam name="TValidationError">Type of the validation error.</typeparam>
7373
public sealed class ValueObjectJsonConverter<T, TValidationError> : JsonConverter<T>
74-
where T : IValueObjectFactory<T, string, TValidationError>, IValueObjectConvertable<string>
74+
where T : IValueObjectFactory<T, string, TValidationError>, IValueObjectConvertible<string>
7575
where TValidationError : class, IValidationError<TValidationError>
7676
{
7777
private static readonly bool _mayReturnInvalidObjects = typeof(IValidatableEnum).IsAssignableFrom(typeof(T));

src/Thinktecture.Runtime.Extensions.Json/Text/Json/Serialization/ValueObjectJsonConverterFactory.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Thinktecture.Text.Json.Serialization;
99
/// Factory for creation of <see cref="ValueObjectJsonConverter{T,TKey,TValidationError}"/>.
1010
/// </summary>
1111
public sealed class ValueObjectJsonConverterFactory<T, TKey, TValidationError> : JsonConverterFactory
12-
where T : IValueObjectFactory<T, TKey, TValidationError>, IValueObjectConvertable<TKey>
12+
where T : IValueObjectFactory<T, TKey, TValidationError>, IValueObjectConvertible<TKey>
1313
where TKey : notnull
1414
where TValidationError : class, IValidationError<TValidationError>
1515
{
@@ -33,7 +33,7 @@ public override JsonConverter CreateConverter(Type typeToConvert, JsonSerializer
3333
/// Factory for creation of <see cref="ValueObjectJsonConverter{T,TKey,TValidationError}"/>.
3434
/// </summary>
3535
public sealed class ValueObjectJsonConverterFactory<T, TValidationError> : JsonConverterFactory
36-
where T : IValueObjectFactory<T, string, TValidationError>, IValueObjectConvertable<string>
36+
where T : IValueObjectFactory<T, string, TValidationError>, IValueObjectConvertible<string>
3737
where TValidationError : class, IValidationError<TValidationError>
3838
{
3939
/// <inheritdoc />

src/Thinktecture.Runtime.Extensions.MessagePack/Formatters/StructValueObjectMessagePackFormatter.cs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ namespace Thinktecture.Formatters;
1212
/// <typeparam name="TKey">Type of the key.</typeparam>
1313
/// <typeparam name="TValidationError">Type of the validation error.</typeparam>
1414
public sealed class StructValueObjectMessagePackFormatter<T, TKey, TValidationError> : IMessagePackFormatter<T>, IMessagePackFormatter<T?>
15-
where T : struct, IValueObjectFactory<T, TKey, TValidationError>, IValueObjectConvertable<TKey>
15+
where T : struct, IValueObjectFactory<T, TKey, TValidationError>, IValueObjectConvertible<TKey>
1616
where TKey : notnull
1717
where TValidationError : class, IValidationError<TValidationError>
1818
{
1919
private static readonly bool _mayReturnInvalidObjects = typeof(IValidatableEnum).IsAssignableFrom(typeof(T));
20+
private static readonly bool _disallowDefaultValues = typeof(IDisallowDefaultValue).IsAssignableFrom(typeof(T));
21+
private static readonly TKey? _keyDefaultValue = default;
2022

2123
/// <inheritdoc />
2224
public void Serialize(ref MessagePackWriter writer, T value, MessagePackSerializerOptions options)
@@ -41,8 +43,19 @@ public void Serialize(ref MessagePackWriter writer, T? value, MessagePackSeriali
4143
/// <inheritdoc />
4244
public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
4345
{
44-
if (!TryReadKey(ref reader, options, out var key))
46+
var formatter = options.Resolver.GetFormatterWithVerify<TKey?>();
47+
var key = formatter.Deserialize(ref reader, options);
48+
49+
if (key is null)
50+
{
51+
if (_disallowDefaultValues)
52+
throw new MessagePackSerializationException($"Cannot convert null to type \"{typeof(T).Name}\" because it doesn't allow default values.");
53+
4554
return default;
55+
}
56+
57+
if (_disallowDefaultValues && key.Equals(_keyDefaultValue))
58+
throw new MessagePackSerializationException($"Cannot convert null to type \"{typeof(T).Name}\" because it doesn't allow default values.");
4659

4760
return Deserialize(key);
4861
}
@@ -51,7 +64,7 @@ public T Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions
5164
T? IMessagePackFormatter<T?>.Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options)
5265
{
5366
if (!TryReadKey(ref reader, options, out var key))
54-
return default;
67+
return default; // nullable struct
5568

5669
return Deserialize(key);
5770
}

src/Thinktecture.Runtime.Extensions.MessagePack/Formatters/ValueObjectMessagePackFormatter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace Thinktecture.Formatters;
1111
/// <typeparam name="TKey">Type of the key.</typeparam>
1212
/// <typeparam name="TValidationError">Type of the validation error.</typeparam>
1313
public sealed class ValueObjectMessagePackFormatter<T, TKey, TValidationError> : IMessagePackFormatter<T?>
14-
where T : class, IValueObjectFactory<T, TKey, TValidationError>, IValueObjectConvertable<TKey>
14+
where T : class, IValueObjectFactory<T, TKey, TValidationError>, IValueObjectConvertible<TKey>
1515
where TKey : notnull
1616
where TValidationError : class, IValidationError<TValidationError>
1717
{

src/Thinktecture.Runtime.Extensions.Newtonsoft.Json/Json/ValueObjectNewtonsoftJsonConverter_T2.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace Thinktecture.Json;
99
/// <typeparam name="TKey">Type of the key.</typeparam>
1010
/// <typeparam name="TValidationError">Type of the validation error.</typeparam>
1111
public sealed class ValueObjectNewtonsoftJsonConverter<T, TKey, TValidationError> : JsonConverter
12-
where T : IValueObjectFactory<T, TKey, TValidationError>, IValueObjectConvertable<TKey>
12+
where T : IValueObjectFactory<T, TKey, TValidationError>, IValueObjectConvertible<TKey>
1313
where TKey : notnull
1414
where TValidationError : class, IValidationError<TValidationError>
1515
{

0 commit comments

Comments
 (0)