4

When creating a SOAP client for example in Visual Studio, currently there is a Problem, that objects cannot be deserialized when they contain scalar types with a null value.

Using a programming language with strict types, float, int, etc.. are not allowed to be null, instead should be casted to 0.0 or 0.

In the SOAP Api, all values are type casted except when they are null: https://github.com/magento/magento2/blob/develop/lib/internal/Magento/Framework/Reflection/TypeCaster.php#L25

Is there any reason for this?

Magento Issue: https://github.com/magento/magento2/issues/3605

1
  • PHP is not a strongly typed language. You may request this be implemented for compatibility in Magento 2 via GitHub
    – scrowler
    Jun 22, 2016 at 17:20

1 Answer 1

1

I don't know the reason for this to be honest.

A hacky fix would be to create a plugin:

app/code/Vendor/Module/etc/di.xml

<config>
    <type name="Magento\Framework\Reflection\TypeCaster">
        <plugin name="module_cast_plugin" type="Vendor\Module\Plugin\CastFix" sortOrder="1"/>
    </type>
</config>

app/code/Vendor/Module/Plugin/CastFix.php

<?php

namespace Vendor\Module\Plugin;

use Magento\Framework\Reflection\TypeCaster;

class CastFix
{
    public function afterCastValueToType(TypeCaster $subject, $output)
    {
        if ($output === null) {
            return 0;
        } else {
            return $output;
        }
    }
}

Please note that I don't know what impact such plugin will have on the Magento software, if you want to use it, make a lot of tests and use it with care.

6
  • this would also return 0 when a field is null and of type string ;) I think I will create a little open source module for this. You can build an around plugin and then do the same type casting as in the original method but only if the value is null. I hope they will change this in the original class as this seems to be a bug. Jun 22, 2016 at 15:27
  • @DavidVerholen totally right I've fixed the code but yeah again, be careful with that as I'm not sure the impact it would have Jun 22, 2016 at 15:29
  • the typecast is executed explicitly in the soap api (did not look in the rest api yet). So it only has impact on the results from the api. But you are right. When you receive an order over the api there re currently several empty price fields. With this fix, they are 0 then. This could possibly cause Problems depending on the implementation of the Systems accessing the api. Although I do not find a definition reight now, I'm pretty sure that the float type used in the wsdl does not allow null values. Jun 22, 2016 at 15:40
  • @DavidVerholen you're dealing with a tricky issue here that could potentially affect a lot of code. Creating an issue was a good thing, I hope Magento dev team will work on that ASAP as it really sounds like a bug to me too Jun 22, 2016 at 15:42
  • 1
    to only change the behaviour for the soap api, it should be possible to create a plugin on a vritual type created for the TypeCaster. Then create a virtual type for the dataobjeprocessor and inject the virtual typecaster into it. github.com/magento/magento2/blob/develop/lib/internal/Magento/… and last but not least inject the virtual DataObjectProcessor into the Soap Handler github.com/magento/magento2/blob/develop/app/code/Magento/…. This way only the soap api should be affected Jun 22, 2016 at 15:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.