Typescript generator: Handle resources with explicit id field #132
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
If any of our resources are identified using Doctrine's Identity Through Foreign Entities (https://www.doctrine-project.org/projects/doctrine-orm/en/2.6/tutorials/composite-primary-keys.html#identity-through-foreign-entities) and they also declare a getter function to serialize their id in the id property of the response, then the standard template defined for the typescript interface does not work for them since it creates 2 members named
id
in the interface. This PR fixes it by removingid
as a hard-coded field in the Handlebars template and by appending the id field to the list of fields to generate only if no property named id exists.Code example:
Entity.php
When this get serialized by the serializer, we get
Without the
getId()
getter we would only have theuser
property in the response.However, by adding this getId, I noticed that Api platform would add a new
id
field in the Hydra documentation (standard resources that have a trueid
property do not expose anid
field in the documentation, I don't know why though), and this would in turn cause the client generator to generate an interface such as (imports omitted for brevity)This makes the typescript compiler not happy as we have a re-definition of the
id
field.So, maybe this could be solved on the PHP side by always specifying the
id
field in the documentation, but for the time being this solves the issue by selectively adding theid
field only when it's not already declared on the resource.