WsdlToPhp / PackageGenerator

Generates a PHP SDK based on a WSDL, simple and powerful, WSDL to PHP
https://providr.io
MIT License
422 stars 73 forks source link

Validation for constraint: fractionDigits fails for integer numbers (0 fraction digits) #210

Closed wilkolazki closed 4 years ago

wilkolazki commented 4 years ago

Hi,

I am trying to implement a soap api which uses only decimal numbers. The generated code is unusable because of following validation:

public function setWeight($weight = null)
{
     /* ..... */
       // validation for constraint: fractionDigits
        if (!is_null($weight) && mb_strlen(mb_substr($weight, mb_strpos($weight, '.') + 1)) > 0) {
            throw new \InvalidArgumentException(sprintf('Invalid value %s, the value must at most contain 0 fraction digits, %d given', var_export($weight, true), mb_strlen(mb_substr($weight, mb_strpos($weight, '.') + 1))), __LINE__);
        }

For any integer number mb_strpos($weight, '.') returns false. When you add a 1 to this it becomes 1. Whole check mb_strlen(mb_substr($weight, mb_strpos($weight, '.') + 1)) will be greater than 0 for any number greater or equal to 10.

For example, when I try to set the weight to 200 I get the following message:

InvalidArgumentException : Invalid value 200, the value must at most contain 0 fraction digits, 2 given

The WSDL fragment this code is base on is as follows:

<xs:simpleType name="Weight">
  <xs:restriction base="xs:decimal">
    <xs:fractionDigits value="0"/>
    <xs:minExclusive value="0"/>
    <xs:maxInclusive value="2500000"/>
  </xs:restriction>
</xs:simpleType>

The full wsdl can be found at here. The WSDL is publicly available, but the service requires a contract.

I hope this description helps fix the issue.

Is there any way to disable the generation of this checks until the issue is fixed?

mikaelcom commented 4 years ago

Validation rules can be not generated, look to https://github.com/WsdlToPhp/PackageGenerator/wiki/Options#validation

mikaelcom commented 4 years ago

Fix testings in progress, feel free to comment the fix at https://github.com/WsdlToPhp/PackageGenerator/commit/a79d7a6e7e0c239781ef55996b7cbe8078f1428d#diff-36d7df7d6013e2e7ba6be1f26ff0d720

mikaelcom commented 4 years ago

You can test fix with corresponding phars from:

Let me know how it goes so I'll publish a new release

wilkolazki commented 4 years ago

I have checked out the branch feature/issue-210 and tested it on my case. The generated validators work properly.

Some weird thing is that the code differs in two places. Besides validators, each generated service is now accessing getSoapClient staticly with self:: and not via $this->. I assume this might be result of some other change already in master, but not in my previous composer installed version. For now I have ignored this change and not commited it to my package.

Thank you for super fast reaction. That was very impressive.

Great work! Szymon Wilkołazki

mikaelcom commented 4 years ago

The feature/issue-210 is based on the 2.x branch which is the initial bahaviour that evolved in the develop branch (3.x tags), that's all :wink:

So if, appart from the fix, the only other changes are the usage of the getSoapClient (statically instead of the inner instance), I'll release it so you'll be able to use the 3.x release that'll generate the code with the fix and the $this->getSoapClient() call.

wilkolazki commented 4 years ago

Sorry for not responding in time. Yes, the validators and the self/$this were the only changes in the whole generated source tree in my project. I think it should be safe to release.

Thanks again for your work.