Skip to content

Commit fcefa57

Browse files
committed
split schemas derived from different origins and pointing to same $ref
1 parent 7c4cc6a commit fcefa57

File tree

4 files changed

+47
-0
lines changed

4 files changed

+47
-0
lines changed

src/Schema.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,9 @@ private function processObject($data, Context $options, $path, $result = null)
706706
try {
707707
$refResult = $this->process($data, $options, $path . '->$ref:' . $refString, $result);
708708
if ($refResult instanceof ObjectItemContract) {
709+
if ($refResult->getFromRefs()) {
710+
$refResult = clone $refResult; // @todo check performance, consider option
711+
}
709712
$refResult->setFromRef($refString);
710713
}
711714
$ref->setImported($refResult);
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace Swaggest\JsonSchema\Tests\Helper;
4+
5+
6+
use Swaggest\JsonSchema\Constraint\Properties;
7+
use Swaggest\JsonSchema\Schema;
8+
use Swaggest\JsonSchema\Structure\ClassStructure;
9+
10+
class DeepRefAnotherTitle extends ClassStructure
11+
{
12+
/**
13+
* @param Properties|static $properties
14+
* @param Schema $ownerSchema
15+
*/
16+
public static function setUpProperties($properties, Schema $ownerSchema)
17+
{
18+
$ownerSchema->setFromRef('http://json-schema.org/draft-04/schema#/properties/title');
19+
$ownerSchema->setFromRef('#/definitions/anotherTitle');
20+
}
21+
22+
}

tests/src/Helper/DeepRefRoot.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class DeepRefRoot extends ClassStructure
1313

1414
public $intermediateTitle;
1515

16+
public $anotherTitle;
17+
1618
public $prop;
1719

1820
/**
@@ -28,6 +30,8 @@ public static function setUpProperties($properties, Schema $ownerSchema)
2830

2931
$properties->intermediateTitle = DeepRefTitle::schema();
3032

33+
$properties->anotherTitle = DeepRefAnotherTitle::schema();
34+
3135
$ownerSchema->type = Schema::STRING;
3236
}
3337
}

tests/src/PHPUnit/ClassStructure/ExportSchemaTest.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public function testDeepRef()
5151
},
5252
"intermediateTitle": {
5353
"$ref": "#/definitions/title"
54+
},
55+
"anotherTitle": {
56+
"$ref": "#/definitions/anotherTitle"
5457
}
5558
},
5659
"type": "string",
@@ -69,6 +72,9 @@ public function testDeepRef()
6972
},
7073
"title": {
7174
"$ref": "http://json-schema.org/draft-04/schema#/properties/title"
75+
},
76+
"anotherTitle": {
77+
"$ref": "http://json-schema.org/draft-04/schema#/properties/title"
7278
}
7379
}
7480
}
@@ -95,11 +101,23 @@ public function testDeepRefSchema()
95101
},
96102
"lvl4": {
97103
"type": "integer"
104+
},
105+
"title": {
106+
"$ref": "http://json-schema.org/draft-04/schema#/properties/title"
107+
},
108+
"anotherTitle": {
109+
"$ref": "http://json-schema.org/draft-04/schema#/properties/title"
98110
}
99111
},
100112
"properties": {
101113
"prop": {
102114
"$ref": "#/definitions/lvl1"
115+
},
116+
"intermediateTitle": {
117+
"$ref": "#/definitions/title"
118+
},
119+
"anotherTitle": {
120+
"$ref": "#/definitions/anotherTitle"
103121
}
104122
},
105123
"type": "object"

0 commit comments

Comments
 (0)