From 6b41ec0bc1be0f41a83b58c110b8a995e83bb888 Mon Sep 17 00:00:00 2001 From: Sergiu Lupaiescu Date: Mon, 19 Oct 2026 17:00:02 +0300 Subject: [PATCH] Use MultiJSON constant and generate/parse API to silence deprecation warnings --- elasticsearch-api/lib/elasticsearch/api.rb | 14 +++++++++++++- elasticsearch-api/spec/unit/api_spec.rb | 11 ++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/elasticsearch-api/lib/elasticsearch/api.rb b/elasticsearch-api/lib/elasticsearch/api.rb index 7504667e31..7d5913da7b 100644 --- a/elasticsearch-api/lib/elasticsearch/api.rb +++ b/elasticsearch-api/lib/elasticsearch/api.rb @@ -29,7 +29,19 @@ module Elasticsearch module API include Elasticsearch::API::Actions - DEFAULT_SERIALIZER = MultiJson + # Default JSON serializer. multi_json >= 1.21 deprecates the MultiJson constant and + # dump/load in favor of MultiJSON, generate/parse; use the new API when available. + module DefaultSerializer + def self.dump(object, options = {}) + defined?(::MultiJSON) ? ::MultiJSON.generate(object, options) : ::MultiJson.dump(object, options) + end + + def self.load(string, options = {}) + defined?(::MultiJSON) ? ::MultiJSON.parse(string, options) : ::MultiJson.load(string, options) + end + end + + DEFAULT_SERIALIZER = DefaultSerializer HTTP_GET = 'GET'.freeze HTTP_HEAD = 'HEAD'.freeze diff --git a/elasticsearch-api/spec/unit/api_spec.rb b/elasticsearch-api/spec/unit/api_spec.rb index 06aebc88c2..a61f62ac67 100644 --- a/elasticsearch-api/spec/unit/api_spec.rb +++ b/elasticsearch-api/spec/unit/api_spec.rb @@ -24,7 +24,16 @@ end it 'has a default serializer' do - expect(Elasticsearch::API.serializer).to eq(MultiJson) + expect(Elasticsearch::API.serializer).to respond_to(:dump, :load) + end + + it 'serializes and deserializes with the default serializer' do + serializer = Elasticsearch::API::DEFAULT_SERIALIZER + expect(serializer.load(serializer.dump({ 'a' => 1 }))).to eq({ 'a' => 1 }) + end + + it 'does not emit deprecation warnings' do + expect { Elasticsearch::API::DEFAULT_SERIALIZER.dump({ a: 1 }) }.not_to output.to_stderr end context 'when settings are changed' do