From 30b616cbf7439b314dc624e8bc309a29c4437b3a Mon Sep 17 00:00:00 2001 From: Patrick Oscity Date: Wed, 19 Aug 2020 22:24:40 +0200 Subject: [PATCH] Flatten config keys when using create_or_update/save with plugins --- lib/kong/plugin.rb | 8 +++++++- spec/kong/plugin_spec.rb | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/lib/kong/plugin.rb b/lib/kong/plugin.rb index 8d26f97..9f50baf 100644 --- a/lib/kong/plugin.rb +++ b/lib/kong/plugin.rb @@ -14,7 +14,13 @@ def create super end - # update resource + # Create or update resource + def create_or_update + flatten_config + super + end + + # Update resource def update flatten_config super diff --git a/spec/kong/plugin_spec.rb b/spec/kong/plugin_spec.rb index 3119773..9a55910 100644 --- a/spec/kong/plugin_spec.rb +++ b/spec/kong/plugin_spec.rb @@ -42,6 +42,24 @@ end end + describe '#create_or_update' do + it 'transforms config keys to config.key format' do + headers = { 'Content-Type' => 'application/json' } + attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345' } + expect(Kong::Client.instance).to receive(:put).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes) + subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345' } }) + subject.create_or_update + end + + it 'transforms nested config keys to config.key format' do + headers = { 'Content-Type' => 'application/json' } + attributes = { 'api_id' => ':api_id', 'config.anonymous' => '12345', 'config.first.second' => '1' } + expect(Kong::Client.instance).to receive(:put).with('/apis/:api_id/plugins/', attributes, nil, headers).and_return(attributes) + subject = described_class.new({ api_id: ':api_id', config: { 'anonymous' => '12345', 'first' => { 'second' => '1' } } }) + subject.create_or_update + end + end + describe '#update' do it 'transforms config keys to config.key format' do headers = { 'Content-Type' => 'application/json' }