Skip to content

Commit

Permalink
Code changes to hide STJ serializer implementation behind a boolean f…
Browse files Browse the repository at this point in the history
…lag.
  • Loading branch information
kundadebdatta committed Jul 19, 2024
1 parent bfdb850 commit 93a0f9c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 117 deletions.
21 changes: 19 additions & 2 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,15 @@ public ConnectionMode ConnectionMode
/// <seealso cref="ItemRequestOptions.EnableContentResponseOnWrite"/>
/// <seealso cref="TransactionalBatchItemRequestOptions.EnableContentResponseOnWrite"/>
public bool? EnableContentResponseOnWrite { get; set; }

/// <summary>
/// Gets or sets the boolean flag to indicate if the default STJ serializer <see cref="CosmosSystemTextJsonSerializer"/> needed to be
/// used for JSON serialization.
/// </summary>
/// <value>
/// The default value is false
/// </value>
public bool UseSystemTextJsonForSerialization { get; set; } = false;

/// <summary>
/// Gets or sets the advanced replica selection flag. The advanced replica selection logic keeps track of the replica connection
Expand Down Expand Up @@ -583,6 +592,11 @@ public CosmosSerializer Serializer
throw new ArgumentException(
$"{nameof(this.Serializer)} is not compatible with {nameof(this.SerializerOptions)}. Only one can be set. ");
}

if (this.UseSystemTextJsonForSerialization)
{
throw new ArgumentException($"Cannot set a custom {nameof(this.Serializer)} when {nameof(this.UseSystemTextJsonForSerialization)} is enabled. Either specify a custom or set {nameof(this.UseSystemTextJsonForSerialization)} to the default STJ serializer.");
}

this.serializerInternal = value;
}
Expand Down Expand Up @@ -833,8 +847,11 @@ internal Func<X509Certificate2, X509Chain, SslPolicyErrors, bool> GetServerCerti
internal void SetSerializerIfNotConfigured(CosmosSerializer serializer)
{
if (this.serializerInternal == null)
{
this.serializerInternal = serializer ?? throw new ArgumentNullException(nameof(serializer));
{
this.serializerInternal = this.UseSystemTextJsonForSerialization
? new CosmosSystemTextJsonSerializer(
new System.Text.Json.JsonSerializerOptions())
: serializer ?? throw new ArgumentNullException(nameof(serializer));
}
}

Expand Down
10 changes: 10 additions & 0 deletions Microsoft.Azure.Cosmos/src/Fluent/CosmosClientBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,16 @@ internal CosmosClientBuilder WithPartitionLevelFailoverEnabled()
this.clientOptions.EnablePartitionLevelFailover = true;
return this;
}

/// <summary>
/// Enables the usage of <see cref="CosmosSystemTextJsonSerializer"/> as the default
/// serializer.
/// </summary>
internal CosmosClientBuilder WithSystemTextJsonSerializerEnabled()
{
this.clientOptions.UseSystemTextJsonForSerialization = true;
return this;
}

/// <summary>
/// Enables SDK to inject fault. Used for testing applications.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace Microsoft.Azure.Cosmos
/// <summary>
/// This class provides a default implementation of System.Text.Json Cosmos Linq Serializer.
/// </summary>
public class CosmosSystemTextJsonSerializer : CosmosLinqSerializer
internal class CosmosSystemTextJsonSerializer : CosmosLinqSerializer
{
/// <summary>
/// A read-only instance of <see cref="JsonSerializerOptions"/>.
Expand All @@ -25,7 +25,7 @@ public class CosmosSystemTextJsonSerializer : CosmosLinqSerializer
/// with the default values for the Cosmos SDK
/// </summary>
/// <param name="jsonSerializerOptions">An instance of <see cref="JsonSerializerOptions"/> containing the json serialization options.</param>
public CosmosSystemTextJsonSerializer(
internal CosmosSystemTextJsonSerializer(
JsonSerializerOptions jsonSerializerOptions)
{
this.jsonSerializerOptions = jsonSerializerOptions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public class LinqAggregateCustomSerializationBaseline : BaselineTests<LinqAggreg
private static Cosmos.Database testDb;
private static Container testContainer;

private static CosmosSerializer stjCosmosSerializer;
private static CosmosClient stjClient;
private static Cosmos.Database testDbSTJ;
private static Container testContainerSTJ;
Expand Down Expand Up @@ -64,10 +63,8 @@ public async static Task Initialize(TestContext textContext)
testDb = await client.CreateDatabaseAsync(dbName);
testContainer = testDb.CreateContainerAsync(new ContainerProperties(id: Guid.NewGuid().ToString(), partitionKeyPath: "/Pk")).Result;

stjCosmosSerializer = new CosmosSystemTextJsonSerializer(new JsonSerializerOptions());

stjClient = TestCommon.CreateCosmosClient((cosmosClientBuilder)
=> cosmosClientBuilder.WithCustomSerializer(stjCosmosSerializer));
=> cosmosClientBuilder.WithSystemTextJsonSerializerEnabled());

// Set a callback to get the handle of the last executed query to do the verification
// This is neede because aggregate queries return type is a scalar so it can't be used
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public async static Task Initialize(TestContext textContext)
TestDb = await CosmosClient.CreateDatabaseAsync(dbName);

CosmosDefaultSTJClient = TestCommon.CreateCosmosClient((cosmosClientBuilder)
=> cosmosClientBuilder.WithCustomSerializer(new CosmosSystemTextJsonSerializer(new JsonSerializerOptions())));
=> cosmosClientBuilder.WithSystemTextJsonSerializerEnabled());

string dbNameSTJ = $"{nameof(LinqTranslationBaselineTests)}-{Guid.NewGuid():N}";
TestDbSTJDefault = await CosmosDefaultSTJClient.CreateDatabaseAsync(dbNameSTJ);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2711,11 +2711,23 @@
],
"MethodInfo": "Boolean get_LimitToEndpoint();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Boolean get_UseSystemTextJsonForSerialization()[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "Boolean get_UseSystemTextJsonForSerialization();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Boolean LimitToEndpoint": {
"Type": "Property",
"Attributes": [],
"MethodInfo": "Boolean LimitToEndpoint;CanRead:True;CanWrite:True;Boolean get_LimitToEndpoint();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;Void set_LimitToEndpoint(Boolean);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Boolean UseSystemTextJsonForSerialization": {
"Type": "Property",
"Attributes": [],
"MethodInfo": "Boolean UseSystemTextJsonForSerialization;CanRead:True;CanWrite:True;Boolean get_UseSystemTextJsonForSerialization();IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;Void set_UseSystemTextJsonForSerialization(Boolean);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Int32 GatewayModeMaxConnectionLimit": {
"Type": "Property",
"Attributes": [],
Expand Down Expand Up @@ -3165,6 +3177,13 @@
],
"MethodInfo": "Void set_TokenCredentialBackgroundRefreshInterval(System.Nullable`1[System.TimeSpan]);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Void set_UseSystemTextJsonForSerialization(Boolean)[System.Runtime.CompilerServices.CompilerGeneratedAttribute()]": {
"Type": "Method",
"Attributes": [
"CompilerGeneratedAttribute"
],
"MethodInfo": "Void set_UseSystemTextJsonForSerialization(Boolean);IsAbstract:False;IsStatic:False;IsVirtual:False;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"Void set_WebProxy(System.Net.IWebProxy)": {
"Type": "Method",
"Attributes": [],
Expand Down Expand Up @@ -3407,34 +3426,7 @@
"NestedTypes": {}
},
"Microsoft.Azure.Cosmos.CosmosLinqSerializer;Microsoft.Azure.Cosmos.CosmosSerializer;IsAbstract:True;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
"Subclasses": {
"Microsoft.Azure.Cosmos.CosmosSystemTextJsonSerializer;Microsoft.Azure.Cosmos.CosmosLinqSerializer;IsAbstract:False;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
"Subclasses": {},
"Members": {
"System.IO.Stream ToStream[T](T)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.IO.Stream ToStream[T](T);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
},
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"T FromStream[T](System.IO.Stream)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "T FromStream[T](System.IO.Stream);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
},
"Void .ctor(System.Text.Json.JsonSerializerOptions)": {
"Type": "Constructor",
"Attributes": [],
"MethodInfo": "[Void .ctor(System.Text.Json.JsonSerializerOptions), Void .ctor(System.Text.Json.JsonSerializerOptions)]"
}
},
"NestedTypes": {}
}
},
"Subclasses": {},
"Members": {
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
"Type": "Method",
Expand Down Expand Up @@ -3685,65 +3677,12 @@
"Microsoft.Azure.Cosmos.CosmosSerializer;System.Object;IsAbstract:True;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
"Subclasses": {
"Microsoft.Azure.Cosmos.CosmosLinqSerializer;Microsoft.Azure.Cosmos.CosmosSerializer;IsAbstract:True;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
"Subclasses": {
"Microsoft.Azure.Cosmos.CosmosSystemTextJsonSerializer;Microsoft.Azure.Cosmos.CosmosLinqSerializer;IsAbstract:False;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
"Subclasses": {},
"Members": {
"System.IO.Stream ToStream[T](T)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.IO.Stream ToStream[T](T);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
},
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"T FromStream[T](System.IO.Stream)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "T FromStream[T](System.IO.Stream);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
},
"Void .ctor(System.Text.Json.JsonSerializerOptions)": {
"Type": "Constructor",
"Attributes": [],
"MethodInfo": "[Void .ctor(System.Text.Json.JsonSerializerOptions), Void .ctor(System.Text.Json.JsonSerializerOptions)]"
}
},
"NestedTypes": {}
}
},
"Members": {
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
}
},
"NestedTypes": {}
},
"Microsoft.Azure.Cosmos.CosmosSystemTextJsonSerializer;Microsoft.Azure.Cosmos.CosmosLinqSerializer;IsAbstract:False;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
"Subclasses": {},
"Members": {
"System.IO.Stream ToStream[T](T)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.IO.Stream ToStream[T](T);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
},
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"T FromStream[T](System.IO.Stream)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "T FromStream[T](System.IO.Stream);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
},
"Void .ctor(System.Text.Json.JsonSerializerOptions)": {
"Type": "Constructor",
"Attributes": [],
"MethodInfo": "[Void .ctor(System.Text.Json.JsonSerializerOptions), Void .ctor(System.Text.Json.JsonSerializerOptions)]"
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:True;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
}
},
"NestedTypes": {}
Expand All @@ -3763,32 +3702,6 @@
},
"NestedTypes": {}
},
"Microsoft.Azure.Cosmos.CosmosSystemTextJsonSerializer;Microsoft.Azure.Cosmos.CosmosLinqSerializer;IsAbstract:False;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
"Subclasses": {},
"Members": {
"System.IO.Stream ToStream[T](T)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.IO.Stream ToStream[T](T);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
},
"System.String SerializeMemberName(System.Reflection.MemberInfo)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "System.String SerializeMemberName(System.Reflection.MemberInfo);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:False;IsConstructor:False;IsFinal:False;"
},
"T FromStream[T](System.IO.Stream)": {
"Type": "Method",
"Attributes": [],
"MethodInfo": "T FromStream[T](System.IO.Stream);IsAbstract:False;IsStatic:False;IsVirtual:True;IsGenericMethod:True;IsConstructor:False;IsFinal:False;"
},
"Void .ctor(System.Text.Json.JsonSerializerOptions)": {
"Type": "Constructor",
"Attributes": [],
"MethodInfo": "[Void .ctor(System.Text.Json.JsonSerializerOptions), Void .ctor(System.Text.Json.JsonSerializerOptions)]"
}
},
"NestedTypes": {}
},
"Microsoft.Azure.Cosmos.CosmosThresholdOptions;System.Object;IsAbstract:False;IsSealed:False;IsInterface:False;IsEnum:False;IsClass:True;IsValueType:False;IsNested:False;IsGenericType:False;IsSerializable:False": {
"Subclasses": {},
"Members": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -501,6 +501,19 @@ public void ThrowOnCustomSerializerWithSerializerOptions()

options.Serializer = new CosmosJsonDotNetSerializer();
}

[TestMethod]
[ExpectedException(typeof(ArgumentException))]
public void ThrowOnCustomSerializerWithSTJSerializerEnabled()
{
CosmosClientOptions options = new CosmosClientOptions()
{
Serializer = new CosmosJsonDotNetSerializer()
};

options.SerializerOptions = new CosmosSerializationOptions();
options.UseSystemTextJsonForSerialization = true;
}

[TestMethod]
[ExpectedException(typeof(ArgumentNullException))]
Expand Down

0 comments on commit 93a0f9c

Please sign in to comment.