Skip to content

Commit

Permalink
fixed found issues
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko committed Oct 30, 2022
1 parent 1f01085 commit 7cad768
Show file tree
Hide file tree
Showing 10 changed files with 43 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public function __invoke()
try {
return $this->routeToOperation($route);
} catch (Throwable $e) {
throw $e;
throw RouteAnalysisErrorException::make($route, $e);
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Dedoc\Scramble\Support\Type\ObjectType;
use Dedoc\Scramble\Support\Type\Type;
use Illuminate\Auth\Access\AuthorizationException;
use Illuminate\Support\Str;

class AuthorizationExceptionToResponseExtension extends ExceptionToResponseExtension
{
Expand Down Expand Up @@ -39,6 +40,6 @@ public function toResponse(Type $type)

public function reference(ObjectType $type)
{
return new Reference('responses', $type->name, $this->components);
return new Reference('responses', Str::start($type->name, '\\'), $this->components);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Dedoc\Scramble\Support\Type\ObjectType;
use Dedoc\Scramble\Support\Type\Type;
use Illuminate\Database\RecordsNotFoundException;
use Illuminate\Support\Str;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

class NotFoundExceptionToResponseExtension extends ExceptionToResponseExtension
Expand Down Expand Up @@ -43,6 +44,6 @@ public function toResponse(Type $type)

public function reference(ObjectType $type)
{
return new Reference('responses', $type->name, $this->components);
return new Reference('responses', Str::start($type->name, '\\'), $this->components);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Dedoc\Scramble\Support\Generator\Types as OpenApiTypes;
use Dedoc\Scramble\Support\Type\ObjectType;
use Dedoc\Scramble\Support\Type\Type;
use Illuminate\Support\Str;
use Illuminate\Validation\ValidationException;

class ValidationExceptionToResponseExtension extends ExceptionToResponseExtension
Expand Down Expand Up @@ -45,6 +46,6 @@ public function toResponse(Type $type)

public function reference(ObjectType $type)
{
return new Reference('responses', $type->name, $this->components);
return new Reference('responses', Str::start($type->name, '\\'), $this->components);
}
}
4 changes: 4 additions & 0 deletions src/Support/Generator/TypeTransformer.php
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,10 @@ function ($acc, $extensionClass) use ($type) {
public function toResponse(Type $type)
{
if (! $response = $this->handleResponseUsingExtensions($type)) {
if ($type->isInstanceOf(\Throwable::class)) {
return null;
}

$response = Response::make(200)
->setContent(
'application/json',
Expand Down
11 changes: 10 additions & 1 deletion src/Support/InferExtensions/JsonResourceCallsTypeInfer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Dedoc\Scramble\Infer\Extensions\ExpressionTypeInferExtension;
use Dedoc\Scramble\Infer\Scope\Scope;
use Dedoc\Scramble\Support\Type\ObjectType;
use Dedoc\Scramble\Support\Type\Type;
use Dedoc\Scramble\Support\Type\TypeHelper;
use Illuminate\Http\Resources\Json\JsonResource;
Expand All @@ -25,7 +26,15 @@ public function getType(Expr $node, Scope $scope): ?Type
if ($node->name->toString() === 'additional' && isset($node->args[0])) {
$type = $scope->getType($node->var);

$type->properties = array_merge($type->properties, [
$objectType = $type instanceof ObjectType
? $type
: $type->type ?? null; // the case then type is Generic. This is the documented case of resources.

if (! $objectType instanceof ObjectType) {
return null;
}

$objectType->properties = array_merge($objectType->properties, [
'additional' => TypeHelper::getArgType($scope, $node->args, ['data', 0]),
]);

Expand Down
15 changes: 11 additions & 4 deletions src/Support/InferHandlers/PhpDocHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Dedoc\Scramble\PhpDoc\ResolveFqnPhpDocTypeVisitor;
use Dedoc\Scramble\Support\PhpDoc;
use Dedoc\Scramble\Support\Type\Type;
use Dedoc\Scramble\Support\Type\Union;
use Illuminate\Support\Str;
use PhpParser\Comment;
use PhpParser\Comment\Doc;
Expand Down Expand Up @@ -59,10 +60,16 @@ public function leave(Node $node, Scope $scope): ?Type
if ($node instanceof Node\Stmt\ClassMethod && ($methodType = $scope->getType($node)) && $doc = $node->getDocComment()) {
$docNode = $this->getDocNode($scope, $doc);

$thrownExceptions = array_map(
fn (ThrowsTagValueNode $t) => PhpDocTypeHelper::toType($t->type),
$docNode->getThrowsTagValues(),
);
$thrownExceptions = collect($docNode->getThrowsTagValues())
->flatMap(function (ThrowsTagValueNode $t) {
$type = PhpDocTypeHelper::toType($t->type);

if ($type instanceof Union) {
return $type->types;
}

return [$type];
});

$methodType->exceptions = [
...$methodType->exceptions,
Expand Down
4 changes: 2 additions & 2 deletions src/Support/OperationExtensions/ResponseExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ public function handle(Operation $operation, RouteInfo $routeInfo)

$responses = collect($returnTypes)
->merge($routeInfo->getMethodType()->exceptions)
->map(fn ($returnType) => $this->openApiTransformer->toResponse($returnType));
->map(fn ($returnType) => $this->openApiTransformer->toResponse($returnType))
->filter();

[$responses, $references] = $responses->partition(fn ($r) => $r instanceof Response);

$responses = $responses
->filter()
->groupBy('code')
->map(function (Collection $responses, $code) {
if (count($responses) === 1) {
Expand Down
5 changes: 5 additions & 0 deletions src/Support/Type/Generic.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function isInstanceOf(string $className)
return $this->type->isInstanceOf($className);
}

public function getPropertyFetchType(string $propertyName): Type
{
return $this->type->getPropertyFetchType($propertyName);
}

public function children(): array
{
return [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public function toSchema(Type $type)
}
}

if (! $array instanceof ArrayType) {
return new UnknownType();
}

$array->items = $this->flattenMergeValues($array->items);

return $this->openApiTransformer->transform($array);
Expand Down

0 comments on commit 7cad768

Please sign in to comment.