Skip to content

Commit b602663

Browse files
authored
Fix invalid escaping of single quot, fix type naming with schema title (#37)
1 parent 0eb5c3b commit b602663

File tree

5 files changed

+299
-29
lines changed

5 files changed

+299
-29
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.4.43] - 2021-04-20
8+
9+
### Fixed
10+
- Invalid escaping of single quot.
11+
- Type naming with schema title.
12+
713
## [0.4.42] - 2020-12-12
814

915
### Fixed
@@ -251,6 +257,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
251257
### Fixed
252258
- Removed unnecessary regexp dependency, #7.
253259

260+
[0.4.43]: https://github.com/swaggest/go-code-builder/compare/v0.4.42...v0.4.43
254261
[0.4.42]: https://github.com/swaggest/go-code-builder/compare/v0.4.41...v0.4.42
255262
[0.4.41]: https://github.com/swaggest/go-code-builder/compare/v0.4.40...v0.4.41
256263
[0.4.40]: https://github.com/swaggest/go-code-builder/compare/v0.4.39...v0.4.40

src/JsonSchema/GoBuilder.php

Lines changed: 52 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ class GoBuilder
5151
/** @var Type[] */
5252
public $pathTypesDefined = [];
5353

54+
/** @var string[] */
55+
public $pathByTypeName = [];
56+
57+
/** @var string[] */
58+
private $typeNameByPath = [];
59+
5460
public function __construct()
5561
{
5662
$this->code = new Code();
@@ -160,22 +166,12 @@ private function makeStruct(Schema $schema, $path)
160166
$this->generatedStructsBySchema->attach($schema, $generatedStruct);
161167
$generatedStruct->schema = $schema;
162168

163-
$structName = '';
164-
if ($schema->title) {
165-
$structName = $this->codeBuilder->exportableName($schema->title);
166-
}
167-
168-
if (empty($structName)) {
169-
if ($path === '#') {
170-
$structName = 'Untitled' . ++$this->untitledIndex;
171-
} else {
172-
$structName = $this->codeBuilder->exportableName($this->pathToName($path));
173-
}
169+
$pathToName = $this->pathToName($path);
170+
if ($path === '#' && empty($schema->title)) {
171+
$pathToName = 'Untitled' . ++$this->untitledIndex;
174172
}
175173

176-
if (isset($this->options->renames[$structName])) {
177-
$structName = $this->options->renames[$structName];
178-
}
174+
$structName = $this->typeName($schema, $pathToName);
179175

180176
if (isset($this->namesGenerated[$structName]) && $schema->getMeta(TypeBuilder::CONDITIONAL_META)) {
181177
$structName = $structName . 'Conditional';
@@ -433,4 +429,46 @@ public function isNullable(Schema $schema)
433429

434430
return false;
435431
}
432+
433+
/**
434+
* @param Schema $schema
435+
* @param string $path
436+
* @param StructDef|null $parentStruct
437+
*/
438+
public function typeName($schema, $path, $parentStruct = null)
439+
{
440+
if (isset($this->typeNameByPath[$path])) {
441+
return $this->typeNameByPath[$path];
442+
}
443+
444+
if (!empty($schema->title)) {
445+
$typeName = $this->codeBuilder->exportableName($schema->title, true);
446+
}
447+
448+
if (empty($typeName) || (isset($this->pathByTypeName[$typeName]) && $this->pathByTypeName[$typeName] !== $path)) {
449+
$typeName = $this->codeBuilder->exportableName($path);
450+
}
451+
452+
if ($parentStruct && false !== $pos = strrpos($path, '->')) {
453+
$propertyName = substr($path, $pos);
454+
$typeName = $this->codeBuilder->exportableName($parentStruct->getName() . $propertyName);
455+
}
456+
457+
if (isset($this->options->renames[$typeName])) {
458+
$typeName = $this->options->renames[$typeName];
459+
}
460+
461+
$tn = $typeName;
462+
$i = 2;
463+
464+
while (isset($this->pathByTypeName[$typeName]) && $this->pathByTypeName[$typeName] !== $path) {
465+
$typeName = $tn . 'Type' . $i;
466+
$i++;
467+
}
468+
469+
$this->pathByTypeName[$typeName] = $path;
470+
$this->typeNameByPath[$path] = $typeName;
471+
472+
return $typeName;
473+
}
436474
}

src/JsonSchema/TypeBuilder.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ private function processConst()
525525
{
526526
if ($this->schema->const !== null) { // todo properly process null const
527527
$path = $this->goBuilder->pathToName($this->path);
528-
$typeName = $this->goBuilder->codeBuilder->exportableName($path);
528+
$typeName = $this->goBuilder->typeName($this->schema, $path, $this->parentStruct);
529529
if ($this->parentStruct && false !== $pos = strrpos($path, '->')) {
530530
$propertyName = substr($path, $pos);
531531
$typeName = $this->goBuilder->codeBuilder->exportableName($this->parentStruct->getName() . $propertyName);
@@ -659,16 +659,7 @@ private function processEnum(NamedType $baseType)
659659
}
660660
$path = $this->goBuilder->pathToName($this->path);
661661

662-
$typeName = $this->goBuilder->codeBuilder->exportableName($path);
663-
if ($this->parentStruct && false !== $pos = strrpos($path, '->')) {
664-
$propertyName = substr($path, $pos);
665-
$typeName = $this->goBuilder->codeBuilder->exportableName($this->parentStruct->getName() . $propertyName);
666-
}
667-
668-
if (isset($this->goBuilder->options->renames[$typeName])) {
669-
$typeName = $this->goBuilder->options->renames[$typeName];
670-
}
671-
662+
$typeName = $this->goBuilder->typeName($this->schema, $path, $this->parentStruct);
672663
$type = new GoType($typeName);
673664
$this->goBuilder->pathTypesDefined[$this->path] = $type;
674665

@@ -682,7 +673,6 @@ private function processEnum(NamedType $baseType)
682673
GO
683674
);
684675

685-
686676
foreach ($enum as $index => $item) {
687677
$itemName = $this->goBuilder->codeBuilder->exportableName($typeName . '_' . $item);
688678
$comment = null;

src/Templates/GoTemplate.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ public function escapeValue($value)
8888
{
8989
if (is_string($value)) {
9090
if (strpos($value, '"') === false && strpos($value, '\\') === false) {
91-
$value = '"' . addslashes($value) . '"';
91+
$value = '"' . str_replace(["\n", "\t", "\r", '"'], ['\n', '\t', '\r', '\"'], $value) . '"';
9292
} elseif (strpos($value, '`')) {
93-
$value = '"' . addslashes($value) . '"';
93+
$value = '"' . str_replace(["\n", "\t", "\r", '"'], ['\n', '\t', '\r', '\"'], $value) . '"';
9494
} else {
9595
$value = '`' . $value . '`';
9696
}

0 commit comments

Comments
 (0)