Skip to content

Commit

Permalink
ability to use paginators as a part of other schemas (#574)
Browse files Browse the repository at this point in the history
  • Loading branch information
romalytvynenko authored Oct 14, 2024
1 parent 278779c commit d004370
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 9 deletions.
18 changes: 15 additions & 3 deletions src/Support/TypeToSchemaExtensions/CursorPaginatorTypeToSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function shouldHandle(Type $type)
/**
* @param Generic $type
*/
public function toResponse(Type $type)
public function toSchema(Type $type)
{
$collectingClassType = $type->templateTypes[0];

Expand All @@ -41,7 +41,7 @@ public function toResponse(Type $type)
return null;
}

$type = (new OpenApiObjectType)
return (new OpenApiObjectType)
->addProperty('data', (new ArrayType)->setItems($collectingType))
->addProperty('path', (new StringType)->nullable(true)->setDescription('Base path for paginator generated URLs.'))
->addProperty('per_page', (new IntegerType)->setDescription('Number of items shown per page.'))
Expand All @@ -50,9 +50,21 @@ public function toResponse(Type $type)
->addProperty('prev_cursor', (new StringType)->nullable(true)->setDescription('The "cursor" that points to the previous set of items.'))
->addProperty('prev_page_url', (new StringType)->format('uri')->nullable(true))
->setRequired(['data', 'path', 'per_page', 'next_cursor', 'next_page_url', 'prev_cursor', 'prev_page_url']);
}

/**
* @param Generic $type
*/
public function toResponse(Type $type)
{
$collectingClassType = $type->templateTypes[0];

if (! $collectingClassType->isInstanceOf(JsonResource::class) && ! $collectingClassType->isInstanceOf(Model::class)) {
return null;
}

return Response::make(200)
->description('Paginated set of `'.$this->components->uniqueSchemaName($collectingClassType->name).'`')
->setContent('application/json', Schema::fromType($type));
->setContent('application/json', Schema::fromType($this->openApiTransformer->transform($type)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function shouldHandle(Type $type)
/**
* @param Generic $type
*/
public function toResponse(Type $type)
public function toSchema(Type $type)
{
$collectingClassType = $type->templateTypes[0];

Expand All @@ -42,7 +42,7 @@ public function toResponse(Type $type)
return null;
}

$type = (new OpenApiObjectType)
return (new OpenApiObjectType)
->addProperty('current_page', new IntegerType)
->addProperty('data', (new ArrayType)->setItems($collectingType))
->addProperty('first_page_url', (new StringType)->nullable(true))
Expand All @@ -63,9 +63,21 @@ public function toResponse(Type $type)
->addProperty('to', (new IntegerType)->nullable(true)->setDescription('Number of the last item in the slice.'))
->addProperty('total', (new IntegerType)->setDescription('Total number of items being paginated.'))
->setRequired(['current_page', 'data', 'first_page_url', 'from', 'last_page_url', 'last_page', 'links', 'next_page_url', 'path', 'per_page', 'prev_page_url', 'to', 'total']);
}

/**
* @param Generic $type
*/
public function toResponse(Type $type)
{
$collectingClassType = $type->templateTypes[0];

if (! $collectingClassType->isInstanceOf(JsonResource::class) && ! $collectingClassType->isInstanceOf(Model::class)) {
return null;
}

return Response::make(200)
->description('Paginated set of `'.$this->components->uniqueSchemaName($collectingClassType->name).'`')
->setContent('application/json', Schema::fromType($type));
->setContent('application/json', Schema::fromType($this->openApiTransformer->transform($type)));
}
}
18 changes: 15 additions & 3 deletions src/Support/TypeToSchemaExtensions/PaginatorTypeToSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function shouldHandle(Type $type)
/**
* @param Generic $type
*/
public function toResponse(Type $type)
public function toSchema(Type $type)
{
$collectingClassType = $type->templateTypes[0];

Expand All @@ -41,7 +41,7 @@ public function toResponse(Type $type)
return null;
}

$type = (new OpenApiObjectType)
return (new OpenApiObjectType)
->addProperty('current_page', new IntegerType)
->addProperty('data', (new ArrayType)->setItems($collectingType))
->addProperty('first_page_url', (new StringType)->nullable(true))
Expand All @@ -52,9 +52,21 @@ public function toResponse(Type $type)
->addProperty('prev_page_url', (new StringType)->nullable(true))
->addProperty('to', (new IntegerType)->nullable(true)->setDescription('Number of the last item in the slice.'))
->setRequired(['current_page', 'data', 'first_page_url', 'from', 'next_page_url', 'path', 'per_page', 'prev_page_url', 'to']);
}

/**
* @param Generic $type
*/
public function toResponse(Type $type)
{
$collectingClassType = $type->templateTypes[0];

if (! $collectingClassType->isInstanceOf(JsonResource::class) && ! $collectingClassType->isInstanceOf(Model::class)) {
return null;
}

return Response::make(200)
->description('Paginated set of `'.$this->components->uniqueSchemaName($collectingClassType->name).'`')
->setContent('application/json', Schema::fromType($type));
->setContent('application/json', Schema::fromType($this->openApiTransformer->transform($type)));
}
}

0 comments on commit d004370

Please sign in to comment.