-
Notifications
You must be signed in to change notification settings - Fork 201
Allow usage of ObjectSerializer for Cosmos bindings #3163
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
base: main
Are you sure you want to change the base?
Changes from all commits
52d0891
1132b07
81b678b
cc17626
1708190
d4ba483
3afc2fd
29f7220
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,6 +1,7 @@ | ||||||||||||||||
| // Copyright (c) .NET Foundation. All rights reserved. | ||||||||||||||||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||||||||||||||||
|
|
||||||||||||||||
| using Azure.Core.Serialization; | ||||||||||||||||
| using Microsoft.Azure.Cosmos; | ||||||||||||||||
|
|
||||||||||||||||
| namespace Microsoft.Azure.Functions.Worker | ||||||||||||||||
|
|
@@ -11,5 +12,8 @@ public class CosmosDBExtensionOptions | |||||||||||||||
| /// Gets or sets the CosmosClientOptions. | ||||||||||||||||
| /// </summary> | ||||||||||||||||
| public CosmosClientOptions ClientOptions { get; set; } = new() { ConnectionMode = ConnectionMode.Gateway }; | ||||||||||||||||
|
|
||||||||||||||||
| // TODO: in next major version, ensure this is WorkerOptions.Serializer by default. | ||||||||||||||||
|
||||||||||||||||
| // TODO: in next major version, ensure this is WorkerOptions.Serializer by default. | |
| // TODO: In the next major version, ensure this defaults to WorkerOptions.Serializer. | |
| // This cannot be changed now to avoid breaking existing users who rely on the current default. | |
| // Currently, this defaults to DefaultSerializer via the CosmosDBBindingOptions.Serializer property, | |
| // unless UseCosmosDBWorkerSerializer is called. |
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Serializer property lacks XML documentation. Consider adding a summary comment to document its purpose, especially since this is a public API. For example:
/// <summary>
/// Gets or sets the ObjectSerializer used for deserializing CosmosDB POCOs.
/// If not set, defaults to WorkerOptions.Serializer when UseCosmosDBWorkerSerializer is called.
/// </summary>| public ObjectSerializer? Serializer { get; set; } | |
| } | |
| /// <summary> | |
| /// Gets or sets the ObjectSerializer used for deserializing CosmosDB POCOs. | |
| /// If not set, defaults to WorkerOptions.Serializer when UseCosmosDBWorkerSerializer is called. | |
| /// </summary> | |
| public ObjectSerializer? Serializer { get; set; } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |||||||||||||||
| // Licensed under the MIT License. See License.txt in the project root for license information. | ||||||||||||||||
|
|
||||||||||||||||
| using System; | ||||||||||||||||
| using Microsoft.Azure.Cosmos; | ||||||||||||||||
| using Azure.Core.Serialization; | ||||||||||||||||
| using Microsoft.Extensions.Azure; | ||||||||||||||||
| using Microsoft.Extensions.DependencyInjection; | ||||||||||||||||
| using Microsoft.Extensions.DependencyInjection.Extensions; | ||||||||||||||||
|
|
@@ -20,25 +20,28 @@ public static class FunctionsWorkerApplicationBuilderExtensions | |||||||||||||||
| /// </summary> | ||||||||||||||||
| /// <param name="builder">The <see cref="IFunctionsWorkerApplicationBuilder"/> to configure.</param> | ||||||||||||||||
| /// <returns>The same instance of the <see cref="IFunctionsWorkerApplicationBuilder"/> for chaining.</returns> | ||||||||||||||||
| public static IFunctionsWorkerApplicationBuilder ConfigureCosmosDBExtension(this IFunctionsWorkerApplicationBuilder builder) | ||||||||||||||||
| public static IFunctionsWorkerApplicationBuilder ConfigureCosmosDBExtension( | ||||||||||||||||
| this IFunctionsWorkerApplicationBuilder builder) | ||||||||||||||||
| { | ||||||||||||||||
| if (builder is null) | ||||||||||||||||
| { | ||||||||||||||||
| throw new System.ArgumentNullException(nameof(builder)); | ||||||||||||||||
| throw new ArgumentNullException(nameof(builder)); | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| builder.Services.AddAzureClientsCore(); // Adds AzureComponentFactory | ||||||||||||||||
| builder.Services.AddOptions<CosmosDBBindingOptions>(); | ||||||||||||||||
| builder.Services.AddOptions<CosmosDBExtensionOptions>() | ||||||||||||||||
| .Configure<IOptions<WorkerOptions>>((cosmos, worker) => | ||||||||||||||||
| { | ||||||||||||||||
| if (cosmos.ClientOptions.Serializer is null) | ||||||||||||||||
| { | ||||||||||||||||
| cosmos.ClientOptions.Serializer = new WorkerCosmosSerializer(worker.Value.Serializer); | ||||||||||||||||
| } | ||||||||||||||||
| }); | ||||||||||||||||
| .PostConfigure<IOptions<WorkerOptions>>((cosmos, worker) => | ||||||||||||||||
| { | ||||||||||||||||
| ObjectSerializer? serializer = cosmos.Serializer ?? worker.Value.Serializer; | ||||||||||||||||
| if (serializer is not null && cosmos.ClientOptions.Serializer is null) | ||||||||||||||||
| { | ||||||||||||||||
| cosmos.ClientOptions.Serializer = new WorkerCosmosSerializer(serializer); | ||||||||||||||||
| } | ||||||||||||||||
| }); | ||||||||||||||||
|
|
||||||||||||||||
| builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton<IConfigureOptions<CosmosDBBindingOptions>, CosmosDBBindingOptionsSetup>()); | ||||||||||||||||
| builder.Services.TryAddEnumerable(ServiceDescriptor.Singleton< | ||||||||||||||||
| IConfigureOptions<CosmosDBBindingOptions>, CosmosDBBindingOptionsSetup>()); | ||||||||||||||||
|
|
||||||||||||||||
| return builder; | ||||||||||||||||
| } | ||||||||||||||||
|
|
@@ -49,10 +52,27 @@ public static IFunctionsWorkerApplicationBuilder ConfigureCosmosDBExtension(this | |||||||||||||||
| /// <param name="builder">The IFunctionsWorkerApplicationBuilder to add the configuration to.</param> | ||||||||||||||||
| /// <param name="options">An Action to configure the CosmosDBExtensionOptions.</param> | ||||||||||||||||
| /// <returns>The same instance of the <see cref="IFunctionsWorkerApplicationBuilder"/> for chaining.</returns> | ||||||||||||||||
| public static IFunctionsWorkerApplicationBuilder ConfigureCosmosDBExtensionOptions(this IFunctionsWorkerApplicationBuilder builder, Action<CosmosDBExtensionOptions> options) | ||||||||||||||||
| public static IFunctionsWorkerApplicationBuilder ConfigureCosmosDBExtensionOptions( | ||||||||||||||||
| this IFunctionsWorkerApplicationBuilder builder, Action<CosmosDBExtensionOptions> options) | ||||||||||||||||
| { | ||||||||||||||||
| builder.Services.Configure(options); | ||||||||||||||||
| return builder; | ||||||||||||||||
| } | ||||||||||||||||
|
|
||||||||||||||||
| /// <summary> | ||||||||||||||||
| /// Configures the CosmosDBExtensionOptions for the Functions Worker Cosmos extension. | ||||||||||||||||
| /// </summary> | ||||||||||||||||
| /// <param name="builder">The IFunctionsWorkerApplicationBuilder to add the configuration to.</param> | ||||||||||||||||
|
Comment on lines
+63
to
+65
|
||||||||||||||||
| /// Configures the CosmosDBExtensionOptions for the Functions Worker Cosmos extension. | |
| /// </summary> | |
| /// <param name="builder">The IFunctionsWorkerApplicationBuilder to add the configuration to.</param> | |
| /// Configures the CosmosDB extension to use the WorkerOptions.Serializer for deserializing POCOs. | |
| /// Call this method to ensure custom serialization settings from WorkerOptions are used for CosmosDB bindings. | |
| /// </summary> | |
| /// <param name="builder">The <see cref="IFunctionsWorkerApplicationBuilder"/> to configure.</param> |
Copilot
AI
Dec 3, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The relationship and execution order between ConfigureCosmosDBExtension (which uses PostConfigure) and UseCosmosDBWorkerSerializer (which uses Configure) may be confusing to users. Consider adding documentation or examples showing:
- When to use
UseCosmosDBWorkerSerializervs setting the serializer viaConfigureCosmosDBExtensionOptions - That
UseCosmosDBWorkerSerializershould typically be called before any manual configuration of the serializer - How the serializer resolution works:
CosmosExtensionOptions.Serializer(if set) →WorkerOptions.Serializer(if UseCosmosDBWorkerSerializer is called) →DefaultSerializer(fallback)
Uh oh!
There was an error while loading. Please reload this page.