Skip to content

Commit 6527058

Browse files
committed
Updated obsolete types for smoother transition
1 parent c9cd682 commit 6527058

File tree

17 files changed

+138
-31
lines changed

17 files changed

+138
-31
lines changed

docs

Submodule docs updated from 40fcf07 to 5414298

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Thinktecture.Unions;
1010
SwitchMethods = SwitchMapMethodsGeneration.DefaultWithPartialOverloads,
1111
MapMethods = SwitchMapMethodsGeneration.DefaultWithPartialOverloads)]
1212
[ObjectFactory<string>(UseForSerialization = SerializationFrameworks.All)]
13-
[JsonConverter(typeof(ThinktectureJsonConverterFactory<TextOrNumberSerializable, string, ValidationError>))]
13+
[JsonConverter(typeof(ThinktectureJsonConverterFactory))] // Optional, ThinktectureJsonConverterFactory can be registered with JsonSerializerOptions
1414
public partial class TextOrNumberSerializable :
1515
IObjectFactory<TextOrNumberSerializable, string, ValidationError>, // For deserialization
1616
IConvertible<string>, // For serialization

src/Thinktecture.Runtime.Extensions.AspNetCore/AspNetCore/ModelBinding/ThinktectureModelBinderProvider.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ public ValueObjectModelBinderProvider(
3636
/// <summary>
3737
/// Provider for creation of model binders for Smart Enums and for Value Objects with a key member.
3838
/// </summary>
39-
[ThinktectureRuntimeExtensionInternal]
4039
public class ThinktectureModelBinderProvider : IModelBinderProvider
4140
{
4241
private readonly bool _skipBindingFromBody;

src/Thinktecture.Runtime.Extensions.AspNetCore/AspNetCore/ModelBinding/TrimmingSmartEnumModelBinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Thinktecture.AspNetCore.ModelBinding;
55
/// </summary>
66
/// <typeparam name="T">Type of the value object.</typeparam>
77
/// <typeparam name="TValidationError">Type of the validation error.</typeparam>
8-
[Obsolete("Use 'mvcOptions.AddThinktectureModelBinding()' instead")]
8+
[Obsolete("Use 'ThinktectureModelBinderProvider' instead")]
99
public sealed class TrimmingSmartEnumModelBinder<T, TValidationError> : ThinktectureModelBinderBase<T, string, TValidationError>
1010
where T : IObjectFactory<T, string, TValidationError>
1111
where TValidationError : class, IValidationError<TValidationError>;

src/Thinktecture.Runtime.Extensions.AspNetCore/AspNetCore/ModelBinding/ValueObjectModelBinder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ namespace Thinktecture.AspNetCore.ModelBinding;
66
/// <typeparam name="T">Type of the value object.</typeparam>
77
/// <typeparam name="TKey">Type of the key member.</typeparam>
88
/// <typeparam name="TValidationError">Type of the validation error.</typeparam>
9-
[Obsolete("Use 'mvcOptions.AddThinktectureModelBinding()' instead")]
9+
[Obsolete("Use 'ThinktectureModelBinderProvider' instead")]
1010
public sealed class ValueObjectModelBinder<T, TKey, TValidationError> : ThinktectureModelBinderBase<T, TKey, TValidationError>
1111
where T : IObjectFactory<T, TKey, TValidationError>
1212
where TKey : notnull

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Thinktecture.EntityFrameworkCore.Storage.ValueConversion;
77
/// <summary>
88
/// Value converter for Smart Enums and for Value Objects with a key member.
99
/// </summary>
10-
[Obsolete("Use 'ThinktectureValueConverterFactory' instead.")]
10+
[Obsolete("Use 'AddThinktectureValueConverters()' or 'UseThinktectureValueConverters()' with corresponding builder instead.")]
1111
public sealed class ValueObjectValueConverterFactory : ThinktectureValueConverterFactory;
1212

1313
/// <summary>

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -137,15 +137,26 @@ public override bool CanConvert(Type typeToConvert)
137137
if (type is null)
138138
return false;
139139

140-
return !_skipObjectsWithJsonConverterAttribute || type.GetCustomAttribute<JsonConverterAttribute>() is null;
140+
if (!_skipObjectsWithJsonConverterAttribute)
141+
return true;
142+
143+
var jsonConverterAttribute = type.GetCustomAttribute<JsonConverterAttribute>();
144+
145+
if (jsonConverterAttribute is null)
146+
return true;
147+
148+
if (jsonConverterAttribute.ConverterType == typeof(ThinktectureJsonConverterFactory))
149+
return true;
150+
151+
return false;
141152
}
142153

143154
private static Type? GetObjectType(Type typeToConvert)
144155
{
145156
// typeToConvert could be derived type (like nested Smart Enum)
146157
var metadata = MetadataLookup.Find(typeToConvert);
147158

148-
if (metadata is not null)
159+
if (metadata is Metadata.Keyed)
149160
return metadata.Type;
150161

151162
if (typeToConvert.GetCustomAttributes<ObjectFactoryAttribute>().Any(a => a.UseForSerialization.HasFlag(SerializationFrameworks.SystemTextJson)))

src/Thinktecture.Runtime.Extensions.MessagePack/ThinktectureMessageFormatterResolver.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ static Cache()
9797
return;
9898
}
9999

100-
var validationErrorType = metadata?.ValidationErrorType ?? type.GetCustomAttribute<ValidationErrorAttribute>()?.Type ?? typeof(ValidationError);
100+
var validationErrorType = metadata?.ValidationErrorType
101+
?? type.GetCustomAttribute<ValidationErrorAttribute>()?.Type
102+
?? typeof(ValidationError);
101103

102104
var formatterTypeDefinition = type.IsClass
103105
? typeof(ThinktectureMessagePackFormatter<,,>)

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,7 @@ namespace Thinktecture.Json;
1212
public sealed class ValueObjectNewtonsoftJsonConverter<T, TKey, TValidationError> : ThinktectureNewtonsoftJsonConverter<T, TKey, TValidationError>
1313
where T : IObjectFactory<T, TKey, TValidationError>, IConvertible<TKey>
1414
where TKey : notnull
15-
where TValidationError : class, IValidationError<TValidationError>
16-
{
17-
}
15+
where TValidationError : class, IValidationError<TValidationError>;
1816

1917
/// <summary>
2018
/// <see cref="JsonConverter"/> for Value Objects.

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

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,69 @@ public sealed class ValueObjectNewtonsoftJsonConverter : ThinktectureNewtonsoftJ
1414
/// <summary>
1515
/// Non-generic converter for Value Objects.
1616
/// </summary>
17-
[ThinktectureRuntimeExtensionInternal]
1817
public class ThinktectureNewtonsoftJsonConverterFactory : JsonConverter
1918
{
2019
private static readonly ConcurrentDictionary<Type, JsonConverter> _cache = new();
2120

21+
private readonly bool _skipObjectsWithJsonConverterAttribute;
22+
23+
/// <summary>
24+
/// Initializes new instance of <see cref="ThinktectureNewtonsoftJsonConverterFactory"/>.
25+
/// </summary>
26+
public ThinktectureNewtonsoftJsonConverterFactory()
27+
: this(true)
28+
{
29+
}
30+
31+
/// <summary>
32+
/// Initializes new instance of <see cref="ThinktectureNewtonsoftJsonConverterFactory"/>.
33+
/// </summary>
34+
/// <param name="skipObjectsWithJsonConverterAttribute">
35+
/// Indication whether to skip value objects with <see cref="JsonConverterAttribute"/>.
36+
/// </param>
37+
public ThinktectureNewtonsoftJsonConverterFactory(
38+
bool skipObjectsWithJsonConverterAttribute)
39+
{
40+
_skipObjectsWithJsonConverterAttribute = skipObjectsWithJsonConverterAttribute;
41+
}
42+
2243
/// <inheritdoc />
2344
public override bool CanConvert(Type objectType)
2445
{
25-
return _cache.ContainsKey(objectType)
26-
|| MetadataLookup.Find(objectType) is not null
27-
|| objectType.GetCustomAttributes<ObjectFactoryAttribute>().Any(a => a.UseForSerialization.HasFlag(SerializationFrameworks.NewtonsoftJson));
46+
if (_cache.ContainsKey(objectType))
47+
return true;
48+
49+
var type = GetObjectType(objectType);
50+
51+
if (type is null)
52+
return false;
53+
54+
if (!_skipObjectsWithJsonConverterAttribute)
55+
return true;
56+
57+
var jsonConverterAttribute = type.GetCustomAttribute<JsonConverterAttribute>();
58+
59+
if (jsonConverterAttribute is null)
60+
return true;
61+
62+
if (jsonConverterAttribute.ConverterType == typeof(ThinktectureNewtonsoftJsonConverterFactory))
63+
return true;
64+
65+
return false;
66+
}
67+
68+
private static Type? GetObjectType(Type objectType)
69+
{
70+
// objectType could be derived type (like nested Smart Enum)
71+
var metadata = MetadataLookup.Find(objectType);
72+
73+
if (metadata is Metadata.Keyed)
74+
return metadata.Type;
75+
76+
if (objectType.GetCustomAttributes<ObjectFactoryAttribute>().Any(a => a.UseForSerialization.HasFlag(SerializationFrameworks.NewtonsoftJson)))
77+
return objectType;
78+
79+
return null;
2880
}
2981

3082
/// <inheritdoc />

0 commit comments

Comments
 (0)