fix: normalize embedded scope IDs and ensure scope resources do not receive scope-prefixed URLs like other resources do

This commit is contained in:
Randall Morey committed 2020-07-23 10:34:31 -04:00
1 parent c743cc1716
commit b7996f6271
4 files changed
+47 -12

No files matched your search

+10 -4
View File
@@ -71,10 +71,16 @@ export default class ApplicationAdapter extends RESTAdapter.extend(
*/
urlPrefix(path, parentURL, modelName, id, snapshot={}) {
const prefix = super.urlPrefix(...arguments);
let scopePath = getWithDefault(snapshot, 'adapterOptions.scopeID', '');
// Ensure a slash is added between prefix + scope path if needed.
if (scopePath && prefix.charAt(prefix.length - 1) !== '/') {
scopePath = `/scopes/${scopePath}`;
const isScope = modelName === 'scope';
let scopePath = '';
// Only non-scope resources need a scope path, since scope resources
// aren't technically "scoped" the same way.
if (!isScope) {
scopePath = getWithDefault(snapshot, 'adapterOptions.scopeID', '');
// Ensure a slash is added between prefix + scope path if needed.
if (scopePath && prefix.charAt(prefix.length - 1) !== '/') {
scopePath = `/scopes/${scopePath}`;
}
}
return `${prefix}${scopePath}`;
}
+19 -1
View File
@@ -1,5 +1,6 @@
import RESTSerializer from '@ember-data/serializer/rest';
import { underscore } from '@ember/string';
import { get } from '@ember/object';
import { copy } from 'ember-copy';
/**
@@ -117,7 +118,7 @@ export default class ApplicationSerializer extends RESTSerializer {
primaryModelClass.modelName
);
// Copy the unrooted payload under the expected root key name.
transformedPayload[payloadKey] = copy(payload);
transformedPayload[payloadKey] = copy(payload, true);
// Return the result of normalizing the transformed payload.
return super.normalizeSingleResponse(
store,
@@ -128,4 +129,21 @@ export default class ApplicationSerializer extends RESTSerializer {
);
}
/**
* One consequence of using model fragments to represent the embedded scope
* is that they cannot have `id` fields. We still need to know scope IDs,
* so we copy the `scope.id` value into the `scope.scope_id` field.
* @override
* @see FragmentScope
* @param {Model} typeClass
* @param {Object} hash
* @return {Object}
*/
normalize(typeClass, hash) {
const normalizedHash = copy(hash, true);
const scopeID = get(normalizedHash, 'scope.id');
if (scopeID) normalizedHash.scope.scope_id = scopeID;
return super.normalize(typeClass, normalizedHash);
}
}