Skip to content

Conversation

veewee
Copy link
Member

@veewee veewee commented Aug 29, 2025

Q A
Type improvement
BC Break no
Fixed issues phpro/soap-client#573

Summary

This PR adds better support for encoding and decoding xsi:type information.

This PR improves the handling of xsi:type information in SOAP encoding/decoding by better supporting type detection and encoding. The changes centralize xsi:type logic, introduce new encoders for better type handling, and add comprehensive test coverage.

  • Introduces new encoders (XsiTypeEncoder, MatchingValueEncoder, FixedIsoEncoder) for better xsi:type handling
  • Refactors xsi:type detection logic from ElementValueBuilder to XsiAttributeBuilder for better centralization
  • Adds comprehensive test cases to verify xsi:type encoding/decoding behavior

Object:

(object)[
    'responses' => [
        new ImpliedSchema015A('abc'),
        new ImpliedSchema015B('def', 'ghi'),
    ],
];

Transcodes to:

 <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
    xmlns:tns="http://test-uri/"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
    <SOAP-ENV:Body SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
        <tns:test>
          <testParam xsi:type="tns:return">
            <responses xsi:type="tns:A">
              <foo xsi:type="xsd:string">abc</foo>
            </responses>
            <responses xsi:type="tns:B">
              <foo xsi:type="xsd:string">def</foo>
              <bar xsi:type="xsd:string">ghi</bar>
            </responses>
          </testParam>
        </tns:test>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

For XSD:

    <complexType name="A">
        <sequence>
            <element name="foo" type="xsd:string" />
        </sequence>
    </complexType>
    <complexType name="B">
      <complexContent>
        <extension base="tns:A">
          <sequence>
            <element name="bar" type="xsd:string" />
          </sequence>
        </extension>
      </complexContent>
    </complexType>
    <element name="return">
        <complexType>
            <sequence>
                <element name="responses" type="tns:A" minOccurs="0" maxOccurs="unbounded" />
            </sequence>        
        </complexType>
    </element>

With encoder configuration:

EncoderRegistry::default()
    ->addClassMap('http://test-uri/', 'B', ImpliedSchema015B::class)
    ->addComplexTypeConverter(
        'http://test-uri/',
        'A',
        new Encoder\MatchingValueEncoder(
            encoderDetector: static fn (Encoder\Context $context, mixed $value): array =>
                $value instanceof ImpliedSchema015B
                    ? [
                        $context->withType($context->type->copy('B')->withXmlTypeName('B')),
                        new Encoder\ObjectEncoder(ImpliedSchema015B::class),
                    ]
                    : [$context],
            defaultEncoder: new Encoder\ObjectEncoder(ImpliedSchema015A::class)
        )
    );

@veewee veewee marked this pull request as draft August 29, 2025 11:14
@veewee veewee changed the title Respect xsi:type information better [WIP] Respect xsi:type information better Aug 29, 2025
@veewee veewee force-pushed the xsi-type-improvements branch 2 times, most recently from e518f39 to b5648d4 Compare August 29, 2025 12:48
@veewee veewee force-pushed the xsi-type-improvements branch from b5648d4 to 07e34a2 Compare August 29, 2025 13:00
@veewee veewee force-pushed the xsi-type-improvements branch 7 times, most recently from 722bca1 to cbe86e3 Compare September 26, 2025 08:19
@veewee veewee marked this pull request as ready for review September 26, 2025 08:19
@veewee veewee changed the title [WIP] Respect xsi:type information better Respect xsi:type information better Sep 26, 2025
@veewee veewee requested a review from Copilot September 26, 2025 08:19
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the handling of xsi:type information in SOAP encoding/decoding by better supporting type detection and encoding. The changes centralize xsi:type logic, introduce new encoders for better type handling, and add comprehensive test coverage.

  • Introduces new encoders (XsiTypeEncoder, MatchingValueEncoder, FixedIsoEncoder) for better xsi:type handling
  • Refactors xsi:type detection logic from ElementValueBuilder to XsiAttributeBuilder for better centralization
  • Adds comprehensive test cases to verify xsi:type encoding/decoding behavior

Reviewed Changes

Copilot reviewed 24 out of 24 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/PhpCompatibility/Implied/ImpliedSchema015Test.php New test for xsi:type handling with class-based objects
tests/PhpCompatibility/Implied/ImpliedSchema014Test.php New test for xsi:type handling with stdClass objects
tests/PhpCompatibility/Implied/ImpliedSchema012Test.php Updates expected XML to use correct xsi:type
tests/PhpCompatibility/Implied/ImpliedSchema011Test.php Updates expected XML to use correct xsi:type
tests/PhpCompatibility/Implied/ImpliedSchema010Test.php Updates expected XML to use correct xsi:type
tests/PhpCompatibility/Implied/ImpliedSchema009Test.php Updates expected XML to use correct xsi:type
tests/PhpCompatibility/AbstractCompatibilityTests.php Adds debug statement for testing
src/Xml/Writer/XsiAttributeBuilder.php Major refactor to centralize xsi:type logic
src/Xml/Writer/ElementValueBuilder.php Removes xsi:type logic and delegates to XsiAttributeBuilder
src/Xml/Reader/ElementValueReader.php Removes xsi:type detection logic
src/TypeInference/XsiTypeDetector.php Refactors to separate type detection from encoder detection
src/EncoderRegistry.php Removes OptionalElementEncoder wrapper and adds new helper methods
src/Encoder/XsiTypeEncoder.php New encoder specifically for handling xsi:type encoding/decoding
src/Encoder/SoapEnc/SoapObjectEncoder.php Updates to use centralized xsi:type logic
src/Encoder/SoapEnc/SoapArrayEncoder.php Updates to use centralized xsi:type logic
src/Encoder/SoapEnc/ApacheMapEncoder.php Updates to use centralized xsi:type logic
src/Encoder/SimpleType/EncoderDetector.php Integrates XsiTypeEncoder into simple type detection
src/Encoder/ObjectEncoder.php Updates to use centralized xsi:type logic
src/Encoder/MatchingValueEncoder.php New encoder for value-based encoder selection
src/Encoder/FixedIsoEncoder.php New encoder for fixed ISO transformations
src/Encoder/EncoderDetector.php Integrates XsiTypeEncoder into encoder detection
examples/encoders/simpleType/anyType-with-xsi-info.php Updates to use new centralized API
examples/encoders/complexType/matching-value.php New example demonstrating MatchingValueEncoder usage
composer.json Updates dependency version

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

@veewee veewee force-pushed the xsi-type-improvements branch from cbe86e3 to 3e44681 Compare September 26, 2025 08:23
@veewee veewee merged commit fdc265c into php-soap:main Sep 26, 2025
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant