WARNING: THIS SITE IS A MIRROR OF GITHUB.COM / IT CANNOT LOGIN OR REGISTER ACCOUNTS / THE CONTENTS ARE PROVIDED AS-IS / THIS SITE ASSUMES NO RESPONSIBILITY FOR ANY DISPLAYED CONTENT OR LINKS / IF YOU FOUND SOMETHING MAY NOT GOOD FOR EVERYONE, CONTACT ADMIN AT ilovescratch@foxmail.com
Skip to content

Conversation

@jviau
Copy link
Contributor

@jviau jviau commented Sep 8, 2025

Issue describing the changes in this PR

resolves #2911

Pull request checklist

  • My changes do not require documentation changes
    • Otherwise: Documentation issue linked to PR
  • My changes should not be added to the release notes for the next release
    • Otherwise: I've added my notes to release_notes.md
  • My changes do not need to be backported to a previous version
    • Otherwise: Backport tracked by issue/PR #issue_or_pr
  • I have added all required tests (Unit tests, E2E tests)

Additional information

Addresses an issue with CosmosDB extension where users could not customize JSON serialization when binding directly to a POCO.

Issue:

CosmosDBConverter was not consuming any user-provided JSON serialization customization. This was a regression from #1924.

Fix:

  1. Add a new option to CosmosDBExtensionOptions to provide a ObjectSerializer, defaulting to WorkerOptions.Serializer if no serializer is explicitly set.
  2. Update CosmosDBConverter to use this new serializer when deserializing POCOs

Concerns / Discussion:

This is a fix for a regression introduced back in December 2023. But it COULD be seen as a new breaking behavior change for some customers. This poses some risk for this change.

@jviau jviau force-pushed the jviau/cosmos-serializer branch from dd50e62 to 0aaa0fd Compare September 29, 2025 22:08
@jviau jviau force-pushed the jviau/cosmos-serializer branch from 0aaa0fd to 1708190 Compare September 29, 2025 22:08
@fabiocav fabiocav closed this Nov 12, 2025
@jviau jviau reopened this Dec 3, 2025
@jviau
Copy link
Contributor Author

jviau commented Dec 3, 2025

Re-opening. I have reverted back to opt-in for this change.

Copilot AI review requested due to automatic review settings December 3, 2025 21:21
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR addresses a regression in the CosmosDB extension where custom JSON serialization settings from WorkerOptions.Serializer were not being used when deserializing POCOs in CosmosDB bindings. The fix introduces a new Serializer property to CosmosDBExtensionOptions and a helper method UseCosmosDBWorkerSerializer() to explicitly opt-in to using the worker's serializer for CosmosDB POCO deserialization.

Key changes:

  • Added ObjectSerializer? Serializer property to CosmosDBExtensionOptions for custom serialization
  • Introduced UseCosmosDBWorkerSerializer() extension method to configure the serializer
  • Refactored CosmosDBConverter to use the configured serializer instead of hardcoded System.Text.Json
  • Fixed a bug in WorkerCosmosSerializer.FromStream() where streams were being disposed prematurely
  • Maintained backward compatibility via a DefaultSerializer with PropertyNameCaseInsensitive = true

Reviewed changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
WorkerCosmosSerializer.cs Fixed stream disposal bug for Stream-type bindings and minor style improvements
FunctionsWorkerApplicationBuilderExtensions.cs Added UseCosmosDBWorkerSerializer() method and updated serializer configuration logic
CosmosDBConverter.cs Replaced hardcoded System.Text.Json with configurable ObjectSerializer throughout
CosmosDBExtensionOptions.cs Added public Serializer property with TODO for future default behavior
CosmosDBBindingOptions.cs Added DefaultSerializer and Serializer property getter for fallback behavior
CosmosDBBindingOptionsSetup.cs Removed unused import and formatting cleanup
release_notes.md Updated version placeholder and release note description

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +17 to 18
public ObjectSerializer? Serializer { get; set; }
}
Copy link

Copilot AI Dec 3, 2025

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>
Suggested change
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; }

Copilot uses AI. Check for mistakes.
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>
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The XML documentation for UseCosmosDBWorkerSerializer is incomplete and potentially misleading. It currently says "Configures the CosmosDBExtensionOptions for the Functions Worker Cosmos extension" which is too generic and identical to ConfigureCosmosDBExtensionOptions. Consider updating it to clarify its specific purpose:

/// <summary>
/// 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>
Suggested change
/// 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 uses AI. Check for mistakes.
Comment on lines +67 to +76
public static IFunctionsWorkerApplicationBuilder UseCosmosDBWorkerSerializer(this IFunctionsWorkerApplicationBuilder builder)
{
builder.Services.AddOptions<CosmosDBExtensionOptions>()
.Configure<IOptions<WorkerOptions>>((cosmos, worker) =>
{
cosmos.Serializer ??= worker.Value.Serializer;
});

return builder;
}
Copy link

Copilot AI Dec 3, 2025

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:

  1. When to use UseCosmosDBWorkerSerializer vs setting the serializer via ConfigureCosmosDBExtensionOptions
  2. That UseCosmosDBWorkerSerializer should typically be called before any manual configuration of the serializer
  3. How the serializer resolution works: CosmosExtensionOptions.Serializer (if set) → WorkerOptions.Serializer (if UseCosmosDBWorkerSerializer is called) → DefaultSerializer (fallback)

Copilot uses AI. Check for mistakes.
/// </summary>
public CosmosClientOptions ClientOptions { get; set; } = new() { ConnectionMode = ConnectionMode.Gateway };

// TODO: in next major version, ensure this is WorkerOptions.Serializer by default.
Copy link

Copilot AI Dec 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The TODO comment mentions ensuring this defaults to WorkerOptions.Serializer in the next major version. Consider adding more context about why this can't be done now (likely to avoid breaking changes) and what the current default behavior is (defaults to DefaultSerializer via CosmosDBBindingOptions.Serializer property unless UseCosmosDBWorkerSerializer is called).

Suggested change
// 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 uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Unable to override the JsonSerializer when using ConfigureFunctionsWebApplication for CosmoDb Input bindings

5 participants