-
-
Notifications
You must be signed in to change notification settings - Fork 906
[Laravel] camelCase named Relations are ignored as they are denormalized as snake_case #6927
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
Comments
You shouldn't mix both camelCase and snake_case, to make things integrate well with Eloquent models, we're doing automatic transformations. If you want to do something more manual, I recommend to declare your |
Hello, Thanks for the answer. I am not trying to mix camelCase and snake_case, just following Laravel's standards so Relations are defined using camelCase. Maybe I didn't explain myself properly but the point is that having a Relation using camelCase ( {
"name": "GrandSon Name",
"grandFather": "/api/grand_fathers/1"
} It is converted to: {
"name": "GrandSon Name",
} by the default provider on ApiPlatformController so the If I switch the Relation's name not using camelCase so for example {
"name": "GrandSon Name",
"grandfather": "/api/grand_fathers/1"
} data remains on I think this is because the nameConverted SnakeCaseToCamelCaseNameConverter used on the Attributes is also used on Relations so it transforms grandFather to grand_father which doesn't exist on the Model. EDIT In order to add more context: The defualt provider uses AbstractObjectNormalizer.php from Symfony. In line 345 it converts the attributes name using the nameconverter. So, as it is using denormalize from SnakeCaseToCamelCaseNameConverter the Attribute grandFather is converted to grand_father so then it is ignored here because it is not on the |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. |
API Platform version(s) affected: 4.0.16
Description
On Laravel we use camelCase naming when the Model's name is composed. On relations, we also use that notation.
The relations uisng camelCase are ignored and the data from the paylod is not sent to the Processor.
How to reproduce
Having 2 Models:
GrandFather
GrandSon
Created using following migrations:
GrandFather
GrandSon
Using
POST
on GrandSon with the following Payload:so the whole call is:
It creates the GrandSon with no realtion at all (so
grand_father_id
is NULL.In order to investigate, if I Print the
$body
variable onApiPlatformController
just before the processor usesprocess
function on Line 98 I get:So the Relation is not just not persisted but the data is totally ignored and it is not sent to the Processor.
If then I try to switch the relation namig to all lowercase so on Model
GrandSon
:and I use the Following Payload:
Then I receive the expected error 500
"Nested documents for attribute \"grandfather\" are not allowed. Use IRIs instead."
so the Relation is processed (The error is a whole different story discussed on: #6882)On the other hand, I trid setting
on the realation which works but then you are forced to use
grand_father
parameter on the Payload.This is specially annoying for all those projects which have several Models and Relations already defined on the usual way and want to start using ApiPlatform.
Possible Solution
Not using SnakeCaseToCamelCaseNameConverter to denormalize relations.
I think the problem is using SnakeCaseToCamelCaseNameConverter to denormalize Relations.
This denormalization works on actual attributes so camelCase attributes defined on Laravel Models are converted to snake_case as this is how the fileds are named on DB but fails on relations.
Example: GrandFather relation is denormalized as grand_father
The text was updated successfully, but these errors were encountered: