Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Serializer: Add SDK serializer to Client.ClientOptions.Serializer #1454

Merged
merged 6 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Microsoft.Azure.Cosmos/src/CosmosClientOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public class CosmosClientOptions
/// </summary>
private int gatewayModeMaxConnectionLimit;
private CosmosSerializationOptions serializerOptions;
private CosmosSerializer serializer;
private CosmosSerializer serializerInternal;

private ConnectionMode connectionMode;
private Protocol connectionProtocol;
Expand Down Expand Up @@ -378,7 +378,7 @@ public CosmosSerializationOptions SerializerOptions
[JsonConverter(typeof(ClientOptionJsonConverter))]
public CosmosSerializer Serializer
{
get => this.serializer;
get => this.serializerInternal;
set
{
if (this.SerializerOptions != null)
Expand All @@ -387,7 +387,7 @@ public CosmosSerializer Serializer
$"{nameof(this.Serializer)} is not compatible with {nameof(this.SerializerOptions)}. Only one can be set. ");
}

this.serializer = value;
this.serializerInternal = value;
}
}

Expand Down Expand Up @@ -563,6 +563,14 @@ internal Protocol ConnectionProtocol
/// </summary>
internal bool? EnableCpuMonitor { get; set; }

internal void SetSerializerIfNotConfigured(CosmosSerializer serializer)
{
if (this.serializerInternal == null)
{
this.serializerInternal = serializer ?? throw new ArgumentNullException(nameof(serializer));
}
}

internal CosmosClientOptions Clone()
{
CosmosClientOptions cloneConfiguration = (CosmosClientOptions)this.MemberwiseClone();
Expand Down
3 changes: 3 additions & 0 deletions Microsoft.Azure.Cosmos/src/Resource/ClientContextCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ internal static CosmosClientContext Create(
clientOptions.Serializer,
clientOptions.SerializerOptions);

// This sets the serializer on client options which gives users access to it if a custom one is not configured.
clientOptions.SetSerializerIfNotConfigured(serializerCore.GetCustomOrDefaultSerializer());

CosmosResponseFactoryInternal responseFactory = new CosmosResponseFactoryCore(serializerCore);

return new ClientContextCore(
Expand Down
10 changes: 10 additions & 0 deletions Microsoft.Azure.Cosmos/src/Serializer/CosmosSerializerCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,16 @@ internal Stream ToStreamSqlQuerySpec(SqlQuerySpec input, ResourceType resourceTy
return serializer.ToStream<SqlQuerySpec>(input);
}

internal CosmosSerializer GetCustomOrDefaultSerializer()
{
if (this.customSerializer != null)
{
return this.customSerializer;
}

return CosmosSerializerCore.propertiesSerializer;
}

internal IEnumerable<T> FromFeedResponseStream<T>(
Stream stream,
ResourceType resourceType)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,9 @@ public async Task ItemLINQWithCamelCaseSerializerOptions(bool isGatewayMode)
}
};
CosmosClient camelCaseCosmosClient = TestCommon.CreateCosmosClient(builder, false);
Assert.IsNotNull(camelCaseCosmosClient.ClientOptions.Serializer);
Assert.IsTrue(camelCaseCosmosClient.ClientOptions.Serializer is CosmosJsonSerializerWrapper);

Cosmos.Database database = camelCaseCosmosClient.GetDatabase(this.database.Id);
Container containerFromCamelCaseClient = database.GetContainer(this.Container.Id);
IList<ToDoActivity> itemList = await ToDoActivity.CreateRandomItems(container: containerFromCamelCaseClient, pkCount: 2, perPKItemCount: 1, randomPartitionKey: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ public async Task TestCustomJsonSerializer()
CosmosClient mockClient = TestCommon.CreateCosmosClient(
(cosmosClientBuilder) => cosmosClientBuilder.WithCustomSerializer(mockJsonSerializer.Object));
Container mockContainer = mockClient.GetContainer(this.database.Id, this.container.Id);
Assert.AreEqual(mockJsonSerializer.Object, mockClient.ClientOptions.Serializer);

//Validate that the custom json serializer is used for creating the item
ItemResponse<ToDoActivity> response = await mockContainer.CreateItemAsync<ToDoActivity>(item: testItem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ internal static CosmosClient CreateCosmosClient(Action<CosmosClientBuilder> cust
customizeClientBuilder(cosmosClientBuilder);
}

return cosmosClientBuilder.Build();
CosmosClient client = cosmosClientBuilder.Build();
Assert.IsNotNull(client.ClientOptions.Serializer);
j82w marked this conversation as resolved.
Show resolved Hide resolved
return client;
}

internal static CosmosClient CreateCosmosClient(CosmosClientOptions clientOptions, string resourceToken = null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void VerifyCosmosConfigurationPropertiesGetUpdated()
Assert.IsFalse(clientOptions.AllowBulkExecution);
Assert.AreEqual(0, clientOptions.CustomHandlers.Count);
Assert.IsNull(clientOptions.SerializerOptions);
Assert.IsNull(clientOptions.Serializer);
Assert.IsNotNull(clientOptions.Serializer);
Assert.IsNull(clientOptions.WebProxy);
Assert.IsFalse(clientOptions.LimitToEndpoint);
Assert.IsFalse(clientOptions.EnableTcpConnectionEndpointRediscovery);
Expand Down