generated from spatie/package-skeleton-laravel
-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to explicitly name class based schemas using `#[SchemaNam…
…e]` attribute (#682) * wip * wip schema names * Fix styling * rethink references collection contexts * fix tests * added multiple open api transformers support * Fix styling --------- Co-authored-by: romalytvynenko <[email protected]>
- Loading branch information
1 parent
ee384d1
commit 6e0232c
Showing
36 changed files
with
571 additions
and
300 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Attributes; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* Allows naming class based schemas for different contexts. | ||
*/ | ||
#[Attribute(Attribute::TARGET_CLASS)] | ||
class SchemaName | ||
{ | ||
public function __construct( | ||
public readonly string $name, | ||
/** | ||
* Some classes can be used both as input and output schemas. So this property is used to | ||
* explicitly name the schema when is in input context. | ||
*/ | ||
public readonly ?string $input = null, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble; | ||
|
||
/** @internal */ | ||
class ContextReferences | ||
{ | ||
public function __construct( | ||
public readonly ContextReferencesCollection $schemas = new ContextReferencesCollection, | ||
public readonly ContextReferencesCollection $responses = new ContextReferencesCollection, | ||
public readonly ContextReferencesCollection $parameters = new ContextReferencesCollection, | ||
public readonly ContextReferencesCollection $examples = new ContextReferencesCollection, | ||
public readonly ContextReferencesCollection $requestBodies = new ContextReferencesCollection, | ||
public readonly ContextReferencesCollection $headers = new ContextReferencesCollection, | ||
public readonly ContextReferencesCollection $securitySchemes = new ContextReferencesCollection, | ||
public readonly ContextReferencesCollection $links = new ContextReferencesCollection, | ||
public readonly ContextReferencesCollection $callbacks = new ContextReferencesCollection, | ||
public readonly ContextReferencesCollection $pathItems = new ContextReferencesCollection, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble; | ||
|
||
use Dedoc\Scramble\Support\Generator\Reference; | ||
use Illuminate\Support\Str; | ||
|
||
/** @internal */ | ||
class ContextReferencesCollection | ||
{ | ||
private array $tempNames = []; | ||
|
||
/** | ||
* @param array<string, Reference[]> $items The key is reference ID and the value is a list of such references. | ||
*/ | ||
public function __construct( | ||
public array $items = [], | ||
) {} | ||
|
||
public function has(string $referenceId): bool | ||
{ | ||
return array_key_exists($referenceId, $this->items); | ||
} | ||
|
||
/** | ||
* @return Reference[] | ||
*/ | ||
public function get(string $referenceId): array | ||
{ | ||
return $this->items[$referenceId] ?? []; | ||
} | ||
|
||
public function add(string $referenceId, Reference $reference): Reference | ||
{ | ||
$this->items[$referenceId] ??= []; | ||
$this->items[$referenceId][] = $this->setUniqueName($reference); | ||
|
||
return $reference; | ||
} | ||
|
||
public function setUniqueName(Reference $reference): Reference | ||
{ | ||
$reference->fullName = $reference->shortName ?: $this->uniqueSchemaName($reference->fullName); | ||
|
||
return $reference; | ||
} | ||
|
||
public function uniqueName(string $referenceId): string | ||
{ | ||
if ($this->has($referenceId)) { | ||
$reference = $this->get($referenceId)[0]; | ||
|
||
return $reference->shortName ?: $reference->fullName; | ||
} | ||
|
||
return $this->uniqueSchemaName($referenceId); | ||
} | ||
|
||
private function uniqueSchemaName(string $fullName) | ||
{ | ||
$shortestPossibleName = class_basename($fullName); | ||
|
||
if ( | ||
($this->tempNames[$shortestPossibleName] ?? null) === null | ||
|| ($this->tempNames[$shortestPossibleName] ?? null) === $fullName | ||
) { | ||
$this->tempNames[$shortestPossibleName] = $fullName; | ||
|
||
return static::slug($shortestPossibleName); | ||
} | ||
|
||
return static::slug($fullName); | ||
} | ||
|
||
private static function slug(string $name) | ||
{ | ||
return Str::replace('\\', '.', $name); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble; | ||
|
||
use Dedoc\Scramble\Support\Generator\OpenApi; | ||
|
||
/** @internal */ | ||
class OpenApiContext | ||
{ | ||
public function __construct( | ||
public readonly OpenApi $openApi, | ||
public readonly GeneratorConfig $config, | ||
public ContextReferences $references = new ContextReferences, | ||
) {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Dedoc\Scramble\Support\Generator; | ||
|
||
use Dedoc\Scramble\Attributes\SchemaName; | ||
use ReflectionClass; | ||
|
||
class ClassBasedReference | ||
{ | ||
public static function create(string $referenceType, string $className, Components $components) | ||
{ | ||
return new Reference($referenceType, $className, $components, static::getClassBasedName($className)); | ||
} | ||
|
||
public static function createInput(string $referenceType, string $className, Components $components) | ||
{ | ||
return new Reference($referenceType, $className, $components, static::getClassBasedName($className, input: true)); | ||
} | ||
|
||
private static function getClassBasedName(string $className, bool $input = false): ?string | ||
{ | ||
$reflectionClass = new ReflectionClass($className); | ||
|
||
$schemaNameAttribute = ($reflectionClass->getAttributes(SchemaName::class)[0] ?? null)?->newInstance(); | ||
|
||
return $schemaNameAttribute | ||
? ($input && $schemaNameAttribute->input ? $schemaNameAttribute->input : $schemaNameAttribute->name) | ||
: null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.