Skip to content

Commit 96b772a

Browse files
authored
Merge pull request #60 from mrbig/issue-59
Fix for json fields containing a dollar sign (Issue #59)
2 parents f221ec5 + 5915431 commit 96b772a

File tree

3 files changed

+110
-1
lines changed

3 files changed

+110
-1
lines changed

src/JsonSchema/SchemaBuilder.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,15 @@ 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+
switch ($key) {
400+
case 'pattern':
401+
$export = '"' . str_replace(array('\\', "\n", "\r", "\t", '"', '${', '{$'), array('\\\\', '\n', '\r', '\t', '\"', '\${', '{\$'), $value) . '"';
402+
break;
403+
default:
404+
$export = '"' . str_replace(array('\\', "\n", "\r", "\t", '"', '$'), array('\\\\', '\n', '\r', '\t', '\"', '\$'), $value) . '"';
405+
break;
406+
}
407+
400408
} else {
401409
$export = PhpCode::varExport($value);
402410
}
+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
<?php
2+
3+
namespace Swaggest\PhpCodeBuilder\Tests\PHPUnit\Issues;
4+
5+
use Swaggest\JsonSchema\Exception\ConstException;
6+
use Swaggest\JsonSchema\Exception\ObjectException;
7+
use Swaggest\JsonSchema\Schema;
8+
use Swaggest\PhpCodeBuilder\App\PhpApp;
9+
use Swaggest\PhpCodeBuilder\JsonSchema\ClassHookCallback;
10+
use Swaggest\PhpCodeBuilder\JsonSchema\PhpBuilder;
11+
use Swaggest\PhpCodeBuilder\PhpClass;
12+
use Swaggest\PhpCodeBuilder\Tests\Tmp\Issue59\Sample;
13+
14+
/**
15+
* @see https://github.com/swaggest/php-code-builder/issues/59
16+
*/
17+
class Issue59Test extends \PHPUnit_Framework_TestCase
18+
{
19+
function testIssue59()
20+
{
21+
$schemaJson = <<<'JSON'
22+
{
23+
"type": "object",
24+
"description": "Description with $dollar sign",
25+
"properties": {
26+
"foo": {
27+
"type": "string"
28+
}
29+
}
30+
}
31+
JSON;
32+
33+
$appPath = realpath(__DIR__ . '/../../Tmp') . '/Issue59';
34+
$appNs = 'Swaggest\PhpCodeBuilder\Tests\Tmp\\Issue59';
35+
36+
$app = new PhpApp();
37+
$app->setNamespaceRoot($appNs, '.');
38+
39+
$schema = Schema::import(json_decode($schemaJson));
40+
$builder = new PhpBuilder();
41+
$builder->buildSetters = false;
42+
$builder->makeEnumConstants = true;
43+
$builder->skipSchemaDescriptions = false;
44+
45+
$builder->classCreatedHook = new ClassHookCallback(
46+
function (PhpClass $class, $path, $schema) use ($app, $appNs) {
47+
$class->setNamespace($appNs);
48+
if ('#' === $path) {
49+
$class->setName('Sample'); // Class name for root schema
50+
}
51+
$app->addClass($class);
52+
}
53+
);
54+
55+
56+
$builder->getType($schema);
57+
58+
$app->clearOldFiles($appPath);
59+
$app->store($appPath);
60+
61+
exec('git diff ' . $appPath, $out);
62+
$out = implode("\n", $out);
63+
$this->assertSame('', $out, "Generated files changed");
64+
}
65+
66+
67+
function testGeneratedValid()
68+
{
69+
Sample::import((object)array('foo' => 'abc'));
70+
}
71+
72+
}

tests/src/Tmp/Issue59/Sample.php

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* @file ATTENTION!!! The code below was carefully crafted by a mean machine.
4+
* Please consider to NOT put any emotional human-generated modifications as the splendid AI will throw them away with no mercy.
5+
*/
6+
7+
namespace Swaggest\PhpCodeBuilder\Tests\Tmp\Issue59;
8+
9+
use Swaggest\JsonSchema\Constraint\Properties;
10+
use Swaggest\JsonSchema\Schema;
11+
use Swaggest\JsonSchema\Structure\ClassStructure;
12+
13+
14+
class Sample extends ClassStructure
15+
{
16+
/** @var string */
17+
public $foo;
18+
19+
/**
20+
* @param Properties|static $properties
21+
* @param Schema $ownerSchema
22+
*/
23+
public static function setUpProperties($properties, Schema $ownerSchema)
24+
{
25+
$properties->foo = Schema::string();
26+
$ownerSchema->type = Schema::OBJECT;
27+
$ownerSchema->description = "Description with \$dollar sign";
28+
}
29+
}

0 commit comments

Comments
 (0)