Skip to content

Commit 16cf4fc

Browse files
Address PR review feedback: use shared test classes, consistent RootContract pattern, remove cache invalidation
Co-authored-by: StephenMolloy <19562826+StephenMolloy@users.noreply.github.com>
1 parent 1070770 commit 16cf4fc

3 files changed

Lines changed: 28 additions & 77 deletions

File tree

src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/Json/DataContractJsonSerializer.cs

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,12 +105,7 @@ internal DataContractJsonSerializer(Type type,
105105
internal ISerializationSurrogateProvider? SerializationSurrogateProvider
106106
{
107107
get { return _serializationSurrogateProvider; }
108-
set
109-
{
110-
_serializationSurrogateProvider = value;
111-
// Reset _rootContract when surrogate provider changes so it gets recalculated
112-
_rootContract = null;
113-
}
108+
set { _serializationSurrogateProvider = value; }
114109
}
115110

116111
public bool IgnoreExtensionDataObject
@@ -193,12 +188,7 @@ private DataContract RootContract
193188
{
194189
if (_rootContract == null)
195190
{
196-
Type contractType = _rootType;
197-
if (_serializationSurrogateProvider != null)
198-
{
199-
contractType = DataContractSurrogateCaller.GetDataContractType(_serializationSurrogateProvider, _rootType);
200-
}
201-
_rootContract = DataContract.GetDataContract(contractType);
191+
_rootContract = DataContract.GetDataContract((_serializationSurrogateProvider == null) ? _rootType : GetSurrogatedType(_serializationSurrogateProvider, _rootType));
202192
CheckIfTypeIsReference(_rootContract);
203193
}
204194
return _rootContract;
@@ -625,9 +615,6 @@ private void Initialize(Type type,
625615
_serializeReadOnlyTypes = serializeReadOnlyTypes;
626616
_dateTimeFormat = dateTimeFormat;
627617
_useSimpleDictionaryFormat = useSimpleDictionaryFormat;
628-
629-
// Reset _rootContract when parameters change so it gets recalculated with surrogate
630-
_rootContract = null;
631618
}
632619

633620
[MemberNotNull(nameof(_rootType))]
@@ -667,5 +654,12 @@ internal static DataContract GetDataContract(DataContract declaredTypeContract,
667654
CheckIfTypeIsReference(contract);
668655
return contract;
669656
}
657+
658+
[RequiresDynamicCode(DataContract.SerializerAOTWarning)]
659+
[RequiresUnreferencedCode(DataContract.SerializerTrimmerWarning)]
660+
internal static Type GetSurrogatedType(ISerializationSurrogateProvider serializationSurrogateProvider, Type type)
661+
{
662+
return DataContractSurrogateCaller.GetDataContractType(serializationSurrogateProvider, DataContract.UnwrapNullableType(type));
663+
}
670664
}
671665
}

src/libraries/System.Runtime.Serialization.Json/tests/DataContractJsonSerializer.cs

Lines changed: 12 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -3128,14 +3128,14 @@ private static string GetAmString(DateTimeFormat dateTimeFormat)
31283128
[Fact]
31293129
public static void DCJS_SerializationSurrogateProvider_PassedToInternalSerializer()
31303130
{
3131-
// Setup: Create a test surrogate that transforms TestClassA to TestClassB
3132-
var surrogateProvider = new TestSurrogateProvider();
3131+
// Setup: Create a test surrogate that transforms NonSerializablePerson
3132+
var surrogateProvider = new MyPersonSurrogateProvider();
31333133

31343134
// Create the serializer and set the surrogate provider
3135-
var serializer = new DataContractJsonSerializer(typeof(TestClassA));
3135+
var serializer = new DataContractJsonSerializer(typeof(NonSerializablePerson));
31363136
serializer.SetSerializationSurrogateProvider(surrogateProvider);
31373137

3138-
var testObj = new TestClassA { Value = "OriginalValue" };
3138+
var testObj = new NonSerializablePerson("John", 30);
31393139

31403140
// Act: Serialize the object
31413141
byte[] serializedData;
@@ -3146,72 +3146,22 @@ public static void DCJS_SerializationSurrogateProvider_PassedToInternalSerialize
31463146
}
31473147

31483148
// The surrogate should have been called during serialization
3149-
Assert.True(surrogateProvider.SerializationSurrogateWasCalled, "Serialization surrogate should have been called during WriteObject");
3149+
Assert.True(surrogateProvider.GetSurrogateTypeWasCalled, "GetSurrogateType should have been called during WriteObject");
3150+
Assert.True(surrogateProvider.GetObjectToSerializeWasCalled, "GetObjectToSerialize should have been called during WriteObject");
31503151

31513152
// Act: Deserialize the object
3152-
TestClassA deserializedObj;
3153+
NonSerializablePerson deserializedObj;
31533154
using (var ms = new MemoryStream(serializedData))
31543155
{
3155-
deserializedObj = (TestClassA)serializer.ReadObject(ms);
3156+
deserializedObj = (NonSerializablePerson)serializer.ReadObject(ms);
31563157
}
31573158

31583159
// The surrogate should have been called during deserialization
3159-
Assert.True(surrogateProvider.DeserializationSurrogateWasCalled, "Deserialization surrogate should have been called during ReadObject");
3160+
Assert.True(surrogateProvider.GetDeserializedObjectWasCalled, "GetDeserializedObject should have been called during ReadObject");
31603161

3161-
// Verify the surrogate transformation was applied
3162-
Assert.Equal("TransformedValue", deserializedObj.Value);
3162+
// Verify the object was properly deserialized
3163+
Assert.Equal("John", deserializedObj.Name);
3164+
Assert.Equal(30, deserializedObj.Age);
31633165
}
31643166

3165-
// Test classes for surrogate provider testing
3166-
[DataContract]
3167-
public class TestClassA
3168-
{
3169-
[DataMember]
3170-
public string Value { get; set; }
3171-
}
3172-
3173-
[DataContract]
3174-
public class TestClassB
3175-
{
3176-
[DataMember]
3177-
public string TransformedValue { get; set; }
3178-
}
3179-
3180-
// Test surrogate provider implementation
3181-
public class TestSurrogateProvider : ISerializationSurrogateProvider
3182-
{
3183-
public bool SerializationSurrogateWasCalled { get; private set; }
3184-
public bool DeserializationSurrogateWasCalled { get; private set; }
3185-
3186-
public Type GetSurrogateType(Type type)
3187-
{
3188-
if (type == typeof(TestClassA))
3189-
{
3190-
return typeof(TestClassB);
3191-
}
3192-
return type;
3193-
}
3194-
3195-
public object GetObjectToSerialize(object obj, Type targetType)
3196-
{
3197-
SerializationSurrogateWasCalled = true;
3198-
3199-
if (obj is TestClassA testA && targetType == typeof(TestClassB))
3200-
{
3201-
return new TestClassB { TransformedValue = testA.Value };
3202-
}
3203-
return obj;
3204-
}
3205-
3206-
public object GetDeserializedObject(object obj, Type targetType)
3207-
{
3208-
DeserializationSurrogateWasCalled = true;
3209-
3210-
if (obj is TestClassB testB && targetType == typeof(TestClassA))
3211-
{
3212-
return new TestClassA { Value = "TransformedValue" };
3213-
}
3214-
return obj;
3215-
}
3216-
}
32173167
}

src/libraries/System.Runtime.Serialization.Xml/tests/SerializationTypes.RuntimeOnly.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2319,8 +2319,13 @@ public object GetDeserializedObject(object obj, Type targetType)
23192319

23202320
public class MyPersonSurrogateProvider : ISerializationSurrogateProvider
23212321
{
2322+
public bool GetSurrogateTypeWasCalled { get; private set; }
2323+
public bool GetObjectToSerializeWasCalled { get; private set; }
2324+
public bool GetDeserializedObjectWasCalled { get; private set; }
2325+
23222326
public Type GetSurrogateType(Type type)
23232327
{
2328+
GetSurrogateTypeWasCalled = true;
23242329
if (type == typeof(NonSerializablePerson))
23252330
{
23262331
return typeof(NonSerializablePersonSurrogate);
@@ -2337,6 +2342,7 @@ public Type GetSurrogateType(Type type)
23372342

23382343
public object GetDeserializedObject(object obj, Type targetType)
23392344
{
2345+
GetDeserializedObjectWasCalled = true;
23402346
if (obj is NonSerializablePersonSurrogate)
23412347
{
23422348
NonSerializablePersonSurrogate person = (NonSerializablePersonSurrogate)obj;
@@ -2353,6 +2359,7 @@ public object GetDeserializedObject(object obj, Type targetType)
23532359

23542360
public object GetObjectToSerialize(object obj, Type targetType)
23552361
{
2362+
GetObjectToSerializeWasCalled = true;
23562363
if (obj is NonSerializablePerson)
23572364
{
23582365
NonSerializablePerson nsp = (NonSerializablePerson)obj;

0 commit comments

Comments
 (0)