-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FEATURE ds-payload-hooks] Add hooks to map type in payload to modelName #4318
Conversation
Overall looks good. Do you mind wrapping this in a feature flag? |
let modelName = this.modelNameFromPayloadType(relationshipDataHash.type); | ||
let deprecatedModelNameLookup = this.modelNameFromPayloadKey(relationshipDataHash.type); | ||
|
||
if (modelName !== deprecatedModelNameLookup && this.modelNameFromPayloadType === JSONAPISerializer.prototype.modelNameFromPayloadType) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like if a user intentionally wanted modelNameFromPayloadType
and modelNameFromPayloadKey
to return different values and they needed to define a custom modelNameFromPayloadType
we would always hit the deprecated case.
What about if we always called modelNameFromPayloadType
but had its default implementation just call modelNameFromPayloadKey
?
modelNameFromPayloadType
and payloadTypeFromModelName
hooksThe `modelNameFromPayloadKey` and `payloadKeyFromModelName` hooks on the serializer allow to customize the mapping between the key of a JSON and the corresponding name of the model. Consider the following payload: ```json { "blog/post": { "id": 1 } } ``` To map the "blog/post" key to the `post` model, the `modelNameFromPayloadKey` hook is used: ```js serializer.modelNameFromPayloadKey("blog/post") ``` --- There are some issues with the current code base, as the `modelNameFromPayloadKey` and `payloadKeyFromModelName` hooks are also used to map the "value" of a type from / to the payload. Consider the following payload of a JSON-API document: ```json { "data": { "id": 1, "type": "API:V1::User" } } ``` To map the namespaced type to the model name ember-data is using, currently the `modelNameFromPayloadKey` is used: ```js serializer.modelNameFromPayloadKey("API::V1::User") ``` Now this gets complicated if your API responds with the following payload (using custom key and custom types for polymorphic records for example): ```json { "blog/post": { "id": 1, "user": 2, "userType": "API:V1::Administrator" } } ``` Now the `modelNameFromPayloadKey` is invoked for both: ```js serializer.modelNameFromPayloadKey("blog/post") serializer.modelNameFromPayloadKey("API::V1::Administrator") ``` This means that the logic within the hook would get complicated. --- As the name suggests, the method should only be used to map the key, not the value. This commit adds `modelNameFromPayloadType` and `payloadTypeFromModelName` hooks and uses them during normalization and serialization. This commit maintains the old behavior using, if it is used and logs a deprecation warning to move to the new hooks.
We should also make an ember-watson transform after this gets merged. |
@bmac I've updated the code and put everything behind the You're comment made me re-think the approach and now the following "rules" apply:
|
The
modelNameFromPayloadKey
andpayloadKeyFromModelName
hooks on theserializer allow to customize the mapping between the key of a JSON and
the corresponding name of the model. Consider the following payload:
To map the "blog/post" key to the
post
model, themodelNameFromPayloadKey
hook is used:There are some issues with the current code base, as the
modelNameFromPayloadKey
andpayloadKeyFromModelName
hooks are alsoused to map the "value" of a type from / to the payload. Consider the
following payload of a JSON-API document:
To map the namespaced type to the model name ember-data is using,
currently the
modelNameFromPayloadKey
is used:Now this gets complicated if your API responds with the following
payload (using custom key and custom types for polymorphic records for
example):
Now the
modelNameFromPayloadKey
is invoked for both:This means that the logic within the hook would get complicated.
As the name suggests, the method should only be used to map the key, not
the value. This commit adds
modelNameFromPayloadType
andpayloadTypeFromModelName
hooks and uses them during normalization andserialization.
This commit maintains the old behavior using, if it is used and logs a
deprecation warning to move to the new hooks.
This PR should help tackling the issues raised in #3801 and #4208.