Skip to content

Fix webapi returns variables in JSON using snake case and I need came… #31001

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

Closed
wants to merge 4 commits into from

Conversation

jayankaghosh
Copy link
Contributor

@jayankaghosh jayankaghosh commented Nov 20, 2020

Description (*)

Added objectKeyMap which accepts parameters from di.xml to modify data object keys
We can now modify the keys of any accessor on any class used in the rendering of the REST API, using the following (or similar) snippet in the di.xml of the webapi_rest directory.

<type name="Magento\Framework\Reflection\DataObjectProcessor">
        <arguments>
            <argument name="objectKeyMap" xsi:type="array">
                <item name="Magento\Cms\Api\Data\PageInterface" xsi:type="array"> <!-- Change the accessors keys of this class -->
                    <item name="page_layout" xsi:type="string">PageLayout</item> <!-- page_layout should be shown as PageLayout in the ouput -->
                </item>
            </argument>
        </arguments>
    </type>

Fixed Issues (if relevant)

  1. Fixes webapi returns variables in JSON using snake case and I need camelCase #24681 : webapi returns variables in JSON using snake case and I need camelCase

Manual testing scenarios (*)

  1. Add a di.xml inside webapi_rest directory of any module and inject object key names to it, similar to the following
<type name="Magento\Framework\Reflection\DataObjectProcessor">
        <arguments>
            <argument name="objectKeyMap" xsi:type="array">
                <item name="Magento\Cms\Api\Data\PageInterface" xsi:type="array">
                    <item name="page_layout" xsi:type="string">PageLayout</item>
                </item>
            </argument>
        </arguments>
    </type>
  1. Call the REST API and observe the new keys reflecting.

Contribution checklist (*)

  • Pull request has a meaningful description of its purpose
  • All commits are accompanied by meaningful commit messages
  • All new or changed code is covered with unit/integration tests (if applicable)
  • All automated tests passed successfully (all builds are green)

@m2-assistant
Copy link

m2-assistant bot commented Nov 20, 2020

Hi @jayankaghosh. Thank you for your contribution
Here is some useful tips how you can test your changes using Magento test environment.
Add the comment under your pull request to deploy test or vanilla Magento instance:

  • @magento give me test instance - deploy test instance based on PR changes
  • @magento give me 2.4-develop instance - deploy vanilla Magento instance

❗ Automated tests can be triggered manually with an appropriate comment:

  • @magento run all tests - run or re-run all required tests against the PR changes
  • @magento run <test-build(s)> - run or re-run specific test build(s)
    For example: @magento run Unit Tests

<test-build(s)> is a comma-separated list of build names. Allowed build names are:

  1. Database Compare
  2. Functional Tests CE
  3. Functional Tests EE,
  4. Functional Tests B2B
  5. Integration Tests
  6. Magento Health Index
  7. Sample Data Tests CE
  8. Sample Data Tests EE
  9. Sample Data Tests B2B
  10. Static Tests
  11. Unit Tests
  12. WebAPI Tests

You can find more information about the builds here

ℹ️ Please run only needed test builds instead of all when developing. Please run all test builds before sending your PR for review.

For more details, please, review the Magento Contributor Guide documentation.

⚠️ According to the Magento Contribution requirements, all Pull Requests must go through the Community Contributions Triage process. Community Contributions Triage is a public meeting.

🕙 You can find the schedule on the Magento Community Calendar page.

📞 The triage of Pull Requests happens in the queue order. If you want to speed up the delivery of your contribution, please join the Community Contributions Triage session to discuss the appropriate ticket.

🎥 You can find the recording of the previous Community Contributions Triage on the Magento Youtube Channel

✏️ Feel free to post questions/proposals/feedback related to the Community Contributions Triage process to the corresponding Slack Channel

@m2-community-project m2-community-project bot added Priority: P4 No current plan to fix. Fixing can be deferred as a logical part of more important work. Severity: S4 Affects aesthetics, professional look and feel, “quality” or “usability”. labels Nov 20, 2020
@ihor-sviziev
Copy link
Contributor

@magento run all tests

Copy link
Contributor

@ihor-sviziev ihor-sviziev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jayankaghosh,
In general your changes looks good to me.

Could you make the new method to be private (due to backward compatibility reasons) and cover your changes with any type of automated tests?

@@ -128,7 +136,7 @@ public function buildOutputDataArray($dataObject, $dataObjectType)
}
}

$outputData[$key] = $value;
$outputData[$this->mapObjectKey($key, ltrim($dataObjectType, '\\'))] = $value;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we move ltrim inside the mapObjectKey method?

Copy link
Contributor Author

@jayankaghosh jayankaghosh Nov 24, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could. But that wouldn't make any difference to the flow right? The mapObjectKey should only be responsible for mapping the object keys. Which is why I had kept the ltrim (to include any class names with preceding slashes given in the di.xml as well) outside of it

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with it that it might not find the correct key, if in the client code we'll not do ltrim.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think I understand. The mapObjectKey can't be called from any other class. It'll just be called from the buildOutputDataArray function, and we have done the ltrim there

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. but in case if someone will adjust this code a bit later or will call it in the another method - this issue will appear. It's better to incapsulate this logic inside the method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. Okay. In that case I think it's better that we name the function something other than mapObjectKey?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getKeyByObjectType?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Guess that'll work

Copy link
Contributor

@ihor-sviziev ihor-sviziev left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @jayankaghosh ,
Now the changes looks good to me. Could you cover your changes with some kind of automated test?

@aredridel
Copy link
Contributor

Hi @ihor-sviziev I am not affiliated with this PR in any way

@ihor-sviziev
Copy link
Contributor

@aredridel , I just pressed wrong nick from the autocomplete. Sorry for disturbing you

@ihor-sviziev
Copy link
Contributor

@magento run all tests

@jayankaghosh
Copy link
Contributor Author

Hi @jayankaghosh ,
Now the changes looks good to me. Could you cover your changes with some kind of automated test?

@ihor-sviziev I'm not sure how to proceed with writing the tests for this. Some help would be appreciated

@ihor-sviziev
Copy link
Contributor

Hi @engcom-Bravo @engcom-Charlie @engcom-Delta could you help with cover thing this PR with automated tests?

@ihor-sviziev
Copy link
Contributor

ihor-sviziev commented Jan 5, 2021

Maybe @engcom-Hotel @sdzhepa could help?

@jayankaghosh
Copy link
Contributor Author

@ihor-sviziev Guess they've abandoned us 😛

@sdzhepa
Copy link
Contributor

sdzhepa commented Feb 5, 2021

Hello @ihor-sviziev @jayankaghosh

Sorry for the late response.

Let me please clarify the situation.
We are working(fix, covering, QA, delivery, etc) and focusing on the Hight Priority(P0-P1-P2) tickets first.
This issue was triaged as Priority: P4 . Currently, we do not have a plan to work on it

@xtremevision
Copy link

Can this solution be used? Perhaps with an override/plugin?

@jayankaghosh
Copy link
Contributor Author

@sdzhepa When can I expect an update on this?

@xtremevision
Copy link

xtremevision commented Sep 20, 2022

@sdzhepa When can I expect an update on this?

Add this in werbapi_rest\di.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
	<type name="[Vendor]\[Module]\Plugin\DataObjectProcessor">
        <arguments>
            <argument name="objectKeyMap" xsi:type="array">
                <item name="[Vendor]\[Module]\Api\Data\[InterfaceFileWhereVarsAreDeclared]" xsi:type="array">
                    <item name="order_item" xsi:type="string">OrderItem</item>
                    <item name="transaction_id" xsi:type="string">TransactionID</item>
                </item>
            </argument>
        </arguments>
    </type>
</config>

This way you can name your vars as you please.

@tuyennn
Copy link
Member

tuyennn commented Oct 5, 2024

@jayankaghosh could I take your pull request to another one with test unit #39237, any problem with you?

@jayankaghosh
Copy link
Contributor Author

@jayankaghosh could I take your pull request to another one with test unit #39237, any problem with you?

No problem man. Please do

@engcom-Dash
Copy link
Contributor

Hello @jayankaghosh,

Thanks for the contributions!

Please resolve the conflict so we can proceed with this PR.

Thanks.

@tuyennn
Copy link
Member

tuyennn commented Nov 13, 2024

@engcom-Dash @jayankaghosh any problem if we close this and switch to #39237

@tuyennn
Copy link
Member

tuyennn commented Jan 17, 2025

@jayankaghosh can you grand me the collaborator access on https://github.com/jayankaghosh/magento2/, I can push my change and follow this pull request

@engcom-Charlie engcom-Charlie self-assigned this Feb 10, 2025
@engcom-Charlie
Copy link
Contributor

Hi @jayankaghosh,

Thank you for your contribution!

@tuyennn, as @jayankaghosh aggred to go ahead with your PR here and we didnt get any input on conflict resolution, we are closing this PR now.

@jayankaghosh, please feel free to reopen once done with changes.

@tuyennn
Copy link
Member

tuyennn commented Feb 10, 2025

@engcom-Charlie I mean #39237 not this pull request

@engcom-Charlie
Copy link
Contributor

@tuyennn you have asked to continue on ##39237 in your comment and as @jayankaghosh is okay to proceed with your request in his comment, I have closed this PR and you can continue on ##39237 further.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Auto-Tests: Not Covered Changes in Pull Request requires coverage by auto-tests Award: bug fix Component: Reflection help wanted Priority: P4 No current plan to fix. Fixing can be deferred as a logical part of more important work. Progress: needs update Release Line: 2.4 Severity: S4 Affects aesthetics, professional look and feel, “quality” or “usability”.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

webapi returns variables in JSON using snake case and I need camelCase
9 participants