Skip to content

Commit 9b1e202

Browse files
mab05kantograssiot
authored andcommitted
add ability to return absolute urls instead of relative IRIs
1 parent 3c371aa commit 9b1e202

File tree

19 files changed

+436
-14
lines changed

19 files changed

+436
-14
lines changed

.circleci/config.yml

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ jobs:
337337
name: Run Behat tests
338338
command: |
339339
mkdir -p build/logs/behat build/coverage
340-
for f in $(find features -name '*.feature' -not -path 'features/main/exposed_state.feature' -not -path 'features/elasticsearch/*' -not -path 'features/mongodb/*' | circleci tests split --split-by=timings); do
340+
for f in $(find features -name '*.feature' -not -path 'features/main/exposed_state.feature' -not -path 'features/elasticsearch/*' -not -path 'features/mongodb/*' -not -path 'features/absolute_url/*' | circleci tests split --split-by=timings); do
341341
_f=$(echo "$f" | tr / _)
342342
FEATURE="${_f}" vendor/bin/behat --format=progress --out=std --format=junit --out=build/logs/behat/"${_f}" --profile=default-coverage --no-interaction --colors "$f"
343343
done
@@ -489,6 +489,48 @@ jobs:
489489
file: build/logs/clover.xml
490490
- save-npm-cache
491491

492+
behat-absolute-url-coverage:
493+
executor: php
494+
environment:
495+
APP_ENV: absolute_url
496+
working_directory: ~/api-platform/core
497+
steps:
498+
- checkout
499+
- install-mongodb-php-extension
500+
- install-pcov-php-extension
501+
- disable-xdebug-php-extension
502+
- disable-php-memory-limit
503+
- restore-composer-cache
504+
- update-project-dependencies
505+
- save-composer-cache
506+
- clear-test-app-cache
507+
- run:
508+
name: Run Behat tests
509+
command: |
510+
mkdir -p build/logs/behat
511+
vendor/bin/behat --format=progress --out=std --format=junit --out=build/logs/behat --profile=absoluteUrl --no-interaction --colors
512+
- restore-npm-cache
513+
- merge-test-reports:
514+
dir: build/logs/behat
515+
out: build/logs/behat/junit.xml
516+
- store_test_results:
517+
path: build/logs
518+
- store_artifacts:
519+
path: build/logs/behat/junit.xml
520+
destination: build/logs/behat/junit.xml
521+
- merge-code-coverage-reports:
522+
dir: build/coverage
523+
out: build/logs/clover.xml
524+
- store_artifacts:
525+
path: build/logs/clover.xml
526+
destination: build/logs/clover.xml
527+
- codecov/upload:
528+
file: build/logs/clover.xml
529+
flags: behat_absolute_url
530+
- coveralls/upload:
531+
file: build/logs/clover.xml
532+
- save-npm-cache
533+
492534
workflows:
493535
version: 2
494536
lint:
@@ -502,3 +544,4 @@ workflows:
502544
- phpunit-mongodb-coverage
503545
- behat-mongodb-coverage
504546
- behat-elasticsearch-coverage
547+
- behat-absolute-url-coverage

.travis.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,4 +249,24 @@ jobs:
249249
- *validate-openapi-v3-json
250250
- *validate-openapi-v3-yaml
251251

252+
- php: '7.3'
253+
env: APP_ENV=absolute_url
254+
before_install:
255+
- *enable-mongodb-php-extension
256+
- *disable-xdebug-php-extension
257+
- *disable-php-memory-limit
258+
- *add-composer-bin-dir-to-path
259+
install:
260+
- *update-project-dependencies
261+
before_script:
262+
- *clear-test-app-cache
263+
script:
264+
- *run-phpunit-tests
265+
- *clear-test-app-cache
266+
- vendor/bin/behat --format=progress --profile=absoluteUrl --no-interaction
267+
- *validate-openapi-v2-json
268+
- *validate-openapi-v2-yaml
269+
- *validate-openapi-v3-json
270+
- *validate-openapi-v3-yaml
271+
252272
fast_finish: true

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ Coverage will be available in `coverage/index.html`.
8484
The command to launch Behat tests is:
8585

8686
```shell
87-
./vendor/bin/behat --suite=default --stop-on-failure -vvv
87+
./vendor/bin/behat --profile=default --stop-on-failure -vvv
8888
```
8989

9090
If you want to launch Behat tests for MongoDB, the command is:
9191

9292
```shell
93-
APP_ENV=mongodb ./vendor/bin/behat --suite=mongodb --stop-on-failure -vvv
93+
APP_ENV=mongodb ./vendor/bin/behat --profile=mongodb --stop-on-failure -vvv
9494
```
9595

9696
## Squash your Commits

behat.yml.dist

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ default:
2020
- 'Behat\MinkExtension\Context\MinkContext'
2121
- 'Behatch\Context\RestContext'
2222
filters:
23-
tags: '~@postgres&&~@mongodb&&~@elasticsearch'
23+
tags: '~@postgres&&~@mongodb&&~@elasticsearch&&~@absoluteUrl'
2424
extensions:
2525
'Behat\Symfony2Extension':
2626
kernel:
@@ -65,7 +65,7 @@ postgres:
6565
- 'Behat\MinkExtension\Context\MinkContext'
6666
- 'Behatch\Context\RestContext'
6767
filters:
68-
tags: '~@sqlite&&~@mongodb&&~@elasticsearch'
68+
tags: '~@sqlite&&~@mongodb&&~@elasticsearch&&~@absoluteUrl'
6969

7070
mongodb:
7171
suites:
@@ -90,7 +90,7 @@ mongodb:
9090
- 'Behat\MinkExtension\Context\MinkContext'
9191
- 'Behatch\Context\RestContext'
9292
filters:
93-
tags: '~@sqlite&&~@elasticsearch&&~@!mongodb'
93+
tags: '~@sqlite&&~@elasticsearch&&~@!mongodb&&~@absoluteUrl'
9494

9595
elasticsearch:
9696
suites:
@@ -111,6 +111,22 @@ elasticsearch:
111111
filters:
112112
tags: '@elasticsearch'
113113

114+
absoluteUrl:
115+
suites:
116+
default: false
117+
absoluteUrl:
118+
paths:
119+
- '%paths.base%/features/absolute_url'
120+
contexts:
121+
- 'DoctrineContext':
122+
doctrine: '@doctrine'
123+
passwordEncoder: '@security.password_encoder'
124+
- 'JsonContext'
125+
- 'Behatch\Context\RestContext'
126+
- 'Behat\MinkExtension\Context\MinkContext'
127+
filters:
128+
tags: '@absoluteUrl'
129+
114130
default-coverage:
115131
suites:
116132
default:
@@ -134,7 +150,7 @@ default-coverage:
134150
- 'Behat\MinkExtension\Context\MinkContext'
135151
- 'Behatch\Context\RestContext'
136152
filters:
137-
tags: '~@postgres&&~@mongodb&&~@elasticsearch'
153+
tags: '~@postgres&&~@mongodb&&~@elasticsearch&&~@absoluteUrl'
138154

139155
mongodb-coverage:
140156
suites:
@@ -160,7 +176,7 @@ mongodb-coverage:
160176
- 'Behat\MinkExtension\Context\MinkContext'
161177
- 'Behatch\Context\RestContext'
162178
filters:
163-
tags: '~@sqlite&&~@elasticsearch&&~@!mongodb'
179+
tags: '~@sqlite&&~@elasticsearch&&~@!mongodb&&~@absoluteUrl'
164180

165181
elasticsearch-coverage:
166182
suites:
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
@absoluteUrl
2+
Feature: IRI should contain Absolute URL
3+
In order to add detail to IRIs
4+
Include the absolute url
5+
6+
@createSchema
7+
Scenario: I should be able to GET a collection of Objects with Absolute Urls
8+
Given there are 1 absoluteUrlDummy objects with a related absoluteUrlRelationDummy
9+
And I add "Accept" header equal to "application/ld+json"
10+
And I add "Content-Type" header equal to "application/json"
11+
And I send a "GET" request to "/absolute_url_dummies"
12+
And the JSON should be equal to:
13+
"""
14+
{
15+
"@context": "\/contexts\/AbsoluteUrlDummy",
16+
"@id": "http:\/\/example.com\/absolute_url_dummies",
17+
"@type": "hydra:Collection",
18+
"hydra:member": [
19+
{
20+
"@id": "http:\/\/example.com\/absolute_url_dummies\/1",
21+
"@type": "AbsoluteUrlDummy",
22+
"absoluteUrlRelationDummy": "http:\/\/example.com\/absolute_url_relation_dummies\/1",
23+
"id": 1
24+
}
25+
],
26+
"hydra:totalItems": 1
27+
}
28+
29+
"""
30+
31+
Scenario: I should be able to POST an object using an Absolute Url
32+
Given I add "Accept" header equal to "application/ld+json"
33+
And I add "Content-Type" header equal to "application/json"
34+
And I send a "POST" request to "/absolute_url_relation_dummies" with body:
35+
"""
36+
{
37+
"absolute_url_dummies": "http://example.com/absolute_url_dummies/1"
38+
}
39+
"""
40+
Then the response status code should be 201
41+
And the JSON should be equal to:
42+
"""
43+
{
44+
"@context": "\/contexts\/AbsoluteUrlRelationDummy",
45+
"@id": "http:\/\/example.com\/absolute_url_relation_dummies\/2",
46+
"@type": "AbsoluteUrlRelationDummy",
47+
"absoluteUrlDummies": [],
48+
"id": 2
49+
}
50+
"""
51+
52+
Scenario: I should be able to GET an Item with Absolute Urls
53+
Given I add "Accept" header equal to "application/ld+json"
54+
And I add "Content-Type" header equal to "application/json"
55+
And I send a "GET" request to "/absolute_url_dummies/1"
56+
And print last JSON response
57+
And the JSON should be equal to:
58+
"""
59+
{
60+
"@context": "\/contexts\/AbsoluteUrlDummy",
61+
"@id": "http:\/\/example.com\/absolute_url_dummies\/1",
62+
"@type": "AbsoluteUrlDummy",
63+
"absoluteUrlRelationDummy": "http:\/\/example.com\/absolute_url_relation_dummies\/1",
64+
"id": 1
65+
}
66+
"""
67+
68+
Scenario: I should be able to GET subresources with Absolute Urls
69+
Given I add "Accept" header equal to "application/ld+json"
70+
And I add "Content-Type" header equal to "application/json"
71+
And I send a "GET" request to "/absolute_url_relation_dummies/1/absolute_url_dummies"
72+
And print last JSON response
73+
And the JSON should be equal to:
74+
"""
75+
{
76+
"@context": "\/contexts\/AbsoluteUrlDummy",
77+
"@id": "http:\/\/example.com\/absolute_url_relation_dummies\/1\/absolute_url_dummies",
78+
"@type": "hydra:Collection",
79+
"hydra:member": [
80+
{
81+
"@id": "http:\/\/example.com\/absolute_url_dummies\/1",
82+
"@type": "AbsoluteUrlDummy",
83+
"absoluteUrlRelationDummy": "http:\/\/example.com\/absolute_url_relation_dummies\/1",
84+
"id": 1
85+
}
86+
],
87+
"hydra:totalItems": 1
88+
}
89+
"""

features/bootstrap/DoctrineContext.php

Lines changed: 37 additions & 0 deletions
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;
@@ -65,6 +67,8 @@
6567
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\Taxon as TaxonDocument;
6668
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\ThirdLevel as ThirdLevelDocument;
6769
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Document\User as UserDocument;
70+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\AbsoluteUrlDummy;
71+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\AbsoluteUrlRelationDummy;
6872
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Address;
6973
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\Answer;
7074
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\CompositeItem;
@@ -1502,6 +1506,23 @@ public function thereAreConvertedOwnerObjects(int $nb)
15021506
$this->manager->flush();
15031507
}
15041508

1509+
/**
1510+
* @Given there are :nb absoluteUrlDummy objects with a related absoluteUrlRelationDummy
1511+
*/
1512+
public function thereAreAbsoluteUrlDummies(int $nb)
1513+
{
1514+
for ($i = 1; $i <= $nb; ++$i) {
1515+
$absoluteUrlRelationDummy = $this->buildAbsoluteUrlRelationDummy();
1516+
$absoluteUrlDummy = $this->buildAbsoluteUrlDummy();
1517+
$absoluteUrlDummy->absoluteUrlRelationDummy = $absoluteUrlRelationDummy;
1518+
1519+
$this->manager->persist($absoluteUrlRelationDummy);
1520+
$this->manager->persist($absoluteUrlDummy);
1521+
}
1522+
1523+
$this->manager->flush();
1524+
}
1525+
15051526
private function isOrm(): bool
15061527
{
15071528
return null !== $this->schemaTool;
@@ -1879,4 +1900,20 @@ private function buildConvertedRelated()
18791900
{
18801901
return $this->isOrm() ? new ConvertedRelated() : new ConvertedRelatedDocument();
18811902
}
1903+
1904+
/**
1905+
* @return AbsoluteUrlDummyDocument|AbsoluteUrlDummy
1906+
*/
1907+
private function buildAbsoluteUrlDummy()
1908+
{
1909+
return $this->isOrm() ? new AbsoluteUrlDummy() : new AbsoluteUrlDummyDocument();
1910+
}
1911+
1912+
/**
1913+
* @return AbsoluteUrlRelationDummyDocument|AbsoluteUrlRelationDummy
1914+
*/
1915+
private function buildAbsoluteUrlRelationDummy()
1916+
{
1917+
return $this->isOrm() ? new AbsoluteUrlRelationDummy() : new AbsoluteUrlRelationDummyDocument();
1918+
}
18821919
}

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ private function registerCommonConfiguration(ContainerBuilder $container, array
154154
$container->setParameter('api_platform.title', $config['title']);
155155
$container->setParameter('api_platform.description', $config['description']);
156156
$container->setParameter('api_platform.version', $config['version']);
157+
$container->setParameter('api_platform.absolute_url', $config['absolute_url']);
157158
$container->setParameter('api_platform.show_webby', $config['show_webby']);
158159
$container->setParameter('api_platform.exception_to_status', $config['exception_to_status']);
159160
$container->setParameter('api_platform.formats', $formats);

src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,10 @@ public function getConfigTreeBuilder()
8484
->cannotBeEmpty()
8585
->defaultValue('0.0.0')
8686
->end()
87+
->booleanNode('absolute_url')
88+
->defaultFalse()
89+
->info('If true, IRIs will include absolute url.')
90+
->end()
8791
->booleanNode('show_webby')->defaultTrue()->info('If true, show Webby on the documentation page')->end()
8892
->scalarNode('default_operation_path_resolver')
8993
->defaultValue('api_platform.operation_path_resolver.underscore')

src/Bridge/Symfony/Bundle/Resources/config/api.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@
6565
<argument type="service" id="api_platform.subresource_data_provider" on-invalid="ignore" />
6666
<argument type="service" id="api_platform.identifier.converter" on-invalid="ignore" />
6767
<argument type="service" id="api_platform.resource_class_resolver" />
68+
<argument>%api_platform.absolute_url%</argument>
6869
</service>
6970
<service id="ApiPlatform\Core\Api\IriConverterInterface" alias="api_platform.iri_converter" />
7071

0 commit comments

Comments
 (0)