Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 31 additions & 33 deletions src/Descriptors/Schema/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -179,20 +179,7 @@ public function updateRelationship(Route $route): OASchema
->resource(Arr::first($route->inversSchemas())::model());
}

$inverseRelation = $route->relation() !== null ? $route->relation()->inverse() : null;
$relation = $route->relation();

$dataSchema = $this
->relationshipData(
$relation,
$resource,
$inverseRelation
);

if ($relation instanceof Eloquent\Fields\Relations\ToMany) {
$dataSchema = OASchema::array('data')
->items($dataSchema);
}
$dataSchema = $this->getDataSchema($route, $resource);

return $dataSchema->title('Resource/'.ucfirst($route->name(true)).'/Relationship/'.ucfirst($route->relationName()).'/Update');
}
Expand All @@ -211,21 +198,7 @@ public function attachRelationship(Route $route): OASchema
->resource(Arr::first($route->inversSchemas())::model());
}

$inverseRelation = $route->relation() !== null ? $route->relation()->inverse() : null;

$relation = $route->relation();

$dataSchema = $this
->relationshipData(
$relation,
$resource,
$inverseRelation
);

if ($relation instanceof Eloquent\Fields\Relations\ToMany) {
$dataSchema = OASchema::array('data')
->items($dataSchema);
}
$dataSchema = $this->getDataSchema($route, $resource);

return $dataSchema->title('Resource/'.ucfirst($route->name(true)).'/Relationship/'.ucfirst($route->relationName()).'/Attach');
}
Expand All @@ -243,10 +216,9 @@ public function detachRelationship(Route $route): OASchema
$resource = $this->generator->resources()
->resource(Arr::first($route->inversSchemas())::model());
}
$inverseRelation = $route->relation() !== null ? $route->relation()->inverse() : null;
return $this->relationshipData($route->relation(), $resource,
$inverseRelation)
->title('Resource/'.ucfirst($route->name(true)).'/Relationship/'.ucfirst($route->relationName()).'/Detach');
$dataSchema = $this->getDataSchema($route, $resource);

return $dataSchema->title('Resource/'.ucfirst($route->name(true)).'/Relationship/'.ucfirst($route->relationName()).'/Detach');
}

/**
Expand Down Expand Up @@ -609,4 +581,30 @@ protected function getDescriptor(Eloquent\Contracts\Filter $filter
}
}

/**
* @param Route $route
* @param JsonApiResource $resource
* @return OASchema
* @throws \GoldSpecDigital\ObjectOrientedOAS\Exceptions\InvalidArgumentException
*/
protected function getDataSchema(Route $route, JsonApiResource $resource): OASchema
{
$inverseRelation = $route->relation() !== null ? $route->relation()->inverse() : null;

$relation = $route->relation();

$dataSchema = $this
->relationshipData(
$relation,
$resource,
$inverseRelation
);

if ($relation instanceof Eloquent\Fields\Relations\ToMany) {
$dataSchema = OASchema::array('data')
->items($dataSchema);
}
return $dataSchema;
}

}
32 changes: 32 additions & 0 deletions tests/Feature/OpenApiSchemaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace LaravelJsonApi\OpenApiSpec\Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use LaravelJsonApi\OpenApiSpec\Facades\GeneratorFacade;
use LaravelJsonApi\OpenApiSpec\Tests\Support\Database\Seeders\DatabaseSeeder;
use LaravelJsonApi\OpenApiSpec\Tests\TestCase;

class OpenApiSchemaTest extends TestCase
{
use RefreshDatabase;

private array $spec;

public function setUp(): void
{
parent::setUp();

$this->seed(DatabaseSeeder::class);

$output = GeneratorFacade::generate('v1', 'json');
$this->spec = json_decode($output, true);
}

public function test_has_many_should_have_array_as_type(): void
{
$this->assertEquals('array', $this->spec['components']['schemas']['resources.posts.relationship.tags.update']['type']);
$this->assertEquals('array', $this->spec['components']['schemas']['resources.posts.relationship.tags.attach']['type']);
$this->assertEquals('array', $this->spec['components']['schemas']['resources.posts.relationship.tags.detach']['type']);
}
}