diff --git a/addons/api/addon/adapters/application.js b/addons/api/addon/adapters/application.js index 93f0c3925..f0feb12d7 100644 --- a/addons/api/addon/adapters/application.js +++ b/addons/api/addon/adapters/application.js @@ -41,7 +41,7 @@ function prenormalizeArrayResponse(response) { export default class ApplicationAdapter extends RESTAdapter.extend( AdapterBuildURLMixin ) { - + // =attributes /** @@ -84,7 +84,6 @@ export default class ApplicationAdapter extends RESTAdapter.extend( */ urlPrefix(path, parentURL, modelName, id, snapshot) { const prefix = super.urlPrefix(...arguments); - const isScope = modelName === 'scope'; let scopePath = ''; let scopeID = ''; if (snapshot && this.hasScopePrefix) { @@ -101,7 +100,7 @@ export default class ApplicationAdapter extends RESTAdapter.extend( // Only non-scope resources need a scope path, since scope resources // aren't technically "scoped" the same way. // Ensure a slash is added between prefix + scope path if needed. - if (!isScope && scopeID && prefix.charAt(prefix.length - 1) !== '/') { + if (scopeID && prefix.charAt(prefix.length - 1) !== '/') { scopePath = `/scopes/${scopeID}`; } return `${prefix}${scopePath}`; diff --git a/addons/api/addon/adapters/scope.js b/addons/api/addon/adapters/scope.js index 6d4549844..f078e89df 100644 --- a/addons/api/addon/adapters/scope.js +++ b/addons/api/addon/adapters/scope.js @@ -3,6 +3,16 @@ import { serializeIntoHash } from '@ember-data/adapter/-private'; export default class ScopeAdapter extends ApplicationAdapter { + // =attributes + + /** + * Scopes do not include a scope prefix. + * @override + */ + hasScopePrefix = false; + + // =methods + /** * Identical to default method except that we pass a query parameter * indicating the parent scope. diff --git a/addons/api/tests/unit/adapters/application-test.js b/addons/api/tests/unit/adapters/application-test.js index dc984bba2..64ae9fd27 100644 --- a/addons/api/tests/unit/adapters/application-test.js +++ b/addons/api/tests/unit/adapters/application-test.js @@ -35,14 +35,16 @@ module('Unit | Adapter | application', function (hooks) { assert.equal(prefix, `v1/scopes/${scopeID}`); }); - test('it does not prefixes URLs with `scopeID` for scope resources', function (assert) { - assert.expect(1); + test('it does not prefix URLs with `scopeID` when hasScopePrefix is `false`', function (assert) { + assert.expect(2); const scopeID = 'global'; const adapter = this.owner.lookup('adapter:application'); - const prefix = adapter.urlPrefix(null, null, 'scope', null, { + adapter.hasScopePrefix = false; + const prefix = adapter.urlPrefix(null, null, null, null, { adapterOptions: { scopeID } }); - assert.equal(prefix, `v1`); + assert.ok(config.api.namespace); + assert.equal(prefix, 'v1'); }); test('it generates correct default URL suffixes', function (assert) { diff --git a/addons/api/tests/unit/adapters/scope-test.js b/addons/api/tests/unit/adapters/scope-test.js index 7dcd482c7..b38f491ce 100644 --- a/addons/api/tests/unit/adapters/scope-test.js +++ b/addons/api/tests/unit/adapters/scope-test.js @@ -6,6 +6,16 @@ module('Unit | Adapter | scope', function(hooks) { setupTest(hooks); setupMirage(hooks); + test('it does not prefix URLs with `scopeID` for scope resources', function (assert) { + assert.expect(1); + const scopeID = 'global'; + const adapter = this.owner.lookup('adapter:scope'); + const prefix = adapter.urlPrefix(null, null, 'scope', null, { + adapterOptions: { scopeID } + }); + assert.equal(prefix, `v1`); + }); + test('it generates correct createRecord URLs with a scope_id query parameter', function (assert) { assert.expect(1); this.server.post('/v1/scopes', (schema, { queryParams }) =>{ diff --git a/addons/api/tests/unit/serializers/application-test.js b/addons/api/tests/unit/serializers/application-test.js index c067939b9..e838a8da0 100644 --- a/addons/api/tests/unit/serializers/application-test.js +++ b/addons/api/tests/unit/serializers/application-test.js @@ -29,6 +29,48 @@ module('Unit | Serializer | application', function (hooks) { }); }); + test('it does not serialize scope_id when serializeScopeID is false', function (assert) { + assert.expect(2); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('user'); + const record = store.createRecord('user', { + name: 'User', + description: 'Description', + scope: { + scope_id: 'global', + type: 'global' + } + }); + const serializedRecord = record.serialize(); + assert.equal(serializer.serializeScopeID, false); + assert.deepEqual(serializedRecord, { + name: 'User', + description: 'Description' + }); + }); + + test('it does serialize scope_id when serializeScopeID is true', function (assert) { + assert.expect(2); + const store = this.owner.lookup('service:store'); + const serializer = store.serializerFor('user'); + serializer.serializeScopeID = true; + const record = store.createRecord('user', { + name: 'User', + description: 'Description', + scope: { + scope_id: 'global', + type: 'global' + } + }); + const serializedRecord = record.serialize(); + assert.equal(serializer.serializeScopeID, true); + assert.deepEqual(serializedRecord, { + name: 'User', + description: 'Description', + scope_id: 'global' + }); + }); + test('it serializes non-nullish version fields', function (assert) { assert.expect(2); const store = this.owner.lookup('service:store'); diff --git a/addons/api/tests/unit/serializers/target-test.js b/addons/api/tests/unit/serializers/target-test.js index e37eb1de2..cc4413567 100644 --- a/addons/api/tests/unit/serializers/target-test.js +++ b/addons/api/tests/unit/serializers/target-test.js @@ -15,6 +15,10 @@ module('Unit | Serializer | target', function(hooks) { { host_set_id: '1', host_catalog_id: '2' }, { host_set_id: '3', host_catalog_id: '4' } ], + scope: { + scope_id: 'org_1', + type: 'org' + }, version: 1 }); const snapshot = record._createSnapshot(); @@ -24,7 +28,8 @@ module('Unit | Serializer | target', function(hooks) { name: 'User', description: 'Description', version: 1, - protocol: null + protocol: null, + scope_id: 'org_1' }); });