-
-
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
Change _normalizeTypeKey to defer normalization to the container #2821
Conversation
|
||
deepEqual(json["evilMinionType"], "yellowMinion"); | ||
}); | ||
|
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.
I believe this test is incorrect.
- You should never manually set a
typeKey
. - The
YellowMinion
was registered into the container with camelCase. - This is a unit test, there is no normalization of model names
Looking into polymorphics, I am confused by the type usage. Sometimes it seems like we trust the typeKey (serialization) other times we camelize it (relationType).
062cd51
to
6e22154
Compare
I'm also in favor of adding a Overall I like this pr. I wanted to bring up a that one of @tomdale's reasons for suggesting Ember Data issues a deprecation warning when it detects caps (that may not have been well communicated in the comment in #2766) was the fear that users would have to change their type argument the |
@bmac I believe a user can move app code between globals and Ember-CLI smoothly today. The default ember application container normalization should be sufficient. Moving unit tests is the mess: There is no normalization, but the model names in the container have changed. Thus you end up being forced into dasherized format in unit tests. I'll add |
2a42c50
to
8165ff7
Compare
Slight change of plans: I did not implement a Need to add a test for new Did some cleanup of embedded records mixin. The tests make a lot of assumptions about camelCasing. Need to add some for serialization. |
8165ff7
to
3f590b5
Compare
@bmac @igorT @stefanpenner I'd appreciate a look. I think this is good to go. |
First, remember that isolated containers have no normalization. When testing with isolated containers, the user-entered model key becomes the container object name as well as the type key. For example, in a unit test this modelFor works like this: * `modelFor("SomeItem")` * Looks for `"model:SomeItem"` in the container * This is normalized to `"model:SomeItem"` (unit tests have no normalization) * The object is returned. In Ember-CLI, a error. In globals Ember, `App.SomeItem`. * The typeKey is set to the normalized value: `SomeItem` In an acceptance or app environment the container has a different resolver. In these cases normalization changes things. For example, with the Ember-CLI resolver: * `modelFor("SomeItem")` * Looks for `"model:SomeItem"` in the container * This is normalized to `"model:some-item"` (Ember-CLI is dasherizing) * The object is returned. In Ember-CLI, `app/models/some-item`. * The typeKey is set to the normalized value: `some-item` What must be added to this confusing situation is that RESTAdapter defers to the `typeKey` for model keys. If you change your resolver, you will change the requests sent to your server and the way data is parsed.
468ad0f
to
e038e41
Compare
This is rebased and passing. Waiting on a +1 or further discussion. |
import DS from "ember-data"; | ||
export default DS.RESTSerializer.extend({ | ||
typeForRoot: function(root) { | ||
return subRoot.singularize().dasherize(); |
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.
Does this mean Ember CLI users will still need to use a custom Serializer after this pr is merged?
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.
I want to stay really specific here. I don't think most devs know that a dasherizing typeForRoot
can make their ember-cli unit tests work properly (with multi-word model names). The current code will pass a camelized name to the store (and then container, where there is no normalization), and not find the model. That behavior is not changed in this PR.
I would recommend that they use a singularize then dasherize typeForRoot
, yeah.
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.
do conventional API's return dasherized responses?
We discussed this a bit at the ember-data meeting.
|
can you also explain what this property will be used for.
only the resolver needs these deprecation warnings |
It is the new public interface for model names. Always dasherized, which matches our conventions across the board (once the deprecations are added to the resolvers)
Ah, so the normalization is different in the three places I listed. It seems correct that the resovler may be the only things that needs to throw, but the normalization should probably be reviewed. |
What are the public usages of this api? (i am just curious why a name placed on a factory is ever needed) |
all normalization must go through the resolver |
when building a url in the adapter. during serialization of a polymorphic model. when determining the root property name of a create payload.
This has changed recently. Looks like you're right. I see at least these:
So "the resolver" translates back to my original statement. "Ember's default container, the isolated container, and the Ember-CLI container must raise a warning or deprecation notice for camelCase lookups." It seems a non sequitur to raise that normalization goes through the resolver. The behavior still needs to change in three situations, correct? |
i want to clarify further, to make sure we remove all mis-behaving code. The resolver is the only thing that knows about naming conventions, and as such it is the only place we should put these deprecations. With one exception, and that is plural/singlular inflections. If other exceptions exist, I strongly suspect that is demonstrating a bug, that we must resolve. |
@return {String} the model's name in your request payloads | ||
*/ | ||
typeForPayload: function(key) { | ||
return camelize(singularize(key)); |
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.
this is plucking from the payload correct?
The idea here is that you shouldn't be able to register anything in the container that is not dasherized either. |
registration itself is some-what of an anti-pattern, most app scenarios should ideally be resolved. |
Each version of Ember data that comes out I check to see if perhaps this has been completed :) Any idea on when this may make it into a beta? 1.0 is coming quickly and hoping this does not get deferred to 2.x I currently can not use the mock http facility and would very much like to. |
Looking at the code more closely, this particular problem is indeed fixed.(Tried beta.18, could be fixed earlier though) The correct adapter is being called. My test is still failing because the http-mock is not being installed. I have found that the tests work using http:/localhost:4200/tests but does not work with ember test -s. Any ideas? |
I believe much of this has been addressed in beta.18 in other PRs by @fivetanley (thank you sir). I'm closing, though I'd like to port my tests from here forward to master now that his code has landed. |
Are mocks suppose to be installed when running ember test -s? |
@cah-briangantzler I asked @rwjblue about mocks with |
Let's talk about Ember-Data model resolution works in this PR.
First, remember that isolated containers have no normalization. When testing with isolated containers (unit tests), the user-entered model name becomes the factory name requested from the container as well as the typeKey.
For example, in a unit test modelFor works like this:
modelFor("SomeItem")
"model:SomeItem"
in the container"model:SomeItem"
(unit tests have nonormalization)
App.SomeItem
.SomeItem
In an acceptance or app environment the container has a different resolver. In these cases normalization changes things. For example, with the Ember-CLI resolver:
modelFor("SomeItem")
"model:SomeItem"
in the container"model:some-item"
(Ember-CLI is dasherizing)app/models/some-item
.some-item
There are several points of confusion IMO. I'd like to suggest two followup issues:
modelFor
. This normalization would ensure that Ember-Data, when there is no normalization provided by the container, always normalizes its own requests to camelCase or some other consistent format instead of blindly trying to trust user input.typeKey
for model keys. If you change your resolver, you will change the requests sent to your server and the way data is parsed.typeForRoot
here (which is definitely just a happens to work fix, it camelizes) or introducing arootForType
.rootForType
with a default of camelization (based on other typeKey normalization in that file) is correct and I believe necessary.typeForRoot
as well.See also: