From e9634327fbba0f0a12c86e47f78993db1242d131 Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Mon, 4 Sep 2023 02:02:14 -0700 Subject: [PATCH 1/2] fix createRecord error when no adapter is present --- packages/store/src/-private/store-service.ts | 9 ++++++--- .../tests/integration/generate-id-test.js | 18 ++++++++++++++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/packages/store/src/-private/store-service.ts b/packages/store/src/-private/store-service.ts index cbfd4f2062d..320f32acec4 100644 --- a/packages/store/src/-private/store-service.ts +++ b/packages/store/src/-private/store-service.ts @@ -675,7 +675,7 @@ class Store extends EmberObject { // to avoid conflicts. if (properties.id === null || properties.id === undefined) { - let adapter = this.adapterFor(modelName); + let adapter = this.adapterFor(modelName, true); if (adapter && adapter.generateIdForRecord) { properties.id = adapter.generateIdForRecord(this, modelName, properties); @@ -2256,7 +2256,7 @@ class Store extends EmberObject { @param {String} modelName @return Adapter */ - adapterFor(modelName: string) { + adapterFor(modelName: string, _allowMissing?: boolean): MinimumAdapterInterface | undefined { if (DEBUG) { assertDestroyingStore(this, 'adapterFor'); } @@ -2292,7 +2292,10 @@ class Store extends EmberObject { return adapter; } - assert(`No adapter was found for '${modelName}' and no 'application' adapter was found as a fallback.`); + assert( + `No adapter was found for '${modelName}' and no 'application' adapter was found as a fallback.`, + _allowMissing + ); } /** diff --git a/tests/adapter-encapsulation/tests/integration/generate-id-test.js b/tests/adapter-encapsulation/tests/integration/generate-id-test.js index 396cccb5e9d..11df8118627 100644 --- a/tests/adapter-encapsulation/tests/integration/generate-id-test.js +++ b/tests/adapter-encapsulation/tests/integration/generate-id-test.js @@ -98,4 +98,22 @@ module('integration/generate-id - GenerateIdForRecord Tests', function (hooks) { assert.deepEqual(record.serialize().data.attributes, props, 'record created without error'); }); + + test('store.createRecord does not error if adapter is undefined.', async function (assert) { + let store = this.owner.lookup('service:store'); + let expectedData = { + data: { + type: 'person', + attributes: { + firstName: 'Gaurav', + lastName: 'Munjal', + }, + }, + }; + + let props = expectedData.data.attributes; + let record = store.createRecord('person', props); + + assert.deepEqual(record.serialize().data.attributes, props, 'record created without error'); + }); }); From 91168033f1b4c4cca9b5eed89d13bc542154a38d Mon Sep 17 00:00:00 2001 From: Chris Thoburn Date: Mon, 4 Sep 2023 02:08:46 -0700 Subject: [PATCH 2/2] make types nice --- packages/store/src/-private/store-service.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/store/src/-private/store-service.ts b/packages/store/src/-private/store-service.ts index 320f32acec4..ee181e4d0ec 100644 --- a/packages/store/src/-private/store-service.ts +++ b/packages/store/src/-private/store-service.ts @@ -2256,7 +2256,9 @@ class Store extends EmberObject { @param {String} modelName @return Adapter */ - adapterFor(modelName: string, _allowMissing?: boolean): MinimumAdapterInterface | undefined { + adapterFor(modelName: string): MinimumAdapterInterface; + adapterFor(modelName: string, _allowMissing: true): MinimumAdapterInterface | undefined; + adapterFor(modelName: string, _allowMissing?: true): MinimumAdapterInterface | undefined { if (DEBUG) { assertDestroyingStore(this, 'adapterFor'); }