elastic / elastic/elasticsearch-ruby
MultiJson deprecation warnings emitted from Elasticsearch::API.serializer with multi_json >= 1.21
- Dominant language
- Ruby
- Stars
- 2k
- Forks
- 615
- PR merge metrics
- No merged PRs in 30d
Description
**Describe the bug/error/problem**
`multi_json` 1.21.0 deprecated the legacy `MultiJson` constant in favor of `MultiJSON`. `elasticsearch-api` still references the legacy constant as its default serializer, in `lib/elasticsearch/api.rb` (still present on `main`, commit `73acacb`):
```ruby
DEFAULT_SERIALIZER = MultiJson
```
Every call that goes through `Elasticsearch::API.serializer` (e.g. `Utils.__bulkify` for bulk requests, `msearch`, `msearch_template`, `fleet.msearch`) routes through `multi_json`'s deprecation shim and emits, once per process:
```
The MultiJson constant is deprecated and will be removed in v2.0. Use MultiJSON instead.
MultiJSON.dump is deprecated and will be removed in v2.0. Use MultiJSON.generate instead.
```
The second warning appears because `dump`/`load` are themselves deprecated aliases on the new `MultiJSON` module (the current API is `generate`/`parse`).
This is the same problem that was reported for the transport layer in elastic/elastic-transport-ruby#125 and fixed in `elastic-transport` 8.5.3: with an up-to-date transport, `elasticsearch-api` is now the remaining source of these warnings.
**To Reproduce**
```ruby
require 'elasticsearch' # with multi_json >= 1.21 installed
Warning[:deprecated] = true
Elasticsearch::API.serializer.dump({ a: 1 })
# => The MultiJson constant is deprecated and will be removed in v2.0. Use MultiJSON instead.
# => MultiJSON.dump is deprecated and will be removed in v2.0. Use MultiJSON.generate instead.
```
In a real application the warnings appear at runtime on the first bulk indexing request (or any other API that serializes a payload through `Elasticsearch::API.serializer`).
**Expected behavior**
No deprecation warnings: the serializer should use the `MultiJSON` constant and its current API (`generate`/`parse`) when `multi_json >= 1.21` is loaded, falling back to `MultiJson.dump`/`MultiJson.load` on older versions. Checking `defined?(::MultiJSON)` handles both cases without version sniffing.
**Your Environment (please complete the following information):**
- Operating System: macOS 15
- Ruby Version: Ruby MRI 4.0.1
- Elasticsearch client version: `elasticsearch` / `elasticsearch-api` 8.19.3 (the legacy constant is also still referenced on `main`)
- `multi_json` version: 1.21.1
- Elasticsearch version: 8.19
**Additional context**
A workaround for applications is to inject a custom serializer:
```ruby
module MultiJsonSerializerShim
def self.dump(object, options = {})
MultiJSON.generate(object, options)
end
def self.load(string, options = {})
MultiJSON.parse(string, options)
end
end
Elasticsearch::API.settings[:serializer] = MultiJsonSerializerShim
```
---
Issue opened with the help of [Claude Code](https://claude.com/claude-code).
Contributor guide
Research direction
Start in lib/elasticsearch/api.rb at DEFAULT_SERIALIZER, then reproduce the Elasticsearch::API.serializer.dump example with multi_json >= 1.21 and deprecation warnings enabled. Verify that serialization uses MultiJSON.generate/parse when available, preserves the older fallback, and emits no deprecation warnings.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- ruby
- Domain
- api, backend-api-design
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Quiet
- Clarity
- Clearly specified
- Newbie friendliness
- 76/100