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
underscore in field name does not work with @Groups #1554
Comments
Am I the only one using underscores :) ? |
@hariszukanovic : I have the same problem |
@hariszukanovic I have the same problem and it even weirder. Suppose you have a field name Now, do this:
Notice I just duplicated the variable and set the second camelCase one to Result: your variable is on the api response named like the second variable even though the colum
|
Same to me. Super weird bug. |
Should this be fixed in the next version?
…On Tue, 1 May 2018, 20:47 Damián Rubio, ***@***.***> wrote:
Same to me. Super weird bug.
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#1554 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABLXKNR9CJaqp512t2aZ2-cY39caM9hqks5tuK3cgaJpZM4Q55u_>
.
|
Could you please provide a little project based on https://github.com/api-platform/api-platform in order to test this behaviour ? |
@Simperfit if any field of the ApiResource entity has an underscore "_" the field will be ReadOnly. Is simple as that. |
I suspect this has to do with how the Symfony PropertyInfo component extracts accessor and mutator methods, specifically: If your property name does not follow the convention, you'd have to add the missing property metadata yourself. |
Would you have a working example of this (should I call it workaround?)? |
I have the same problematic behavior w/ fields prefixed with
If you remove the group strat too, I guess you'll see your field camelized. EDIT: woops, never mind, i messed up with the getter method. |
closing, use custom metadata if the behavior differs from the one expected by the property info as @teohhanhui said |
Even weirder behaviour is that if it´s camel case, api platform returns array of IRIs if its snake case it returs list of objects in some places and in some scenarios and in some it returns objects. My project takes like 2x longer with api platform than without it. Took my few hours to figure out the underscore thing, fixed part of my code, but broke the other part. So i have no idea what to do. |
I'm not sure what the "custom metadata" solutions refers to, that only lead me to SQL errors because the DB columns are still "some_thing" and setting the name property in @Orm\Column to "someThing" broke the queries. But if you find this issue on google because you're naming your Entity properties "some_thing" and they don't show in your API responses, it's because you need to name them "someThing". If you want "some_thing" in the the API response, do this: From the docs: https://api-platform.com/docs/core/serialization/#name-conversion |
Using camel case in variables only is mandatory to develop Symfony bundles or help in the Symfony project. If your project follow PSR-12 (PSR-2 is deprecated) you can choose what do you want for variable names. I always choose snake case and have use a lot of different bundles without problems since Symfony 2, and reflection works fine in all cases. /**
* @ORM\Column(type="int")
* @Groups({"baz:read", "baz:write"})
*/
private $foo_bar; to /**
* @ORM\Column(type="int")
*/
private $foo_bar;
/**
* @Groups({"baz:read"})
*/
public function getFooBar(): ?int
{
return $this->foo_bar;
}
/**
* @Groups({"baz:write"})
*/
public function setFooBar(int $foo_bar): self
{
$this->foo_bar = $foo_bar;
return $this;
} |
By the way another workaround is a modification to @TeLiXj answer because for me rewriting annotations for 207 properties plus getters and setters was looking like a chore.
Changed getters and setters from:
To (notice the underscore placement):
And then docs post example shows:
And return example shows:
Which is what i was after. |
Finally, after update to PHP 8 and move from annotations to attributes works at least on read |
… isReadable() when checking snake_case properties (jbtronics) This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [PropertyInfo] Make isWriteable() more consistent with isReadable() when checking snake_case properties | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Related with issues #29933 and #16889 | License | MIT | Doc PR | Currently PropertyInfo is a bit inconsistent in the behavior of isWriteable() and isReadable() when using it with properties in snake_case format, which are only accessible via a getter and setter. To be readable the getter function has to be in camel_case (e.g. `getSnakeCase()`), while the setter function has to remain in the snake case format (e.g. `setSnake_case()`). In this example class: ```php class Dummy { private string $snake_case; public function getSnakeCase(): string { } public function setSnakeCase(string $snake_case): void { } } ```` the property `snake_case` is considered readable like you would expect, but not writeable, even though the property has a useable setter and the value can be actually be modified fine by something like the PropertyAccess component. To make the property actually considered writeable, the setter would need to be named `setSnake_case`, which is pretty inconsistent with the behavior of isReadable and makes it very hard to use this component on snake_case properties. This inconsistencies are caused by the fact, that `isReadable` in ReflectionExtractor uses the `getReadInfo()` function which does a camelization of the property name internally, while the `isWriteable()` function uses the `getMutatorMethod()` function which just perform a capitalization of the first letter. This PR fixes this inconsistencies between isReadable() and isWriteable() by allowing to use a camelCase style setter to be considered writeable, as this is much more common then to use the mix of snake and camelCase currently required. The getWriteInfo() function is not useable here, because it needs both a add and remove Function on collection setters to give proper infos, while the current `isWriteable()` implementation considers a class with just a add or a remove function as writeable. Therefore the property name just gets camelized before being feed into the `getMutatorMethod()`, which gives the desired result. To maximize backwards compatibility, if no camelCase style setter is found, it is still checked for a snake_case setter, so that classes having these, are still considered writeable after the change. The current behavior is causing some inconsistencies in higher level libraries, which rely on this component. In API Platform for example properties in snake_case are considered read only even though they have a (camel case) setter, because of this. See issue: api-platform/core#5641 and api-platform/core#1554 Commits ------- 7c9e6bc [PropertyInfo] Make isWriteable() more consistent with isReadable() when checking snake_case properties
… isReadable() when checking snake_case properties (jbtronics) This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [PropertyInfo] Make isWriteable() more consistent with isReadable() when checking snake_case properties | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | no | New feature? | yes | Deprecations? | no | Tickets | Related with issues #29933 and #16889 | License | MIT | Doc PR | Currently PropertyInfo is a bit inconsistent in the behavior of isWriteable() and isReadable() when using it with properties in snake_case format, which are only accessible via a getter and setter. To be readable the getter function has to be in camel_case (e.g. `getSnakeCase()`), while the setter function has to remain in the snake case format (e.g. `setSnake_case()`). In this example class: ```php class Dummy { private string $snake_case; public function getSnakeCase(): string { } public function setSnakeCase(string $snake_case): void { } } ```` the property `snake_case` is considered readable like you would expect, but not writeable, even though the property has a useable setter and the value can be actually be modified fine by something like the PropertyAccess component. To make the property actually considered writeable, the setter would need to be named `setSnake_case`, which is pretty inconsistent with the behavior of isReadable and makes it very hard to use this component on snake_case properties. This inconsistencies are caused by the fact, that `isReadable` in ReflectionExtractor uses the `getReadInfo()` function which does a camelization of the property name internally, while the `isWriteable()` function uses the `getMutatorMethod()` function which just perform a capitalization of the first letter. This PR fixes this inconsistencies between isReadable() and isWriteable() by allowing to use a camelCase style setter to be considered writeable, as this is much more common then to use the mix of snake and camelCase currently required. The getWriteInfo() function is not useable here, because it needs both a add and remove Function on collection setters to give proper infos, while the current `isWriteable()` implementation considers a class with just a add or a remove function as writeable. Therefore the property name just gets camelized before being feed into the `getMutatorMethod()`, which gives the desired result. To maximize backwards compatibility, if no camelCase style setter is found, it is still checked for a snake_case setter, so that classes having these, are still considered writeable after the change. The current behavior is causing some inconsistencies in higher level libraries, which rely on this component. In API Platform for example properties in snake_case are considered read only even though they have a (camel case) setter, because of this. See issue: api-platform/core#5641 and api-platform/core#1554 Commits ------- 7c9e6bc36e [PropertyInfo] Make isWriteable() more consistent with isReadable() when checking snake_case properties
I am using doctrine.orm.naming_strategy.underscore, in generated entities, the member variables are named in snake case.
When using
@Groups
to select fields, any of the fields with an underscore in their name are missing from the response.Example: service_index
Any fields that are a single word (no underscore) do not exhibit this problem.
Also, when not using
@Groups
everything works just fine.If variables are named in camel case, they appear in the response even when using
@Groups
and even if the column in the DB is still named in snake case.Example: serviceIndex
v2.0.11 and v.2.1.4 tested.
The text was updated successfully, but these errors were encountered: