Skip to content

Commit

Permalink
Support non-Eloquent schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenpaauw committed Sep 1, 2023
1 parent 5ab90cf commit 3121bac
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions src/Descriptors/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,21 @@
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema as OASchema;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use LaravelJsonApi\Contracts\Schema\Attribute as AttributeContract;
use LaravelJsonApi\Contracts\Schema\Field;
use LaravelJsonApi\Contracts\Schema\PolymorphicRelation;
use LaravelJsonApi\Contracts\Schema\Relation as RelationContract;
use LaravelJsonApi\Contracts\Schema\Schema as JASchema;
use LaravelJsonApi\Contracts\Schema\Sortable;
use LaravelJsonApi\Core\Resources\JsonApiResource;
use LaravelJsonApi\Core\Support\Str;
use LaravelJsonApi\Eloquent;
use LaravelJsonApi\Eloquent\Fields\ArrayHash;
use LaravelJsonApi\Eloquent\Fields\ArrayList;
use LaravelJsonApi\Eloquent\Fields\Attribute;
use LaravelJsonApi\Eloquent\Fields\Boolean;
use LaravelJsonApi\Eloquent\Fields\ID;
use LaravelJsonApi\Eloquent\Fields\Map;
use LaravelJsonApi\Eloquent\Fields\Number;
use LaravelJsonApi\Eloquent\Fields\Relations\Relation;
use LaravelJsonApi\Eloquent\Pagination\CursorPagination;
use LaravelJsonApi\Eloquent\Pagination\PagePagination;
use LaravelJsonApi\OpenApiSpec\Builders\Paths\Operation\SchemaBuilder;
Expand Down Expand Up @@ -347,10 +347,10 @@ protected function fields(
return collect($fields)
->mapToGroups(function (Field $field) {
switch (true) {
case $field instanceof Attribute:
case $field instanceof AttributeContract:
$key = 'attributes';
break;
case $field instanceof Relation:
case $field instanceof RelationContract:
$key = 'relationships';
break;
default:
Expand Down Expand Up @@ -402,11 +402,11 @@ protected function attributes(

$schema = $fieldDataType->title($field->name());

$column = $field instanceof Attribute ? $field->column() : $field->name();
$column = $field instanceof AttributeContract ? $field->column() : $field->name();
if (isset($example[$column])) {
$schema = $schema->example($example[$column]);
}
if ($field->isReadOnly(null)) {
if (method_exists($field, 'isReadOnly') && $field->isReadOnly(null)) {
$schema = $schema->readOnly(true);
}

Expand All @@ -427,22 +427,22 @@ protected function relationships(
JsonApiResource $example
): array {
return $relationships
->map(function (Relation $relation) use ($example) {
->map(function (RelationContract $relation) use ($example) {
return $this->relationship($relation, $example);
})->toArray();
}

/**
* @param \LaravelJsonApi\Eloquent\Fields\Relations\Relation $relation
* @param \LaravelJsonApi\Core\Resources\JsonApiResource $example
* @param bool $includeData
* @param \LaravelJsonApi\Contracts\Schema\Relation $relation
* @param \LaravelJsonApi\Core\Resources\JsonApiResource $example
* @param bool $includeData
*
* @throws \GoldSpecDigital\ObjectOrientedOAS\Exceptions\InvalidArgumentException
*
* @return \GoldSpecDigital\ObjectOrientedOAS\Objects\Schema
*/
protected function relationship(
Relation $relation,
RelationContract $relation,
JsonApiResource $example,
bool $includeData = false
): OASchema {
Expand All @@ -469,16 +469,16 @@ protected function relationship(
}

/**
* @param \LaravelJsonApi\Eloquent\Fields\Relations\Relation $relation
* @param \LaravelJsonApi\Core\Resources\JsonApiResource $example
* @param string $type
* @param \LaravelJsonApi\Contracts\Schema\Relation $relation
* @param \LaravelJsonApi\Core\Resources\JsonApiResource $example
* @param string $type
*
* @throws \GoldSpecDigital\ObjectOrientedOAS\Exceptions\InvalidArgumentException
*
* @return \GoldSpecDigital\ObjectOrientedOAS\Objects\Schema
*/
protected function relationshipData(
Relation $relation,
RelationContract $relation,
JsonApiResource $example,
string $type
): OASchema {
Expand Down Expand Up @@ -524,7 +524,9 @@ public function relationshipLinks(
string $type
): OASchema {
$name = Str::dasherize(
Str::plural($relation->relationName())
Str::plural(method_exists($relation, 'relationName')
? $relation->relationName()
: Str::camel($relation->name()))
);

/*
Expand Down

0 comments on commit 3121bac

Please sign in to comment.