diff --git a/changelog/24224.txt b/changelog/24224.txt new file mode 100644 index 000000000000..040b42d94da8 --- /dev/null +++ b/changelog/24224.txt @@ -0,0 +1,3 @@ +```release-note:bug +ui: Fix JSON editor in KV V2 unable to handle pasted values +``` diff --git a/ui/lib/kv/addon/components/kv-data-fields.hbs b/ui/lib/kv/addon/components/kv-data-fields.hbs index 76c5c2dfd3a0..b384d3e4c2e3 100644 --- a/ui/lib/kv/addon/components/kv-data-fields.hbs +++ b/ui/lib/kv/addon/components/kv-data-fields.hbs @@ -15,7 +15,7 @@ {{#if @showJson}} {{#if (or @modelValidations.secretData.errors this.lintingErrors)}} diff --git a/ui/lib/kv/addon/components/kv-data-fields.js b/ui/lib/kv/addon/components/kv-data-fields.js index bb1f759810d1..663df063e4c9 100644 --- a/ui/lib/kv/addon/components/kv-data-fields.js +++ b/ui/lib/kv/addon/components/kv-data-fields.js @@ -6,7 +6,7 @@ import Component from '@glimmer/component'; import { action } from '@ember/object'; import { tracked } from '@glimmer/tracking'; -import KVObject from 'vault/lib/kv-object'; +import { stringify } from 'core/helpers/stringify'; /** * @module KvDataFields is used for rendering the fields associated with kv secret data, it hides/shows a json editor and renders validation errors for the json editor @@ -28,10 +28,13 @@ import KVObject from 'vault/lib/kv-object'; export default class KvDataFields extends Component { @tracked lintingErrors; + @tracked codeMirrorString; - get emptyJson() { - // if secretData is null, this specially formats a blank object and renders a nice initial state for the json editor - return KVObject.create({ content: [{ name: '', value: '' }] }).toJSONString(true); + constructor() { + super(...arguments); + this.codeMirrorString = this.args.secret?.secretData + ? stringify([this.args.secret.secretData], {}) + : '{ "": "" }'; } @action @@ -41,5 +44,6 @@ export default class KvDataFields extends Component { if (!this.lintingErrors) { this.args.secret.secretData = JSON.parse(value); } + this.codeMirrorString = value; } } diff --git a/ui/lib/kv/addon/components/page/secret/edit.hbs b/ui/lib/kv/addon/components/page/secret/edit.hbs index 34a0365d1608..8a5be5646996 100644 --- a/ui/lib/kv/addon/components/page/secret/edit.hbs +++ b/ui/lib/kv/addon/components/page/secret/edit.hbs @@ -5,7 +5,12 @@ <:toolbarFilters> - + JSON @@ -38,7 +43,7 @@ 0) { + // Dumb way to check if there's a nested object in the secret + this.secretDataIsAdvanced = true; + } } get showOldVersionAlert() { diff --git a/ui/tests/acceptance/secrets/backend/kv/kv-v2-workflow-edge-cases-test.js b/ui/tests/acceptance/secrets/backend/kv/kv-v2-workflow-edge-cases-test.js index c92914102ba9..87aad84cc2c0 100644 --- a/ui/tests/acceptance/secrets/backend/kv/kv-v2-workflow-edge-cases-test.js +++ b/ui/tests/acceptance/secrets/backend/kv/kv-v2-workflow-edge-cases-test.js @@ -24,6 +24,7 @@ import { } from 'vault/tests/helpers/policy-generator/kv'; import { clearRecords, writeSecret, writeVersionedSecret } from 'vault/tests/helpers/kv/kv-run-commands'; import { FORM, PAGE } from 'vault/tests/helpers/kv/kv-selectors'; +import codemirror from 'vault/tests/helpers/codemirror'; /** * This test set is for testing edge cases, such as specific bug fixes or reported user workflows @@ -269,6 +270,20 @@ module('Acceptance | kv-v2 workflow | edge cases', function (hooks) { await click(PAGE.breadcrumbAtIdx(2)); assert.dom(PAGE.list.item()).exists({ count: 2 }, 'two secrets are listed'); }); + + test('complex values default to JSON display', async function (assert) { + await visit(`/vault/secrets/${this.backend}/kv/create`); + await fillIn(FORM.inputByAttr('path'), 'complex'); + + await click(FORM.toggleJson); + assert.strictEqual(codemirror().getValue(), '{ "": "" }'); + codemirror().setValue('{ "foo3": { "name": "bar3" } }'); + await click(FORM.saveBtn); + // Future: test that json is automatic on details too + await click(PAGE.detail.createNewVersion); + assert.dom(FORM.toggleJson).isDisabled(); + assert.dom(FORM.toggleJson).isChecked(); + }); }); // NAMESPACE TESTS diff --git a/ui/tests/integration/components/kv/kv-data-fields-test.js b/ui/tests/integration/components/kv/kv-data-fields-test.js index 954e4d0e1fa7..44ed1c9a2b1c 100644 --- a/ui/tests/integration/components/kv/kv-data-fields-test.js +++ b/ui/tests/integration/components/kv/kv-data-fields-test.js @@ -43,7 +43,7 @@ module('Integration | Component | kv-v2 | KvDataFields', function (hooks) { assert.strictEqual( codemirror().getValue(' '), - `{ \"\": \"\" }`, // eslint-disable-line no-useless-escape + `{ \"\": \"\" }`, // eslint-disable-line no-useless-escape 'json editor initializes with empty object' ); await fillIn(`${FORM.jsonEditor} textarea`, 'blah');