Skip to content

Commit

Permalink
Check for unique type schemas in Markdown rendering (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Aug 3, 2023
1 parent 7720eb5 commit 8d1c179
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 72 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"require": {
"ext-json": "*",
"swaggest/json-schema": "^0.12.33",
"swaggest/code-builder": "^0.3.4",
"swaggest/code-builder": "^0.3.8",
"php": ">=5.6.0"
},
"require-dev": {
Expand Down
86 changes: 24 additions & 62 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 24 additions & 9 deletions src/Markdown/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace Swaggest\PhpCodeBuilder\Markdown;

use Swaggest\CodeBuilder\CodeBuilder;
use Swaggest\CodeBuilder\TableRenderer;
use Swaggest\JsonSchema\Schema;
use Swaggest\PhpCodeBuilder\PhpCode;
Expand All @@ -27,6 +26,8 @@ class TypeBuilder
*/
public $types = [];

public $uniqueTypeSchemas = [];

public $file = '';

public function __construct()
Expand Down Expand Up @@ -315,14 +316,10 @@ private function hasConstraints($schema)
return false;
}

private function makeTypeDef(Schema $schema, $path)
public function renderTypeDef(Schema $schema, $typeName, $path)
{
$tn = $this->typeName($schema, $path, true);
$typeName = $this->typeName($schema, $path);
$this->processed->attach($schema, $typeName);

$head = '';
if (!empty($schema->title) && $schema->title != $tn) {
if (!empty($schema->title) && $schema->title != $typeName) {
$head .= $schema->title . "\n";
}

Expand Down Expand Up @@ -353,12 +350,12 @@ private function makeTypeDef(Schema $schema, $path)
}
}

$tnl = strtolower($tn);
$tnl = strtolower($typeName);

$res = <<<MD
### <a id="$tnl"></a>$tn
### <a id="$tnl"></a>$typeName
$head
MD;
Expand All @@ -384,6 +381,7 @@ private function makeTypeDef(Schema $schema, $path)
->setColDelimiter('|')
->setHeadRowDelimiter('-')
->setOutlineVertical(true)
->multilineCellDelimiter('<br>')
->setShowHeader();

$res .= "\n\n";
Expand Down Expand Up @@ -419,6 +417,7 @@ private function makeTypeDef(Schema $schema, $path)
->setColDelimiter('|')
->setHeadRowDelimiter('-')
->setOutlineVertical(true)
->multilineCellDelimiter('<br>')
->setShowHeader();

}
Expand All @@ -427,7 +426,23 @@ private function makeTypeDef(Schema $schema, $path)
MD;

return $res;
}

private function makeTypeDef(Schema $schema, $path)
{
$tn = $this->typeName($schema, $path, true);
$typeName = $this->typeName($schema, $path);
$this->processed->attach($schema, $typeName);

$res = $this->renderTypeDef($schema, $tn, $path);

if (isset($this->uniqueTypeSchemas[$res])) {
return $this->uniqueTypeSchemas[$res];
}

$this->types[$typeName] = $res;
$this->uniqueTypeSchemas[$res] = $typeName;
$this->file .= $res;

return $typeName;
Expand Down

0 comments on commit 8d1c179

Please sign in to comment.