Skip to content

Allow usage of ObjectSerializer for Cosmos bindings#3163

Merged
jviau merged 5 commits intomainfrom
jviau/cosmos-serializer
Mar 12, 2026
Merged

Allow usage of ObjectSerializer for Cosmos bindings#3163
jviau merged 5 commits intomainfrom
jviau/cosmos-serializer

Conversation

@jviau
Copy link
Copy Markdown
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 2 times, most recently 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
Copy Markdown
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
Copy Markdown
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 thread extensions/Worker.Extensions.CosmosDB/src/Config/CosmosDBExtensionOptions.cs Outdated
Copy link
Copy Markdown
Member

@brettsam brettsam left a comment

Choose a reason for hiding this comment

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

No objections to the changes, but would love to see tests added around this, especially since it looks like we broke behavior at one point.

@jviau jviau force-pushed the jviau/cosmos-serializer branch from 29f7220 to 45f41f3 Compare March 4, 2026 21:25
@jviau
Copy link
Copy Markdown
Contributor Author

jviau commented Mar 4, 2026

@brettsam unit tests have been added. I verify the new options setup in one test class, and then added converter tests to verify the resolved serializer is used for the POCO case.

@jviau jviau merged commit f24c10e into main Mar 12, 2026
74 checks passed
@jviau jviau deleted the jviau/cosmos-serializer branch March 12, 2026 15:57
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

6 participants