-
Notifications
You must be signed in to change notification settings - Fork 9.4k
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
Comments
Hi @craigcarnell. Thank you for your report.
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:
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?
|
Hi @engcom-Charlie. Thank you for working on this issue.
|
✅ Confirmed by @engcom-Charlie 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. |
I am working on this |
Hi @jayankaghosh. Thank you for working on this issue.
|
@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? |
@jayankaghosh Whatever works is good with me 👍 |
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 |
@magento-engcom-team What is the current status of this ticket |
I made some changes in the proposed solution, and worked for me! |
would be helpful to preseserve camel case in some cases |
@magento I am working on this |
@tuyennn any progress ? |
@jakwinkler you see the pull request, it was Priority: P4 and no one from Adobe is gonna review it, soon. lol |
@jakwinkler it looks like we can achieve similar results using the output processors. 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 It even looks like we can add custom attributes directly to the output with this approach. |
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 (*)
webapi.xml:
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
The text was updated successfully, but these errors were encountered: