Skip to content
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

fix exception when type schema json contains x-psx-mapping attr #28

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

AMar4enko
Copy link

@AMar4enko AMar4enko commented Dec 11, 2024

I ran into an edge case using psx/schema to generate PHP DTOs from TypeScript via intermediary TypeSchema json representation.
On TypeScript side, I have field metadata, identifying source DB field name, that is in underscore case. I was looking for an option to capture this field name mapping in TypeSchema. After a quick look into the repo, I discovered I could make use of custom x-psx-mapping TypeSchema JSON attribute (which isn't documented btw).

This commit fixes PHP parser/generator combo exception when dealing with x-psx-mapping attribute.

@AMar4enko
Copy link
Author

AMar4enko commented Dec 19, 2024

Hey @chriskapp thank you for the feedback!
Alright, I was in doubts which file this particular fix belongs, decided to put it in BCLayer.php since I felt it served as kind of data normalization layer prior parsing step. As you correctly pointed out, TypeSchema parser does consider custom x-psx properties and stores them into type attributes. Please have a look at

$mapped = $mapping[$raw] ?? $raw;

It expects mapping type attribute (which gets parsed from x-psx-mapping attribute of TypeSchema definition JSON) in form of associative array, whereas parser by default parses JSON into stdClass tree. Thus adding x-psx-mapping attribute to TypeSchema JSON causes all code generators throw an error due to mapping attribute value being instance of stdClass gets treated as associative array. As to small addition to source_typeschema.json - it triggers the edgecase in such a way it doesn't break any other tests.

@chriskapp
Copy link
Member

@AMar4enko ok, I have also looked into this in detail and this indeed a bug at the TypeSchema parser, good catch, I think the fix is fine but could you adjust the test cases a little bit, we should move this to the TypeSchema parser test case and there we can compare the attributes which we already have, I have created a patch which shows the adjustments s.
fix_parse_mapping_attribute.patch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants