Skip to content

Conversation

@lemenkov
Copy link
Contributor

Summary
Fix libbson deprecated API warning with version compatibility

Details
This PR fixes deprecation warnings that appear when compiling the cachedb_mongodb module with mongo-c-driver >= 1.29.0. The bson_as_json() function was deprecated in favor of bson_as_legacy_extended_json() in mongo-c-driver version 1.29.0 (released October 2024).

The warning appears during compilation:

warning: 'bson_as_json' is deprecated: Use bson_as_legacy_extended_json instead [-Wdeprecated-declarations]

The deprecation was introduced to clarify which JSON format is being produced - legacy extended JSON vs. canonical extended JSON. The function signatures are identical, making this a straightforward API update.

However, many LTS distributions still ship older versions of mongo-c-driver (< 1.29.0), so the code needs to maintain backward compatibility with both the old and new API.

Solution
Add a compatibility macro at the top of modules/cachedb_mongodb/cachedb_mongodb_dbase.c that maps the new function name to the old one on older versions:

/* Compatibility wrapper for libbson < 1.29.0 */
#if !MONGOC_CHECK_VERSION(1, 29, 0)
#define bson_as_legacy_extended_json bson_as_json
#endif

Then update all bson_as_json() calls to bson_as_legacy_extended_json() throughout the file.

This approach:

  • Uses the new API name everywhere in the code (clean, future-proof)
  • Automatically works with older versions via the macro alias
  • No behavioral changes - both functions produce identical output
  • Maintains compatibility with all mongo-c-driver versions

Compatibility
Fully backward compatible. The macro ensures the code works with:

  • mongo-c-driver >= 1.29.0: Uses new bson_as_legacy_extended_json() API
  • mongo-c-driver < 1.29.0: Transparently uses old bson_as_json() via macro

No runtime behavior changes - the same JSON is produced on all versions.

Closing issues
N/A

During compilation of cachedb_mongodb module, numerous deprecation
warnings appear on systems with mongo-c-driver >= 1.29.0:

```
Compiling cachedb_mongodb_dbase.c
gcc -fPIC -DPIC -O2 -flto=auto -ffat-lto-objects -fexceptions -g -grecord-gcc-switches -pipe -Wall -Wno-complain-wrong-lang -Werror=format-security -Wp,-U_FORTIFY_SOURCE,-D_FORTIFY_SOURCE=3 -Wp,-D_GLIBCXX_ASSERTIONS -specs=/usr/lib/rpm/redhat/redhat-hardened-cc1 -fstack-protector-strong -specs=/usr/lib/rpm/redhat/redhat-annobin-cc1 -m64 -march=x86-64 -mtune=generic -fasynchronous-unwind-tables -fstack-clash-protection -fcf-protection -mtls-dialect=gnu2 -fno-omit-frame-pointer -mno-omit-leaf-frame-pointer -DMOD_NAME='cachedb_mongodb' -DPKG_MALLOC  -DSHM_MMAP  -DUSE_MCAST  -DDISABLE_NAGLE  -DSTATISTICS  -DHAVE_RESOLV_RES  -DF_MALLOC  -DQ_MALLOC  -DHP_MALLOC  -DDBG_MALLOC  -DF_PARALLEL_MALLOC  -DHAVE_STDATOMIC -DHAVE_GENERICS  -DNAME='"opensips"' -DVERSION='"3.6.2"' -DARCH='"x86_64"' -DOS='"linux"' -DCOMPILER='"gcc 15"' -D__CPU_x86_64 -D__OS_linux -D__SMP_yes -DCFG_DIR='"/etc/opensips/"'  -DVERSIONTYPE='"git"' -DTHISREVISION='"994bcd690"' -DFAST_LOCK -DADAPTIVE_WAIT -DADAPTIVE_WAIT_LOOPS=1024 -DHAVE_GETHOSTBYNAME2 -DHAVE_UNION_SEMUN -DHAVE_MSG_NOSIGNAL -DHAVE_MSGHDR_MSG_CONTROL -DHAVE_ALLOCA_H -DHAVE_TIMEGM -DHAVE_EPOLL -DHAVE_SIGIO_RT -DHAVE_SELECT -I/usr/include/json-c -I/usr/include/json-c -DJSON_PKG_MAJOR=0 -DJSON_PKG_MINOR=18 -DJSON_PKG_MICRO=0 -DUTF8PROC_EXPORTS -I/usr/include/libmongoc-1.0 -I/usr/include/libbson-1.0 -c cachedb_mongodb_dbase.c -o cachedb_mongodb_dbase.o
cachedb_mongodb_dbase.c: In function ‘mongo_con_set’:
cachedb_mongodb_dbase.c:315:9: warning: ‘bson_as_json’ is deprecated: Use bson_as_legacy_extended_json instead [-Wdeprecated-declarations]
  315 |         dbg_bson("query: ", query);
      |         ^~~~~~~~
In file included from /usr/include/libmongoc-1.0/mongoc/mongoc.h:22,
                 from /usr/include/libmongoc-1.0/mongoc.h:18,
                 from cachedb_mongodb_dbase.h:30,
                 from cachedb_mongodb_dbase.c:22:
/usr/include/libbson-1.0/bson/bson.h:535:1: note: declared here
  535 | bson_as_json (const bson_t *bson, size_t *length) BSON_GNUC_DEPRECATED_FOR (bson_as_legacy_extended_json);
      | ^~~~~~~~~~~~
```

The MongoDB C driver (libbson) deprecated bson_as_json() in version
1.29.0 (October 2024) in favor of bson_as_legacy_extended_json() to
clarify which JSON format is being produced (legacy vs. canonical
extended JSON).

We added compatibility macro at the top of cachedb_mongodb_dbase.c - for
mongo-c-driver < 1.29.0, define bson_as_legacy_extended_json as an alias
to bson_as_json, allowing the code to use the new API name while
maintaining backward compatibility.

This change maintains compatibility with all mongo-c-driver versions.
The new function name is used on >= 1.29.0, while older versions
transparently use the original bson_as_json() through the macro alias.

No behavioral changes - the replacement is functionally identical and
produces the same JSON output format. The new name simply makes it
explicit that the legacy extended JSON format is being used.

Note: mongo-c-driver 1.29.0 was released in October 2024. Many LTS
distributions still ship earlier versions (e.g., RHEL 8/9, Ubuntu
20.04/22.04, Debian 11/12), making the compatibility macro necessary.

Signed-off-by: Peter Lemenkov <lemenkov@gmail.com>
Assisted-by: Claude (Anthropic) <https://claude.ai>
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.

1 participant