Skip to content

webapi returns variables in JSON using snake case and I need camelCase #24681

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

Open
csdougliss opened this issue Sep 21, 2019 · 15 comments · May be fixed by #39237
Open

webapi returns variables in JSON using snake case and I need camelCase #24681

csdougliss opened this issue Sep 21, 2019 · 15 comments · May be fixed by #39237
Assignees
Labels
Component: Framework/Reflection Component: Webapi Use with concrete module component label E.g. "Component: Webapi" + "Catalog" feature request Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Priority: P4 No current plan to fix. Fixing can be deferred as a logical part of more important work. Progress: PR in progress Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Severity: S4 Affects aesthetics, professional look and feel, “quality” or “usability”. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it

Comments

@csdougliss
Copy link
Contributor

csdougliss commented Sep 21, 2019

Precondition (*)

Magento 2.3.x

Summary (*)

I want to return JSON in camelCase in my custom webapi.xml and interface that I have created.
There is no way to configure in webapi.xml the format that you wish to use.

By default M2 will return data like so: server_transaction_id but my third party JavaScript is expecting serverTransactionId for example.

Secondly, It is also rather annoying to have to create an interface to do this instead of just being able to use json_encode the data. Using json_encode is not the solution as you end up with \ in the format.

See below:

Examples (*)

interface Check3dsVersionInterface
{
    /**
     * @return string
     */
    public function getServerTransactionId();

    /**
     * @param string $serverTransactionId
     * @return $this
     */
    public function setServerTransactionId($serverTransactionId);

webapi.xml:

<route url="/V1/xx-payments/:cartId/check-3ds-version" method="POST">
        <service class="xx\xx\Api\xxServiceInterface" method="check3dsVersion"/>
        <resources>
            <resource ref="anonymous" />
        </resources>
    </route>

Proposed solution

In webapi.xml, it would be useful to specify the format of the json, for example to return as camelCase or as it is currently which is underscores

@m2-assistant
Copy link

m2-assistant bot commented Sep 21, 2019

Hi @craigcarnell. Thank you for your report.
To help us process this issue please make sure that you provided the following information:

  • Summary of the issue
  • Information on your environment
  • Steps to reproduce
  • Expected and actual results

Please make sure that the issue is reproducible on the vanilla Magento instance following Steps to reproduce. To deploy vanilla Magento instance on our environment, please, add a comment to the issue:

@magento give me 2.3-develop instance - upcoming 2.3.x release

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

@craigcarnell do you confirm that you were able to reproduce the issue on vanilla Magento instance following steps to reproduce?

  • yes
  • no

@magento-engcom-team magento-engcom-team added the Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed label Sep 21, 2019
@csdougliss csdougliss changed the title webapi returns variables in JSON using underscores and I need camelCase webapi returns variables in JSON using snake case and I need camelCase Sep 21, 2019
@engcom-Charlie engcom-Charlie self-assigned this Sep 23, 2019
@m2-assistant
Copy link

m2-assistant bot commented Sep 23, 2019

Hi @engcom-Charlie. Thank you for working on this issue.
In order to make sure that issue has enough information and ready for development, please read and check the following instruction: 👇

  • 1. Verify that issue has all the required information. (Preconditions, Steps to reproduce, Expected result, Actual result).

    DetailsIf the issue has a valid description, the label Issue: Format is valid will be added to the issue automatically. Please, edit issue description if needed, until label Issue: Format is valid appears.

  • 2. Verify that issue has a meaningful description and provides enough information to reproduce the issue. If the report is valid, add Issue: Clear Description label to the issue by yourself.

  • 3. Add Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 4. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 5. Add label Issue: Confirmed once verification is complete.

  • 6. Make sure that automatic system confirms that report has been added to the backlog.

@engcom-Charlie engcom-Charlie added Component: Webapi Use with concrete module component label E.g. "Component: Webapi" + "Catalog" Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release labels Sep 23, 2019
@ghost ghost unassigned engcom-Charlie Sep 23, 2019
@magento-engcom-team magento-engcom-team added the Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development label Sep 23, 2019
@magento-engcom-team
Copy link
Contributor

✅ Confirmed by @engcom-Charlie
Thank you for verifying the issue. Based on the provided information internal tickets MC-20328 were created

Issue Available: @engcom-Charlie, You will be automatically unassigned. Contributors/Maintainers can claim this issue to continue. To reclaim and continue work, reassign the ticket to yourself.

@ghost ghost added the feature request label Sep 27, 2019
@jayankaghosh
Copy link
Contributor

I am working on this

@jayankaghosh jayankaghosh self-assigned this Oct 21, 2019
@m2-assistant
Copy link

m2-assistant bot commented Oct 21, 2019

Hi @jayankaghosh. Thank you for working on this issue.
Looks like this issue is already verified and confirmed. But if you want to validate it one more time, please, go though the following instruction:

  • 1. Add/Edit Component: XXXXX label(s) to the ticket, indicating the components it may be related to.

  • 2. Verify that the issue is reproducible on 2.3-develop branch

    Details- Add the comment @magento give me 2.3-develop instance to deploy test instance on Magento infrastructure.
    - If the issue is reproducible on 2.3-develop branch, please, add the label Reproduced on 2.3.x.
    - If the issue is not reproducible, add your comment that issue is not reproducible and close the issue and stop verification process here!

  • 3. If the issue is not relevant or is not reproducible any more, feel free to close it.


@jayankaghosh
Copy link
Contributor

@craigcarnell instead of specifying whether to render in snake_case or CamelCase, which would again be restrictive, why don't we make sort of a map wherein through di.xml we can specify which field of which class should be called what?

@csdougliss
Copy link
Contributor Author

@jayankaghosh Whatever works is good with me 👍

@mlambley
Copy link

mlambley commented Jul 22, 2020

One can always hack the field namer. Works well enough, although I agree that an elegant solution would be preferred.

<!--di.xml-->
<type name="Magento\Framework\Reflection\FieldNamer">
    <plugin name="my_field_namer" type="Vendor\Project\Plugin\FieldNamer"/>
</type>
//Plugin/FieldNamer.php
use Magento\Framework\Api\SimpleDataObjectConverter;
use Magento\Framework\Reflection\FieldNamer as Subject;

class FieldNamer
{
    public function aroundGetFieldNameForMethodName(
        Subject $subject,
        callable $proceed,
        $methodName
    ) {
        if (substr($methodName, 0, 2) === Subject::IS_METHOD_PREFIX) {
            return SimpleDataObjectConverter::snakeCaseToCamelCase(substr($methodName, 2));
        } elseif (substr($methodName, 0, 3) === Subject::HAS_METHOD_PREFIX) {
            return SimpleDataObjectConverter::snakeCaseToCamelCase(substr($methodName, 3));
        } elseif (substr($methodName, 0, 3) === Subject::GETTER_PREFIX) {
            return SimpleDataObjectConverter::snakeCaseToCamelCase(substr($methodName, 3));
        }
    }
}

You will need to adjust your data object fields accordingly

interface ProductDataResponseInterface
{
    const STOCK_ITEM_ID = 'stockItemID';
    const STOCK_CATEGORIES = 'stockCategories';
//...etc
}

EDIT: This solution doesn't work because it's too broad. You would need to tailor this such that a different implementation of Reflection\FieldNamer is used only in the places where you need it to be.

@ghost ghost 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 Aug 18, 2020
@m2-community-project m2-community-project bot added Progress: PR Created Indicates that Pull Request has been created to fix issue and removed Progress: PR in progress labels Sep 3, 2020
@ghost ghost added Progress: PR in progress and removed Progress: PR Created Indicates that Pull Request has been created to fix issue labels Sep 11, 2020
@m2-community-project m2-community-project bot added Progress: PR Created Indicates that Pull Request has been created to fix issue and removed Progress: PR in progress labels Sep 24, 2020
@ghost ghost unassigned jayankaghosh Oct 28, 2020
@tuyennn
Copy link
Member

tuyennn commented Nov 20, 2020

@magento-engcom-team What is the current status of this ticket

@nikolasr200
Copy link

One can always hack the field namer. Works well enough, although I agree that an elegant solution would be preferred.

<!--di.xml-->
<type name="Magento\Framework\Reflection\FieldNamer">
    <plugin name="my_field_namer" type="Vendor\Project\Plugin\FieldNamer"/>
</type>
//Plugin/FieldNamer.php
use Magento\Framework\Api\SimpleDataObjectConverter;
use Magento\Framework\Reflection\FieldNamer as Subject;

class FieldNamer
{
    public function aroundGetFieldNameForMethodName(
        Subject $subject,
        callable $proceed,
        $methodName
    ) {
        if (substr($methodName, 0, 2) === Subject::IS_METHOD_PREFIX) {
            return SimpleDataObjectConverter::snakeCaseToCamelCase(substr($methodName, 2));
        } elseif (substr($methodName, 0, 3) === Subject::HAS_METHOD_PREFIX) {
            return SimpleDataObjectConverter::snakeCaseToCamelCase(substr($methodName, 3));
        } elseif (substr($methodName, 0, 3) === Subject::GETTER_PREFIX) {
            return SimpleDataObjectConverter::snakeCaseToCamelCase(substr($methodName, 3));
        }
    }
}

You will need to adjust your data object fields accordingly

interface ProductDataResponseInterface
{
    const STOCK_ITEM_ID = 'stockItemID';
    const STOCK_CATEGORIES = 'stockCategories';
//...etc
}

EDIT: This solution doesn't work because it's too broad. You would need to tailor this such that a different implementation of Reflection\FieldNamer is used only in the places where you need it to be.

I made some changes in the proposed solution, and worked for me!
One of the changes, was to use afterGetFieldNameForMethodName instead of around, and then iterate through a list of qualified methodNames to retrieve them the way that i needed.

@ilnytskyi
Copy link
Contributor

would be helpful to preseserve camel case in some cases

@tuyennn
Copy link
Member

tuyennn commented Oct 4, 2024

@magento I am working on this

tuyennn added a commit to tuyennn/magento2 that referenced this issue Oct 4, 2024
@tuyennn tuyennn linked a pull request Oct 4, 2024 that will close this issue
5 tasks
@engcom-Bravo engcom-Bravo added the Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it label Dec 23, 2024
@jakwinkler
Copy link

@tuyennn any progress ?

@tuyennn
Copy link
Member

tuyennn commented May 9, 2025

@tuyennn any progress ?

@jakwinkler you see the pull request, it was Priority: P4 and no one from Adobe is gonna review it, soon. lol

@ilnytskyi
Copy link
Contributor

@jakwinkler it looks like we can achieve similar results using the output processors.

Image

as introduced in this commit 89fb372

the only disadvantage is that current implementation allows only one processor per class.

But unless we want to modify core interfaces, custom ones should be safe.

Ultimately we can hack those outputs via plugins - for Magento\Framework\Reflection\DataObjectProcessor

It even looks like we can add custom attributes directly to the output with this approach.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Component: Framework/Reflection Component: Webapi Use with concrete module component label E.g. "Component: Webapi" + "Catalog" feature request Issue: Clear Description Gate 2 Passed. Manual verification of the issue description passed Issue: Confirmed Gate 3 Passed. Manual verification of the issue completed. Issue is confirmed Issue: Format is valid Gate 1 Passed. Automatic verification of issue format passed Issue: Ready for Work Gate 4. Acknowledged. Issue is added to backlog and ready for development Priority: P4 No current plan to fix. Fixing can be deferred as a logical part of more important work. Progress: PR in progress Reproduced on 2.3.x The issue has been reproduced on latest 2.3 release Severity: S4 Affects aesthetics, professional look and feel, “quality” or “usability”. Triage: Dev.Experience Issue related to Developer Experience and needs help with Triage to Confirm or Reject it
Projects
Status: Pull Request in Progress