Open NotionCommotion opened 1 year ago
Not a great solution either, but you might get the right behavior by explicitly specifying uriTemplates for your operations, without having to add a dummy property to your class...?
@mrossard Appreciate your suggestion. I expect it will work, however, I feel it is on par with my dummy property solution and bothered changing, and my opinion is that the right solution is not for ApiPlatform to require an ID called "id".
I definitely agree, that just seemed "not as bad" of a solution while waiting for a real fix.
In fact, just adding #[ApiProperty(identifier: true)] to your $code might be (still not perfect but) enough...?
is it possible that https://github.com/api-platform/core/commit/1170c384636bbad999f037cedcdb4190a8028360 fixes that issue?
@soyuka Nope
Mhh okay I'll investigate. id
is automatically detected as identifier but if this breaks it's probably not good.
Also related to this kind of work on APIs I'd suggest to split the resource from the entity (https://github.com/api-platform/core/pull/5409#discussion_r1119900564) I need to document this better though.
I debugged this, and in case where id
field does not exist, the $identifiers
variable is populated like this
array:1 [
"id" => "name=Cost of Goods Sold;id=Cost of Goods Sold"
]
I even have the operations url templates defined properly
#[ApiResource(
operations: [
new GetCollection(
uriTemplate: "/accounts.{_format}",
requirements: ['name', '.*'],
security: "is_granted('ROLE_ACCOUNT_LIST')"
),
new Get(
uriTemplate: "/accounts/{name}.{_format}",
requirements: ['name' => '.*'],
security: "is_granted('ROLE_ACCOUNT_LIST')"
),
new Post(
uriTemplate: "/accounts.{_format}",
security: "is_granted('ROLE_ACCOUNT_CREATE')"
),
new Delete(
uriTemplate: "/accounts/{name}.{_format}",
security: "is_granted('ROLE_ACCOUNT_DELETE')"
),
new Put(
uriTemplate: "/accounts/{name}.{_format}",
security: "is_granted('ROLE_ACCOUNT_EDIT')"
),
new Patch(
uriTemplate: "/accounts/{name}.{_format}",
security: "is_granted('ROLE_ACCOUNT_EDIT')"
)
], normalizationContext: ['groups' => ['chart']])]
adding this solves the issue as @NotionCommotion said but I really dont need that id field
#[ApiProperty(identifier: false)]
private ?int $id = null;
API Platform version(s) affected: 3.1.2
Description
If an identifier named something different that
$id
is used, a second identifier named$id
is automatically created.How to reproduce
When using 3.0, the path to a NaicsCode resource was
/naics_codes/123
, but with 3.1 it isnaics_codes/code=123;id=123
Possible Solution
Not a good long term solution, but adding this appears to work.
Additional Context