Skip to content

Commit

Permalink
amending bundle tool to verify and complete missing discriminators on…
Browse files Browse the repository at this point in the history
… build time (relates to #99)
  • Loading branch information
sebastian-iancu committed Nov 24, 2023
1 parent 750d37d commit 94d05e1
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 9 additions & 0 deletions development/src/Writer/AbstractWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,4 +130,13 @@ public function writeJsonFile(): void {
}
}

/**
* @param string $schemaTitle
* @return string
*/
public static function getSchemaRef(string $schemaTitle): string {
$schemaName = ucfirst(str_replace(' ', '', ucwords(str_replace('_', ' ', strtolower($schemaTitle)))));
return '#/components/schemas/' . $schemaName;
}

}
19 changes: 18 additions & 1 deletion development/src/Writer/Codegen.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace OpenEHR\Specifications\Tools\OpenAPI\Writer;

use cebe\openapi\spec\Schema;
use cebe\openapi\spec\Discriminator;

class Codegen extends AbstractWriter {

Expand All @@ -27,12 +28,28 @@ protected function prepareInput(): void {
*/
protected function cleaning(Schema $schema): void {
if ($schema->properties) {
foreach ( $schema->properties as $property) {
foreach ($schema->properties as $property) {
if (isset($property->format) && $property->format === 'uuid') {
unset($property->format);
}
}
}
if ($schema->allOf) {
/** @var Schema $parentSchema */
$parentSchema = $schema->allOf[0]->resolve();
if (count($schema->allOf) > 1) {
echo "\n WARNING: $schema->title have more children under [allOf].\n";
}
if (!isset($parentSchema->discriminator)) {
$parentSchema->discriminator = new Discriminator(['propertyName' => '_type']);
}
if (!isset($parentSchema->discriminator->mapping[$schema->title])) {
$mapping = $parentSchema->discriminator->mapping ?? [];
$mapping[$schema->title] = static::getSchemaRef($schema->title);
echo "(added $schema->title mapping in $parentSchema->title) ";
$parentSchema->discriminator->mapping = $mapping;
}
}
}

/**
Expand Down

0 comments on commit 94d05e1

Please sign in to comment.