Skip to content

Commit 51573a3

Browse files
antograssiotmab05kdunglas
authored
Add possibility to specify a url generation strategy (#3198)
* add ability to return absolute urls instead of relative IRIs apply absolute url to all generated URLs/IRIs Allow passing an url generation strategy instead of a bool Switch to a resource attribute to manage URL generation strategy * fix: rename function Co-authored-by: Michael A. Bos <[email protected]> Co-authored-by: Kévin Dunglas <[email protected]>
1 parent e7dad31 commit 51573a3

36 files changed

+1344
-60
lines changed

features/bootstrap/DoctrineContext.php

Lines changed: 77 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
declare(strict_types=1);
1313

14+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\AbsoluteUrlDummy as AbsoluteUrlDummyDocument;
15+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\AbsoluteUrlRelationDummy as AbsoluteUrlRelationDummyDocument;
1416
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Address as AddressDocument;
1517
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Answer as AnswerDocument;
1618
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\CompositeItem as CompositeItemDocument;
@@ -52,6 +54,8 @@
5254
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\FourthLevel as FourthLevelDocument;
5355
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Greeting as GreetingDocument;
5456
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\MaxDepthDummy as MaxDepthDummyDocument;
57+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\NetworkPathDummy as NetworkPathDummyDocument;
58+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\NetworkPathRelationDummy as NetworkPathRelationDummyDocument;
5559
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Order as OrderDocument;
5660
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Person as PersonDocument;
5761
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\PersonToPet as PersonToPetDocument;
@@ -69,6 +73,8 @@
6973
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ThirdLevel as ThirdLevelDocument;
7074
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\UrlEncodedId as UrlEncodedIdDocument;
7175
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
76+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\AbsoluteUrlDummy;
77+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\AbsoluteUrlRelationDummy;
7278
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Address;
7379
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer;
7480
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
@@ -113,6 +119,9 @@
113119
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Greeting;
114120
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\InternalUser;
115121
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\MaxDepthDummy;
122+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\NetworkPathDummy;
123+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\NetworkPathRelationDummy;
124+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Node;
116125
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Order;
117126
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Person;
118127
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\PersonToPet;
@@ -1533,7 +1542,7 @@ public function thereAreDummyMercureObjects(int $nb)
15331542
{
15341543
for ($i = 1; $i <= $nb; ++$i) {
15351544
$relatedDummy = $this->buildRelatedDummy();
1536-
$relatedDummy->setName('RelatedDummy #'.$i);
1545+
$relatedDummy->setName('RelatedDummy #' . $i);
15371546

15381547
$dummyMercure = $this->buildDummyMercure();
15391548
$dummyMercure->name = "Dummy Mercure #$i";
@@ -1547,6 +1556,40 @@ public function thereAreDummyMercureObjects(int $nb)
15471556
$this->manager->flush();
15481557
}
15491558

1559+
/**
1560+
* @Given there are :nb absoluteUrlDummy objects with a related absoluteUrlRelationDummy
1561+
*/
1562+
public function thereAreAbsoluteUrlDummies(int $nb)
1563+
{
1564+
for ($i = 1; $i <= $nb; ++$i) {
1565+
$absoluteUrlRelationDummy = $this->buildAbsoluteUrlRelationDummy();
1566+
$absoluteUrlDummy = $this->buildAbsoluteUrlDummy();
1567+
$absoluteUrlDummy->absoluteUrlRelationDummy = $absoluteUrlRelationDummy;
1568+
1569+
$this->manager->persist($absoluteUrlRelationDummy);
1570+
$this->manager->persist($absoluteUrlDummy);
1571+
}
1572+
1573+
$this->manager->flush();
1574+
}
1575+
1576+
/**
1577+
* @Given there are :nb networkPathDummy objects with a related networkPathRelationDummy
1578+
*/
1579+
public function thereAreNetworkPathDummies(int $nb)
1580+
{
1581+
for ($i = 1; $i <= $nb; ++$i) {
1582+
$networkPathRelationDummy = $this->buildNetworkPathRelationDummy();
1583+
$networkPathDummy = $this->buildNetworkPathDummy();
1584+
$networkPathDummy->networkPathRelationDummy = $networkPathRelationDummy;
1585+
1586+
$this->manager->persist($networkPathRelationDummy);
1587+
$this->manager->persist($networkPathDummy);
1588+
}
1589+
1590+
$this->manager->flush();
1591+
}
1592+
15501593
private function isOrm(): bool
15511594
{
15521595
return null !== $this->schemaTool;
@@ -1931,5 +1974,38 @@ private function buildConvertedRelated()
19311974
private function buildDummyMercure()
19321975
{
19331976
return $this->isOrm() ? new DummyMercure() : new DummyMercureDocument();
1977+
1978+
}
1979+
1980+
/**
1981+
* @return AbsoluteUrlDummyDocument|AbsoluteUrlDummy
1982+
*/
1983+
private function buildAbsoluteUrlDummy()
1984+
{
1985+
return $this->isOrm() ? new AbsoluteUrlDummy() : new AbsoluteUrlDummyDocument();
1986+
}
1987+
1988+
/**
1989+
* @return AbsoluteUrlRelationDummyDocument|AbsoluteUrlRelationDummy
1990+
*/
1991+
private function buildAbsoluteUrlRelationDummy()
1992+
{
1993+
return $this->isOrm() ? new AbsoluteUrlRelationDummy() : new AbsoluteUrlRelationDummyDocument();
1994+
}
1995+
1996+
/**
1997+
* @return NetworkPathDummyDocument|NetworkPathDummy
1998+
*/
1999+
private function buildNetworkPathDummy()
2000+
{
2001+
return $this->isOrm() ? new NetworkPathDummy() : new NetworkPathDummyDocument();
2002+
}
2003+
2004+
/**
2005+
* @return NetworkPathRelationDummyDocument|NetworkPathRelationDummy
2006+
*/
2007+
private function buildNetworkPathRelationDummy()
2008+
{
2009+
return $this->isOrm() ? new NetworkPathRelationDummy() : new NetworkPathRelationDummyDocument();
19342010
}
19352011
}

features/doctrine/search_filter.feature

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,6 @@ Feature: Search filter on collections
805805
When I send a "GET" request to "/converted_owners?name_converted.name_converted=Converted 3"
806806
Then the response status code should be 200
807807
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
808-
Then print last JSON response
809808
And the JSON should be valid according to this schema:
810809
"""
811810
{

features/hal/absolute_url.feature

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
Feature: IRI should contain Absolute URL
2+
In order to add detail to IRIs
3+
Include the absolute url
4+
5+
@createSchema
6+
Scenario: I should be able to GET a collection of Objects with Absolute Urls
7+
Given there are 1 absoluteUrlDummy objects with a related absoluteUrlRelationDummy
8+
And I add "Accept" header equal to "application/hal+json"
9+
And I send a "GET" request to "/absolute_url_dummies"
10+
And the JSON should be equal to:
11+
"""
12+
{
13+
"_links": {
14+
"self": {
15+
"href": "http://example.com/absolute_url_dummies"
16+
},
17+
"item": [
18+
{
19+
"href": "http://example.com/absolute_url_dummies/1"
20+
}
21+
]
22+
},
23+
"totalItems": 1,
24+
"itemsPerPage": 3,
25+
"_embedded": {
26+
"item": [
27+
{
28+
"_links": {
29+
"self": {
30+
"href": "http://example.com/absolute_url_dummies/1"
31+
},
32+
"absoluteUrlRelationDummy": {
33+
"href": "http://example.com/absolute_url_relation_dummies/1"
34+
}
35+
},
36+
"id": 1
37+
}
38+
]
39+
}
40+
}
41+
"""
42+
43+
Scenario: I should be able to POST an object using an Absolute Url
44+
Given I add "Accept" header equal to "application/hal+json"
45+
And I add "Content-Type" header equal to "application/json"
46+
And I send a "POST" request to "/absolute_url_relation_dummies" with body:
47+
"""
48+
{
49+
"absolute_url_dummies": "http://example.com/absolute_url_dummies/1"
50+
}
51+
"""
52+
Then the response status code should be 201
53+
And the JSON should be equal to:
54+
"""
55+
{
56+
"_links": {
57+
"self": {
58+
"href": "http://example.com/absolute_url_relation_dummies/2"
59+
}
60+
},
61+
"id": 2
62+
}
63+
"""
64+
65+
Scenario: I should be able to GET an Item with Absolute Urls
66+
Given I add "Accept" header equal to "application/hal+json"
67+
And I add "Content-Type" header equal to "application/json"
68+
And I send a "GET" request to "/absolute_url_dummies/1"
69+
And the JSON should be equal to:
70+
"""
71+
{
72+
"_links": {
73+
"self": {
74+
"href": "http://example.com/absolute_url_dummies/1"
75+
},
76+
"absoluteUrlRelationDummy": {
77+
"href": "http://example.com/absolute_url_relation_dummies/1"
78+
}
79+
},
80+
"id": 1
81+
}
82+
"""
83+
84+
Scenario: I should be able to GET subresources with Absolute Urls
85+
Given I add "Accept" header equal to "application/hal+json"
86+
And I add "Content-Type" header equal to "application/json"
87+
And I send a "GET" request to "/absolute_url_relation_dummies/1/absolute_url_dummies"
88+
And the JSON should be equal to:
89+
"""
90+
{
91+
"_links": {
92+
"self": {
93+
"href": "http://example.com/absolute_url_relation_dummies/1/absolute_url_dummies"
94+
},
95+
"item": [
96+
{
97+
"href": "http://example.com/absolute_url_dummies/1"
98+
}
99+
]
100+
},
101+
"totalItems": 1,
102+
"itemsPerPage": 3,
103+
"_embedded": {
104+
"item": [
105+
{
106+
"_links": {
107+
"self": {
108+
"href": "http://example.com/absolute_url_dummies/1"
109+
},
110+
"absoluteUrlRelationDummy": {
111+
"href": "http://example.com/absolute_url_relation_dummies/1"
112+
}
113+
},
114+
"id": 1
115+
}
116+
]
117+
}
118+
}
119+
"""

features/hal/network_path.feature

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
Feature: IRI should contain network path
2+
In order to add detail to IRIs
3+
Include the network path
4+
5+
@createSchema
6+
Scenario: I should be able to GET a collection of objects with network paths
7+
Given there are 1 networkPathDummy objects with a related networkPathRelationDummy
8+
And I add "Accept" header equal to "application/hal+json"
9+
And I send a "GET" request to "/network_path_dummies"
10+
And the JSON should be equal to:
11+
"""
12+
{
13+
"_links": {
14+
"self": {
15+
"href": "//example.com/network_path_dummies"
16+
},
17+
"item": [
18+
{
19+
"href": "//example.com/network_path_dummies/1"
20+
}
21+
]
22+
},
23+
"totalItems": 1,
24+
"itemsPerPage": 3,
25+
"_embedded": {
26+
"item": [
27+
{
28+
"_links": {
29+
"self": {
30+
"href": "//example.com/network_path_dummies/1"
31+
},
32+
"networkPathRelationDummy": {
33+
"href": "//example.com/network_path_relation_dummies/1"
34+
}
35+
},
36+
"id": 1
37+
}
38+
]
39+
}
40+
}
41+
"""
42+
43+
Scenario: I should be able to POST an object using a network path
44+
Given I add "Accept" header equal to "application/hal+json"
45+
And I add "Content-Type" header equal to "application/json"
46+
And I send a "POST" request to "/network_path_relation_dummies" with body:
47+
"""
48+
{
49+
"network_path_dummies": "//example.com/network_path_dummies/1"
50+
}
51+
"""
52+
Then the response status code should be 201
53+
And the JSON should be equal to:
54+
"""
55+
{
56+
"_links": {
57+
"self": {
58+
"href": "//example.com/network_path_relation_dummies/2"
59+
}
60+
},
61+
"id": 2
62+
}
63+
"""
64+
65+
Scenario: I should be able to GET an Item with network paths
66+
Given I add "Accept" header equal to "application/hal+json"
67+
And I send a "GET" request to "/network_path_dummies/1"
68+
And the JSON should be equal to:
69+
"""
70+
{
71+
"_links": {
72+
"self": {
73+
"href": "//example.com/network_path_dummies/1"
74+
},
75+
"networkPathRelationDummy": {
76+
"href": "//example.com/network_path_relation_dummies/1"
77+
}
78+
},
79+
"id": 1
80+
}
81+
"""
82+
83+
Scenario: I should be able to GET subresources with network paths
84+
Given I add "Accept" header equal to "application/hal+json"
85+
And I send a "GET" request to "/network_path_relation_dummies/1/network_path_dummies"
86+
And the JSON should be equal to:
87+
"""
88+
{
89+
"_links": {
90+
"self": {
91+
"href": "//example.com/network_path_relation_dummies/1/network_path_dummies"
92+
},
93+
"item": [
94+
{
95+
"href": "//example.com/network_path_dummies/1"
96+
}
97+
]
98+
},
99+
"totalItems": 1,
100+
"itemsPerPage": 3,
101+
"_embedded": {
102+
"item": [
103+
{
104+
"_links": {
105+
"self": {
106+
"href": "//example.com/network_path_dummies/1"
107+
},
108+
"networkPathRelationDummy": {
109+
"href": "//example.com/network_path_relation_dummies/1"
110+
}
111+
},
112+
"id": 1
113+
}
114+
]
115+
}
116+
}
117+
"""

0 commit comments

Comments
 (0)