Skip to content
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

Incorrect handling of union types #260

Open
smx-smx opened this issue Jan 18, 2022 · 6 comments
Open

Incorrect handling of union types #260

smx-smx opened this issue Jan 18, 2022 · 6 comments
Assignees
Labels

Comments

@smx-smx
Copy link

smx-smx commented Jan 18, 2022

Describe the bug
An incorrect package is generated when having 2 different requests with the same field name but different type.
This is the relevant WSDL snippet (redacted)

What happens is that a single Items class is generated for both RequestA and RequestB, having a field of type string[] (probably the last type encountered in the WSDL).
This makes it impossible to call RequestA due to the wrong type bindings.

<!-- request A -->
<xs:complexType name="RequestA">
	<xs:sequence>
		<!-- Here "Items" is a struct array -->
		<xs:element name="Items" type="ns2:Items"/>
	</xs:sequence>
</xs:complexType>

<!-- request B -->
<xs:complexType name="RequestB">
	<xs:sequence>
		<!-- Here "Items" is a string array -->
		<xs:element name="Items">
			<xs:complexType>
				<xs:sequence>
					<xs:element name="Item" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>
				</xs:sequence>
			</xs:complexType>
		</xs:element>
	</xs:sequence>
</xs:complexType>

<!-- struct typedef -->
<xs:complexType name="Item">
	<xs:sequence>
		<xs:element name="A" type="xs:string"/>
		<xs:element name="B" type="xs:int"/>
		<xs:element name="C" type="xs:string"/>
		<xs:element name="D" type="xs:string"/>
	</xs:sequence>
</xs:complexType>

<!-- Item[] typedef -->
<xs:complexType name="Items">
	<xs:sequence>
		<xs:element name="Item" type="ns2:Item" maxOccurs="unbounded"/>
	</xs:sequence>
</xs:complexType>

To Reproduce
Steps to reproduce the behavior:

  • During the package generation? no/yes (package generates without errors, but incorrectly)
  • During the usage of the generated package? yes (validation error when adding Item[], string[] expected)

Expected behavior
Generate 2 different classes for the union type, one with a string[] field and another with Item[]

Additional context
Using wsdltophp/packagegenerator @ 5c8ee49

@mikaelcom
Copy link
Member

It's seems to me it's similar to #247, thus it can be hard to handle especially if it concerns the response object as PHP wouldn't be able to differentiate the two structs, this is why the generator creates only one class.

In addition, it's based on the Structs returned by the SoapClient class so this is also the possible origin of the generated class.

I can take a further look to it if you provide me the WSDL at [email protected].

@smx-smx
Copy link
Author

smx-smx commented Jan 22, 2022

I worked around this issue by making a callable function and using it like this:

$setItems = function($items){
  $this->Items = $items;
};
$setItems->call($reqA, $items);

So that I can set the field directly and skip the setter (with its type check), and it works.
Therefore I was wondering, what if we keep a single class per-item with opaque fields, but have different "models" for the conflicting types? This would let us work around the single-class limitation you talked about.

We could have a 1 to many mapping between the item class and multiple representations (models) for its fields.

As for sending the WSDL, I'm afraid I'd have to create a simplified and abstracted test case first...

@mikaelcom
Copy link
Member

Or you disable the validation rules on the generation. In your case it's working because the property is an array of string or of Items. But if the proprty was either a string of an Item, your anonymous function would fail, am I wrong?

@smx-smx
Copy link
Author

smx-smx commented Jan 29, 2022

Not if the fields are only type hinted, or in other words of "mixed" type (and therefore not checked by PHP upon assignment).
If the "root" model only has untyped fields, multiple strongly typed models can then be "derived" from it

@mikaelcom
Copy link
Member

But if you pass to the SoapClient a derived model instance containing the typed properties, then it would generated a bad XML request because the derived modules would not be mapped correctly for the XML request, or I misunderstood what you're saying.

Can you give an example?

@mikaelcom
Copy link
Member

Possibly related to #268

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants