Skip to content

Commit df2abc9

Browse files
authored
Escape interpolated PHP vars in descriptions (#55)
1 parent 395ef94 commit df2abc9

File tree

3 files changed

+4
-5
lines changed

3 files changed

+4
-5
lines changed

src/JsonSchema/SchemaBuilder.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ private function processOther()
396396
} elseif ($value instanceof \stdClass) {
397397
$export = '(object)' . PhpCode::varExport((array)$value);
398398
} elseif (is_string($value)) {
399-
$export = '"' . str_replace(array('\\', "\n", "\r", "\t", '"'), array('\\\\', '\n', '\r', '\t', '\"'), $value) . '"';
399+
$export = '"' . str_replace(array('\\', "\n", "\r", "\t", '"', '${', '{$'), array('\\\\', '\n', '\r', '\t', '\"', '\${', '{\$'), $value) . '"';
400400
} else {
401401
$export = PhpCode::varExport($value);
402402
}

src/Traits/Description.php

-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,6 @@
22

33
namespace Swaggest\PhpCodeBuilder\Traits;
44

5-
6-
use Swaggest\PhpCodeBuilder\PhpDoc;
7-
85
trait Description
96
{
107
use PhpDocable;

tests/src/PHPUnit/JsonSchema/TypeBuilderTest.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public function testRef()
2222
"definitions": {
2323
"header": {
2424
"type": "object",
25+
"description": "Something with an interpolated ${VALUE}",
2526
"properties": {
2627
"maximum": {"$ref": "#/definitions/maximum"}
2728
}
@@ -68,6 +69,7 @@ public static function setUpProperties($properties, Schema $ownerSchema)
6869
$properties->maximum = Schema::number();
6970
$properties->maximum->setFromRef('#/definitions/maximum');
7071
$ownerSchema->type = Schema::OBJECT;
72+
$ownerSchema->description = "Something with an interpolated \${VALUE}";
7173
$ownerSchema->setFromRef('#/definitions/header');
7274
}
7375
@@ -92,7 +94,7 @@ public function setMaximum($maximum)
9294

9395
eval(substr($file->render(), 6));
9496
$exported = Schema::export($className::schema());
95-
$this->assertSame('{"properties":{"maximum":{"$ref":"#/definitions/maximum"}},"type":"object","definitions":{"maximum":{"type":"number"}}}',
97+
$this->assertSame('{"description":"Something with an interpolated ${VALUE}","properties":{"maximum":{"$ref":"#/definitions/maximum"}},"type":"object","definitions":{"maximum":{"type":"number"}}}',
9698
json_encode($exported, JSON_UNESCAPED_SLASHES));
9799
}
98100

0 commit comments

Comments
 (0)