Add configurable ObjectId-to-string conversion to function_app.py #4
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This pull request updates the
function_app.pyof the MongoDB_DataAPI_Azure project to introduce an optional, environment‑controlled flag that determines whether MongoDBObjectIdvalues are automatically converted to strings in API responses.Previously, all
ObjectIdvalues were converted to strings unconditionally to ensure the output was valid JSON for Azure Logic Apps and other REST clients. With this enhancement, that behavior becomes configurable - giving users flexibility depending on their downstream requirements.Details of Changes
Added a new boolean configuration flag:
true(default), allObjectIdvalues in responses are recursively converted to strings.false, raw BSONObjectIdobjects are returned (useful for systems that can handle them natively or apply$projecttransformations independently).Implemented a recursive helper function:
Updated the main response logic:
Added safe handling for nested documents and lists to ensure deep conversion.
Maintained existing functionality for datetime serialization via
DateTimeEncoder.Motivation
Azure Logic Apps and other HTTP clients cannot directly parse BSON
ObjectIdtypes, which causes serialization errors unless the values are first converted to strings.However, some more advanced consumers may prefer to handle
ObjectIds in Extended JSON form or internally manage conversion. This change introduces a runtime environment variable for that flexibility.Testing
ENABLE_OBJECTID_CONVERSION=true)_idfields and all nestedObjectIds are converted to strings.ENABLE_OBJECTID_CONVERSION=false)ObjectIdobjects as-is. Useful for internal testing and advanced consumers.Example
Set the environment variable in Azure Function configuration:
or in the Azure Portal → Configuration → Application Settings.
Impact