Skip to content

Commit 7c4cc6a

Browse files
committed
keep intermediate references in path
1 parent 4ac7f41 commit 7c4cc6a

File tree

5 files changed

+65
-17
lines changed

5 files changed

+65
-17
lines changed

src/Context.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@ class Context extends MagicMap
4848
public $version = Schema::VERSION_AUTO;
4949

5050
public $exportedDefinitions = [];
51+
52+
public $isRef = false;
53+
5154
/**
5255
* @param boolean $skipValidation
5356
* @return Context

src/Schema.php

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1034,26 +1034,31 @@ public function process($data, Context $options, $path = '#', $result = null)
10341034
});
10351035
}
10361036

1037-
if ('#' !== $path && $refs = $data->getFromRefs()) {
1038-
$ref = $refs[0];
1039-
if (!array_key_exists($ref, $options->exportedDefinitions)) {
1040-
$exported = null;
1041-
$options->exportedDefinitions[$ref] = &$exported;
1042-
$exported = $this->process($data, $options /*, $ref*/);
1043-
unset($exported);
1044-
}
1037+
if ($options->isRef) {
1038+
$options->isRef = false;
1039+
} else {
1040+
if ('#' !== $path && $refs = $data->getFromRefs()) {
1041+
$ref = $refs[0];
1042+
if (!array_key_exists($ref, $options->exportedDefinitions) && strpos($ref, '://') === false) {
1043+
$exported = null;
1044+
$options->exportedDefinitions[$ref] = &$exported;
1045+
$options->isRef = true;
1046+
$exported = $this->process($data, $options, $ref);
1047+
unset($exported);
1048+
}
10451049

1046-
for ($i = 1; $i < count($refs); $i++) {
1047-
$ref = $refs[$i];
1048-
if (!array_key_exists($ref, $options->exportedDefinitions)) {
1049-
$exported = new \stdClass();
1050-
$exported->{self::PROP_REF} = $refs[$i-1];
1051-
$options->exportedDefinitions[$ref] = $exported;
1050+
for ($i = 1; $i < count($refs); $i++) {
1051+
$ref = $refs[$i];
1052+
if (!array_key_exists($ref, $options->exportedDefinitions) && strpos($ref, '://') === false) {
1053+
$exported = new \stdClass();
1054+
$exported->{self::PROP_REF} = $refs[$i - 1];
1055+
$options->exportedDefinitions[$ref] = $exported;
1056+
}
10521057
}
1053-
}
10541058

1055-
$result->{self::PROP_REF} = $refs[count($refs) - 1];
1056-
return $result;
1059+
$result->{self::PROP_REF} = $refs[count($refs) - 1];
1060+
return $result;
1061+
}
10571062
}
10581063

10591064
if ($options->circularReferences->contains($data)) {

tests/src/Helper/DeepRefRoot.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99

1010
class DeepRefRoot extends ClassStructure
1111
{
12+
public $directTitle;
13+
14+
public $intermediateTitle;
15+
1216
public $prop;
1317

1418
/**
@@ -18,6 +22,12 @@ class DeepRefRoot extends ClassStructure
1822
public static function setUpProperties($properties, Schema $ownerSchema)
1923
{
2024
$properties->prop = DeepRefProperty::schema();
25+
26+
$properties->directTitle = new Schema();
27+
$properties->directTitle->ref = 'http://json-schema.org/draft-04/schema#/properties/title';
28+
29+
$properties->intermediateTitle = DeepRefTitle::schema();
30+
2131
$ownerSchema->type = Schema::STRING;
2232
}
2333
}

tests/src/Helper/DeepRefTitle.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
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 DeepRefTitle 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/title');
20+
}
21+
}

tests/src/PHPUnit/ClassStructure/ExportSchemaTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public function testDeepRef()
4545
"properties": {
4646
"prop": {
4747
"$ref": "#/definitions/lvlD"
48+
},
49+
"directTitle": {
50+
"$ref": "http://json-schema.org/draft-04/schema#/properties/title"
51+
},
52+
"intermediateTitle": {
53+
"$ref": "#/definitions/title"
4854
}
4955
},
5056
"type": "string",
@@ -60,6 +66,9 @@ public function testDeepRef()
6066
},
6167
"lvlD": {
6268
"$ref": "#/definitions/lvlC"
69+
},
70+
"title": {
71+
"$ref": "http://json-schema.org/draft-04/schema#/properties/title"
6372
}
6473
}
6574
}

0 commit comments

Comments
 (0)