Skip to content

Commit

Permalink
Escape interpolated PHP vars in descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop committed Dec 18, 2023
1 parent 395ef94 commit 83dc3e7
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/JsonSchema/SchemaBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private function processOther()
} elseif ($value instanceof \stdClass) {
$export = '(object)' . PhpCode::varExport((array)$value);
} elseif (is_string($value)) {
$export = '"' . str_replace(array('\\', "\n", "\r", "\t", '"'), array('\\\\', '\n', '\r', '\t', '\"'), $value) . '"';
$export = '"' . str_replace(array('\\', "\n", "\r", "\t", '"', '${', '{$'), array('\\\\', '\n', '\r', '\t', '\"', '\${', '{\$'), $value) . '"';
} else {
$export = PhpCode::varExport($value);
}
Expand Down
3 changes: 0 additions & 3 deletions src/Traits/Description.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

namespace Swaggest\PhpCodeBuilder\Traits;


use Swaggest\PhpCodeBuilder\PhpDoc;

trait Description
{
use PhpDocable;
Expand Down
4 changes: 3 additions & 1 deletion tests/src/PHPUnit/JsonSchema/TypeBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public function testRef()
"definitions": {
"header": {
"type": "object",
"description": "Something with an interpolated ${VALUE}",
"properties": {
"maximum": {"$ref": "#/definitions/maximum"}
}
Expand Down Expand Up @@ -68,6 +69,7 @@ public static function setUpProperties($properties, Schema $ownerSchema)
$properties->maximum = Schema::number();
$properties->maximum->setFromRef('#/definitions/maximum');
$ownerSchema->type = Schema::OBJECT;
$ownerSchema->description = "Something with an interpolated \${VALUE}";
$ownerSchema->setFromRef('#/definitions/header');
}
Expand All @@ -92,7 +94,7 @@ public function setMaximum($maximum)

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

Expand Down

0 comments on commit 83dc3e7

Please sign in to comment.