Skip to content

Commit

Permalink
fix reference being ignored if one type had few type to schema transf…
Browse files Browse the repository at this point in the history
…ormers (#623)
  • Loading branch information
romalytvynenko authored Nov 10, 2024
1 parent 9bef930 commit e8c2b8f
Showing 1 changed file with 31 additions and 32 deletions.
63 changes: 31 additions & 32 deletions src/Support/Generator/TypeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Dedoc\Scramble\Support\Type\TemplateType;
use Dedoc\Scramble\Support\Type\Type;
use Dedoc\Scramble\Support\Type\Union;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use PHPStan\PhpDocParser\Ast\PhpDoc\PhpDocNode;

Expand Down Expand Up @@ -221,46 +222,44 @@ public function transform(Type $type)

private function handleUsingExtensions(Type $type)
{
return array_reduce(
$this->typeToSchemaExtensions,
function ($acc, $extensionClass) use ($type) {
$extension = new $extensionClass($this->infer, $this, $this->components);
/** @var Collection $extensions */
$extensions = collect($this->typeToSchemaExtensions)
->map(fn ($extensionClass) => new $extensionClass($this->infer, $this, $this->components))
->filter->shouldHandle($type)
->values();

if (! $extension->shouldHandle($type)) {
return $acc;
}
$referenceExtension = $extensions->last();

/** @var Reference|null $reference */
$reference = method_exists($extension, 'reference')
? $extension->reference($type)
: null;
/** @var Reference|null $reference */
$reference = $referenceExtension && method_exists($referenceExtension, 'reference')
? $referenceExtension->reference($type)
: null;

if ($reference && $this->components->hasSchema($reference->fullName)) {
return $reference;
}
if ($reference && $this->components->hasSchema($reference->fullName)) {
return $reference;
}

if ($reference) {
$this->components->addSchema($reference->fullName, Schema::fromType(new UnknownType('Reference is being analyzed.')));
}
if ($reference) {
$this->components->addSchema($reference->fullName, Schema::fromType(new UnknownType('Reference is being analyzed.')));
}

if ($handledType = $extension->toSchema($type, $acc)) {
if ($reference) {
return $this->components->addSchema($reference->fullName, Schema::fromType($handledType));
}
$handledType = $extensions
->reduce(function ($acc, $extension) use ($type) {
return $extension->toSchema($type, $acc) ?: $acc;
});

return $handledType;
}
if ($handledType && $reference) {
$reference = $this->components->addSchema($reference->fullName, Schema::fromType($handledType));
}

/*
* If we couldn't handle a type, the reference is removed.
*/
if ($reference) {
$this->components->removeSchema($reference->fullName);
}
/*
* If we couldn't handle a type, the reference is removed.
*/
if (! $handledType && $reference) {
$this->components->removeSchema($reference->fullName);
}

return $acc;
}
);
return $reference ?: $handledType;
}

public function toResponse(Type $type)
Expand Down

0 comments on commit e8c2b8f

Please sign in to comment.