diff --git a/src/Descriptors/Schema/Schema.php b/src/Descriptors/Schema/Schema.php index 56e6017..2f75a7f 100644 --- a/src/Descriptors/Schema/Schema.php +++ b/src/Descriptors/Schema/Schema.php @@ -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'); } @@ -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'); } @@ -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'); } /** @@ -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; + } + } diff --git a/tests/Feature/OpenApiSchemaTest.php b/tests/Feature/OpenApiSchemaTest.php new file mode 100644 index 0000000..7543876 --- /dev/null +++ b/tests/Feature/OpenApiSchemaTest.php @@ -0,0 +1,32 @@ +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']); + } +}