Skip to content

Commit baec6b8

Browse files
committed
Normalize namespace, starts always with / and ends without / - #12
1 parent 20fa704 commit baec6b8

2 files changed

Lines changed: 13 additions & 9 deletions

File tree

src/Shorthand/Shorthand.php

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,8 @@ private static function convertShorthandStringToJsonSchema(string $shorthandStr,
131131
$namespace = $customData['voNamespace'] ?? '';
132132
$typeNamespaceDetected = false !== \strpos($type, '/');
133133

134-
if ($namespace !== '') {
135-
$namespace = \rtrim($namespace, '/');
134+
if ($namespace !== '' && $namespace !== '/') {
135+
$namespace = '/' . \trim($namespace, '/');
136136
}
137137

138138
// it's an array, the collection / list class is created implicitly
@@ -154,7 +154,10 @@ private static function convertShorthandStringToJsonSchema(string $shorthandStr,
154154

155155
if ($typeNamespaceDetected) {
156156
$namespace = self::extractNamespace($type);
157-
$namespace = $namespace === '' ? '/' : $namespace;
157+
158+
if ($namespace !== '' && $namespace !== '/') {
159+
$namespace = '/' . \trim($namespace, '/');
160+
}
158161

159162
$type = self::extractType($type);
160163
}
@@ -194,7 +197,7 @@ private static function convertShorthandStringToJsonSchema(string $shorthandStr,
194197

195198
if ($namespace) {
196199
$schema['namespace'] = $namespace;
197-
$schema['$ref'] = '#/definitions/' . \ltrim($namespace, '/') . '/' . $type;
200+
$schema['$ref'] = '#/definitions' . \rtrim($namespace, '/') . '/' . $type;
198201
}
199202

200203
return $schema;
@@ -265,6 +268,6 @@ private static function extractType(string $type): string
265268

266269
private static function extractNamespace(string $type): string
267270
{
268-
return \substr($type, 0, \strrpos($type, '/'));
271+
return \substr($type, 0, \strrpos($type, '/') + 1);
269272
}
270273
}

tests/Type/ObjectTypeTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ public function it_supports_definition_of_objects_shorthand_type_ns(): void
186186
/** @var ReferenceType $billingAddress */
187187
$billingAddress = $properties['billing_address']->first();
188188
$this->assertInstanceOf(ReferenceType::class, $billingAddress);
189-
$this->assertSame(['namespace' => 'Order'], $billingAddress->custom());
189+
$this->assertSame(['namespace' => '/Order'], $billingAddress->custom());
190190

191191
/** @var ArrayType $shippingAddress */
192192
$shippingAddress = $properties['shipping_address']->first();
@@ -196,7 +196,8 @@ public function it_supports_definition_of_objects_shorthand_type_ns(): void
196196
/** @var ReferenceType $address */
197197
$address = $shippingAddress->items()[0]->first();
198198
$this->assertInstanceOf(ReferenceType::class, $address);
199-
$this->assertSame(['namespace' => 'Order'], $address->custom());
199+
$this->assertSame(['namespace' => '/Order'], $address->custom());
200+
$this->assertSame('#/definitions/Order/Address', $address->ref());
200201
}
201202

202203
/**
@@ -383,12 +384,12 @@ public function it_supports_definition_of_objects_shorthand_nested_ns(): void
383384
$paymentAddress = $subProperties['payment_address']->first();
384385
$this->assertInstanceOf(StringType::class, $paymentAddress);
385386
// value object defines it's own namespace
386-
$this->assertSame(['namespace' => 'Payment'], $paymentAddress->custom());
387+
$this->assertSame(['namespace' => '/Payment'], $paymentAddress->custom());
387388

388389
/** @var ReferenceType $billingAddress */
389390
$billingAddress = $subProperties['billing_address']->first();
390391
$this->assertInstanceOf(ReferenceType::class, $billingAddress);
391-
// reference value object use defined voNamespace because of missing namespace definition
392+
// reference value object uses defined voNamespace because of missing namespace definition
392393
$this->assertSame(['namespace' => '/Shipping'], $billingAddress->custom());
393394
$this->assertSame('#/definitions/Shipping/Address', $billingAddress->ref());
394395

0 commit comments

Comments
 (0)