-
Notifications
You must be signed in to change notification settings - Fork 169
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
Mirage cannot include non-camel cased model attributes when using json-api-serializer #247
Comments
@btrude Yes – as I wrote in my SO answer, Mirage needs to make assumptions about attribute and relationship name formatting within the confines of Mirage's data layer. The format of your API shouldn't affect how Mirage's data layer works – that's what the Serializer layer is for. If you're working with Mirage in your tests, for example, you shouldn't do server.create('user', {
first_name: 'Barry'
}); just because your API is snake-cased. There are too many formatting decisions around APIs to have them leak into Mirage's model & data layer. For example, APIs sometimes include a root, and sometimes don't. Therefore, Mirage's architecture is designed to keep all of these concerns within the serializer layer, and to keep Mirage's data layer using the camelCase convention everywhere. So, that line in the code does transform the keys into camelCase, because Mirage is expecting to find them that way. If your API sends snake-cased attributes for certain models, I would have those models share a customized Serializer that uses the |
I would definitely appreciate an example because the line I am calling out explicitly overrides any transformations that would have been done in the keyFor* functions making it impossible (as far as my limited understanding tells me) to override this behavior. Our serializer prior to running into this issue already made use of those functions to reconcile the shape of our data with mirage's expectations - it is only when we need to include relationships that are not camel cased on the model that we run into this issue (which admittedly is not often). |
Sure I can help with an example. Could you post a complete sample JSON response for exactly what you're trying to mock? |
Thank you @samselikoff I really appreciate the help! In this case we are making a request to: and expecting to get back something looking like this (attributes removed for readability):
|
Ok. So you want to camelize both the type name and the relationship names, correct? (Sorry about the delayed response! Busy holidays.) |
@samselikoff No worries, thanks for getting back to me! The issue is specifically with type names in the array of included relationships but we do also need to camelize type/relationship names across the board. |
Here's a simplified example: The relevant part is the application: JSONAPISerializer.extend({
typeKeyForModel(model) {
return snakeCase(model.modelName);
},
keyForAttribute(key) {
return snakeCase(key);
},
keyForRelationship(key) {
return snakeCase(key);
}
}) Does that response look like what you're after? |
So if I'm understanding correctly we should be setting up our serializer as such and then instantiating factories with camel cased attributes instead of what we were trying to accomplish previously which is instantiating factories with snake case attributes and then using the serializer to camelize them, correct? |
You got it 👍 Camel case everywhere in Mirage JS code, since it's JS and that's just normal JS convention. Then serializers to turn Mirage's data into the format your app expects based on your production API. |
Cool, thanks for clarifying. In trying to implement this in our codebase I'm now running into the issue of our models not having valid associations because we're now passing all of our factory attributes in camelized. Specifically I'm getting the following error:
Is there another serializer hook that I'm missing to handle this case? |
That error means you’re creating data somewhere with the ORM, and passing in ‘class-session’ as a model name where mirage expects ‘classSession’. Typically in mirage you create data either with factories in the ‘seeds()‘ method, in tests, or in POST or PUT route handlers. So it could be you or it could be one of Mirage’s shorthands. Depending on which, you might need to further customize the serializer. But I don’t think you will. It’s probably from a factory somewhere. |
In this case the error is being thrown from within a test like this:
|
Hm ok I don't think dasherize vs. camelCase is the problem here... can you show me your |
It occurs to me that I should have mentioned at some point that we are using Ember data and that mirage infers all of our models from our ember data models. I would guess that the suggestion here will be to create mirage-specific models that override the snake casing on our ember data models. |
Hm, yes that might be it. Though I would suggest to refactor your ED models, if it's not too late. Your API format should never leak into your ORM like that, ideally. |
Testing it now and creating a model with camelized attributes for
Completely agree, we're working with older code and an extremely naive implementation of mirage so hopefully we can get to that soon. Thanks again for all your help, I think this should solve everything. |
Awesome. Good luck + let me know if you need more help! |
Which attributes do you want to remove? and Im not sure I understand what you mean by need data inside of it. If you need the data, why are you removing them? The response is crafted by the mirage serializer. For what ever you need most likely you will need to create a serializer and customize that |
https://github.com/miragejs/miragejs/blob/master/lib/serializers/json-api-serializer.js#L450
Hopefully I'm not misunderstanding something here as I am new to mirage, but the line above explicitly transforms included relationship keys into camel case before checking for their presence on a given model. The attributes on our models are both snake and camel case so this line causes an error when queries attempt to include snake_cased keys. Overriding this function to check for snake and camel cased attributes has resolved our issues for now but perhaps it makes sense to allow for this behavior by default.
The text was updated successfully, but these errors were encountered: