diff --git a/.gitignore b/.gitignore index 5a22558..ded2fad 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ /vendor composer.lock -.phpunit.result.cache \ No newline at end of file +.phpunit.result.cache +.idea diff --git a/composer.json b/composer.json index 4a08960..4a3a165 100644 --- a/composer.json +++ b/composer.json @@ -22,7 +22,7 @@ "symfony/yaml": "^5.3" }, "require-dev": { - "laravel-json-api/laravel": "^1.0", + "laravel-json-api/laravel": "^2.0", "orchestra/testbench": "^6.9", "phpunit/phpunit": "^9.5", "ext-json": "*" diff --git a/src/Contracts/DescribesEndpoints.php b/src/Contracts/DescribesEndpoints.php new file mode 100644 index 0000000..796f943 --- /dev/null +++ b/src/Contracts/DescribesEndpoints.php @@ -0,0 +1,8 @@ +route->schema(); + + if (!$schema instanceof DescribesEndpoints) { + return ''; + } + + return $schema->describeEndpoint($this->route->route()->getName()); } /** diff --git a/src/Descriptors/Schema/Schema.php b/src/Descriptors/Schema/Schema.php index 2f75a7f..5f09f6a 100644 --- a/src/Descriptors/Schema/Schema.php +++ b/src/Descriptors/Schema/Schema.php @@ -111,7 +111,7 @@ public function store(Route $route): OASchema OASchema::object('attributes') ->properties(...$fields->get('attributes')), OASchema::object('relationships') - ->properties(...$fields->get('relationships')) + ->properties(...$fields->get('relationships') ?: []) ); } @@ -140,7 +140,7 @@ public function update(Route $route): OASchema OASchema::object('attributes') ->properties(...$fields->get('attributes')), OASchema::object('relationships') - ->properties(...$fields->get('relationships')) + ->properties(...$fields->get('relationships') ?: []) ) ->required('type', 'id', 'attributes'); } diff --git a/src/Generator.php b/src/Generator.php index 20b60c8..f269a5d 100644 --- a/src/Generator.php +++ b/src/Generator.php @@ -6,6 +6,7 @@ use GoldSpecDigital\ObjectOrientedOAS\OpenApi; use LaravelJsonApi\Contracts\Server\Server; +use LaravelJsonApi\Core\Support\AppResolver; use LaravelJsonApi\OpenApiSpec\Builders\InfoBuilder; use LaravelJsonApi\OpenApiSpec\Builders\PathsBuilder; use LaravelJsonApi\OpenApiSpec\Builders\ServerBuilder; @@ -37,10 +38,9 @@ public function __construct($key) $this->key = $key; $apiServer = config("jsonapi.servers.$key"); - $app = app(); + $appResolver = app(AppResolver::class); - - $this->server = new $apiServer($app, $this->key); + $this->server = new $apiServer($appResolver, $this->key); $this->infoBuilder = new InfoBuilder($this); $this->serverBuilder = new ServerBuilder($this); diff --git a/tests/Feature/OpenApiSchemaTest.php b/tests/Feature/OpenApiSchemaTest.php index 7543876..e77b839 100644 --- a/tests/Feature/OpenApiSchemaTest.php +++ b/tests/Feature/OpenApiSchemaTest.php @@ -29,4 +29,16 @@ public function test_has_many_should_have_array_as_type(): void $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']); } + + public function test_it_uses_the_description_from_the_schema() + { + $this->assertEquals('This is an example show all description', $this->spec['paths']['/posts']['get']['description']); + $this->assertEquals('This is an example show one description', $this->spec['paths']['/posts/{post}']['get']['description']); + $this->assertEquals('This is an example show posts author description', $this->spec['paths']['/posts/{post}/author']['get']['description']); + } + + public function test_it_creates_an_empty_description_if_a_schema_does_not_implement_the_describes_actions_interface() + { + $this->assertEquals('', $this->spec['paths']['/videos']['get']['description']); + } } diff --git a/tests/Support/JsonApi/V1/Posts/PostSchema.php b/tests/Support/JsonApi/V1/Posts/PostSchema.php index b175a7e..2705811 100644 --- a/tests/Support/JsonApi/V1/Posts/PostSchema.php +++ b/tests/Support/JsonApi/V1/Posts/PostSchema.php @@ -20,6 +20,7 @@ namespace LaravelJsonApi\OpenApiSpec\Tests\Support\JsonApi\V1\Posts; use LaravelJsonApi\HashIds\HashId; +use LaravelJsonApi\OpenApiSpec\Contracts\DescribesEndpoints; use LaravelJsonApi\OpenApiSpec\Tests\Support\Models\Post; use LaravelJsonApi\Eloquent\Fields\DateTime; use LaravelJsonApi\Eloquent\Fields\Relations\BelongsTo; @@ -37,7 +38,7 @@ use LaravelJsonApi\Eloquent\SoftDeletes; use LaravelJsonApi\Eloquent\Sorting\SortCountable; -class PostSchema extends Schema +class PostSchema extends Schema implements DescribesEndpoints { use SoftDeletes; @@ -61,6 +62,17 @@ class PostSchema extends Schema */ protected $defaultSort = '-createdAt'; + private array $descriptions = [ + 'v1.posts.index' => 'This is an example show all description', + 'v1.posts.show' => 'This is an example show one description', + 'v1.posts.author' => 'This is an example show posts author description', + ]; + + public function describeEndpoint(string $endpoint): string + { + return $this->descriptions[$endpoint] ?? ''; + } + /** * @inheritDoc */