Skip to content

Commit b25d608

Browse files
committed
Use the new naming logic for specifying the xml type name as well so that the encoder doesnt get confused
1 parent 77a2550 commit b25d608

File tree

4 files changed

+49
-42
lines changed

4 files changed

+49
-42
lines changed

src/Metadata/Converter/Types/Configurator/XmlTypeInfoConfigurator.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33

44
namespace Soap\WsdlReader\Metadata\Converter\Types\Configurator;
55

6+
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem;
7+
use GoetasWebservices\XML\XSDReader\Schema\Element\ElementItem;
68
use GoetasWebservices\XML\XSDReader\Schema\Item;
79
use GoetasWebservices\XML\XSDReader\Schema\SchemaItem;
810
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
911
use Soap\Engine\Metadata\Model\XsdType as EngineType;
12+
use Soap\WsdlReader\Metadata\Converter\Types\Detector\AttributeTypeNameDetector;
13+
use Soap\WsdlReader\Metadata\Converter\Types\Detector\ElementTypeNameDetector;
1014
use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext;
1115

1216
final class XmlTypeInfoConfigurator
@@ -25,9 +29,16 @@ public function __invoke(EngineType $engineType, mixed $xsdType, TypesConverterC
2529
$targetNamespace = $xsdType->getSchema()->getTargetNamespace() ?? '';
2630
$typeNamespace = $type?->getSchema()->getTargetNamespace() ?: $targetNamespace;
2731

32+
$parentContext = $context->parent()->unwrapOr(null);
33+
$xmlTypeName = match(true) {
34+
$parentContext && $item instanceof ElementItem => (new ElementTypeNameDetector())($item, $parentContext),
35+
$parentContext && $item instanceof AttributeItem => (new AttributeTypeNameDetector())($item, $parentContext),
36+
default => $typeName,
37+
};
38+
2839
return $engineType
2940
->withXmlTargetNodeName($itemName ?: $typeName)
30-
->withXmlTypeName($typeName ?: $itemName ?: '')
41+
->withXmlTypeName($xmlTypeName)
3142
->withXmlNamespace($typeNamespace)
3243
->withXmlNamespaceName(
3344
$context->knownNamespaces->lookupNameFromNamespace($typeNamespace)->unwrapOr(

src/Metadata/Converter/Types/Detector/AttributeDeclaringParentTypeDetector.php

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use GoetasWebservices\XML\XSDReader\Schema\SchemaItem;
99
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
1010
use Psl\Option\Option;
11-
use Soap\WsdlReader\Metadata\Converter\Types\ParentContext;
1211
use function Psl\Option\none;
1312
use function Psl\Option\some;
1413

@@ -50,18 +49,4 @@ public function __invoke(AttributeItem $item, ?SchemaItem $parent): Option
5049

5150
return none();
5251
}
53-
54-
55-
/**
56-
* @param Option<ParentContext> $parentContext
57-
* @return Option<Type>
58-
*/
59-
public static function detectWithParentContext(AttributeItem $item, Option $parentContext): Option
60-
{
61-
/** @var self $calculate */
62-
static $calculate = new self();
63-
64-
return $parentContext
65-
->andThen(static fn (ParentContext $context) => $calculate($item, $context->currentParent()));
66-
}
6752
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php declare(strict_types=1);
2+
3+
namespace Soap\WsdlReader\Metadata\Converter\Types\Detector;
4+
5+
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem;
6+
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeSingle;
7+
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
8+
use Psl\Option\Option;
9+
use Soap\WsdlReader\Metadata\Converter\Types\ParentContext;
10+
use function Psl\Option\from_nullable;
11+
12+
final class AttributeTypeNameDetector
13+
{
14+
public function __invoke(AttributeItem $attribute, ParentContext $parentContext): string
15+
{
16+
$attributeType = $attribute instanceof AttributeSingle ? $attribute->getType() : null;
17+
$attributeRestriction = $attributeType?->getRestriction();
18+
$attributeTypeName = $attributeType?->getName();
19+
$attributeRestrictionName = ($attributeRestriction && !$attributeRestriction->getChecks()) ? $attributeRestriction->getBase()?->getName() : null;
20+
21+
$typeName = $attributeTypeName ?: ($attributeRestrictionName ?: $attribute->getName());
22+
23+
// If a name cannot be determined from the type, we fallback to the attribute name:
24+
// Prefix the attribute name with the parent element name resulting in a more unique type-name.
25+
if (!$attributeTypeName && !$attributeRestrictionName) {
26+
$typeName = (new AttributeDeclaringParentTypeDetector())($attribute, $parentContext->currentParent())
27+
->andThen(static fn (Type $parent): Option => from_nullable($parent->getName()))
28+
->map(static fn (string $parentName): string => $parentName . ucfirst($typeName))
29+
->unwrapOr($typeName);
30+
}
31+
32+
return $typeName;
33+
}
34+
}

src/Metadata/Converter/Types/Visitor/AttributeContainerVisitor.php

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,16 @@
55

66
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeContainer;
77
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeItem;
8-
use GoetasWebservices\XML\XSDReader\Schema\Attribute\AttributeSingle;
98
use GoetasWebservices\XML\XSDReader\Schema\Attribute\Group;
109
use GoetasWebservices\XML\XSDReader\Schema\Type\Type;
11-
use Psl\Option\Option;
1210
use Soap\Engine\Metadata\Collection\PropertyCollection;
1311
use Soap\Engine\Metadata\Model\Property;
1412
use Soap\Engine\Metadata\Model\TypeMeta;
1513
use Soap\Engine\Metadata\Model\XsdType as EngineType;
1614
use Soap\WsdlReader\Metadata\Converter\Types\Configurator;
17-
use Soap\WsdlReader\Metadata\Converter\Types\Detector\AttributeDeclaringParentTypeDetector;
15+
use Soap\WsdlReader\Metadata\Converter\Types\Detector\AttributeTypeNameDetector;
1816
use Soap\WsdlReader\Metadata\Converter\Types\TypesConverterContext;
1917
use function Psl\Fun\pipe;
20-
use function Psl\Option\from_nullable;
2118
use function Psl\Result\wrap;
2219
use function Psl\Type\instance_of;
2320
use function Psl\Vec\flat_map;
@@ -91,35 +88,15 @@ private function parseAttribute(AttributeItem $attribute, TypesConverterContext
9188
return $this->parseAttributes($attribute, $context);
9289
}
9390

94-
// Detecting the type-name for an attribute is complex.
95-
// We first try to use the type name,
96-
// Next up is the base type of the restriction if there aren't any restriction checks configured.
97-
// Finally there is a fallback to the attribute name
98-
$attributeType = $attribute instanceof AttributeSingle ? $attribute->getType() : null;
99-
$attributeRestriction = $attributeType?->getRestriction();
100-
$attributeTypeName = $attributeType?->getName();
101-
$attributeRestrictionName = ($attributeRestriction && !$attributeRestriction->getChecks()) ? $attributeRestriction->getBase()?->getName() : null;
102-
103-
$typeName = $attributeTypeName ?: ($attributeRestrictionName ?: $attribute->getName());
104-
$engineType = EngineType::guess($typeName);
105-
106-
// If a name cannot be determined from the type, we fallback to the attribute name:
107-
// Prefix the attribute name with the parent element name resulting in a more unique type-name.
108-
if (!$attributeTypeName && !$attributeRestrictionName) {
109-
$engineType = AttributeDeclaringParentTypeDetector::detectWithParentContext($attribute, $context->parent())
110-
->andThen(static fn (Type $parent): Option => from_nullable($parent->getName()))
111-
->map(static fn (string $parentName): EngineType => $engineType->copy($parentName . ucfirst($typeName)))
112-
->unwrapOr($engineType);
113-
}
114-
91+
$typeName = (new AttributeTypeNameDetector())($attribute, $context->parent()->unwrap());
11592
$configure = pipe(
11693
static fn (EngineType $engineType): EngineType => (new Configurator\AttributeConfigurator())($engineType, $attribute, $context),
11794
);
11895

11996
return new PropertyCollection(
12097
new Property(
12198
$attribute->getName(),
122-
$configure($engineType)
99+
$configure(EngineType::guess($typeName))
123100
)
124101
);
125102
}

0 commit comments

Comments
 (0)