Skip to content

Commit

Permalink
Generate the correct routes for relationships.
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrala authored and captnCC committed Aug 10, 2021
1 parent 4cfbc01 commit 05d09dd
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
23 changes: 17 additions & 6 deletions src/Route.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


use Illuminate\Routing\Route as IlluminateRoute;
use Illuminate\Support\Facades\URL;
use Illuminate\Support\Str;
use LaravelJsonApi\Contracts\Schema\PolymorphicRelation;
use LaravelJsonApi\Contracts\Schema\Relation;
Expand Down Expand Up @@ -94,12 +95,7 @@ public function __construct(Server $server, IlluminateRoute $route)
$this->action = $action;
}


$this->uri = str_replace(
$route->getPrefix(),
'',
$route->uri()
);
$this->setUriForRoute();

[$controller, $method] = explode('@', $this->route->getActionName(), 2);

Expand Down Expand Up @@ -285,4 +281,19 @@ public static function belongsTo(
);
}

protected function setUriForRoute(): void {
$domain = URL::to('/');
$serverBasePath = str_replace(
$domain,
'',
$this->server->url(),
);

$this->uri = str_replace(
$serverBasePath,
'',
'/' . $this->route->uri(),
);
}

}
1 change: 1 addition & 0 deletions tests/Feature/GenerateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public function test_url_is_properly_parsed()
$spec = Yaml::parse($openapiYaml);

$this->assertArrayHasKey('/posts', $spec['paths'], 'Path to resource is not replaced correctly.');
$this->assertArrayHasKey('/posts/{post}/relationships/author', $spec['paths'], 'Path to resource is not replaced correctly.');

$this->assertEquals('http://localhost/api/v1', $spec['servers'][0]['variables']['serverUrl']['default']);
}
Expand Down

0 comments on commit 05d09dd

Please sign in to comment.