Skip to content

Commit dfbde56

Browse files
committed
Improve Input/Output with normalizers
1 parent fa279d3 commit dfbde56

File tree

64 files changed

+1407
-319
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1407
-319
lines changed

features/bootstrap/DoctrineContext.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCar;
6060
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyCarColor;
6161
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDate;
62+
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoCustom;
6263
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoNoInput;
6364
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyDtoNoOutput;
6465
use ApiPlatform\Core\Tests\Fixtures\TestBundle\Entity\DummyFriend;
@@ -1182,6 +1183,36 @@ public function thereIsAMaxDepthDummyWithLevelOfDescendants(int $level)
11821183
$this->manager->flush();
11831184
}
11841185

1186+
/**
1187+
* @Given there is a DummyCustomDto
1188+
*/
1189+
public function thereIsADummyCustomDto()
1190+
{
1191+
$dto = new DummyDtoCustom();
1192+
$dto->lorem = 'test';
1193+
$dto->ipsum = '0';
1194+
$this->manager->persist($dto);
1195+
1196+
$this->manager->flush();
1197+
$this->manager->clear();
1198+
}
1199+
1200+
/**
1201+
* @Given there are :nb DummyCustomDto
1202+
*/
1203+
public function thereAreNbDummyCustomDto($nb)
1204+
{
1205+
for ($i = 1; $i <= $nb; ++$i) {
1206+
$dto = new DummyDtoCustom();
1207+
$dto->lorem = 'test';
1208+
$dto->ipsum = (string) $i;
1209+
$this->manager->persist($dto);
1210+
}
1211+
1212+
$this->manager->flush();
1213+
$this->manager->clear();
1214+
}
1215+
11851216
private function isOrm(): bool
11861217
{
11871218
return null !== $this->schemaTool;

features/graphql/mutation.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ Feature: GraphQL mutation support
310310
When I send the following GraphQL request:
311311
"""
312312
mutation {
313-
createDummyDtoNoInput(input: {foo: "A new one", bar: 3, clientMutationId: "myId"}) {
313+
createDummyDtoNoInput(input: {lorem: "A new one", ipsum: 3, clientMutationId: "myId"}) {
314314
clientMutationId
315315
}
316316
}
@@ -323,7 +323,7 @@ Feature: GraphQL mutation support
323323
{
324324
"errors": [
325325
{
326-
"message": "Field \"foo\" is not defined by type createDummyDtoNoInputInput.",
326+
"message": "Field \"lorem\" is not defined by type createDummyDtoNoInputInput.",
327327
"extensions": {
328328
"category": "graphql"
329329
},
@@ -335,14 +335,14 @@ Feature: GraphQL mutation support
335335
]
336336
},
337337
{
338-
"message": "Field \"bar\" is not defined by type createDummyDtoNoInputInput.",
338+
"message": "Field \"ipsum\" is not defined by type createDummyDtoNoInputInput.",
339339
"extensions": {
340340
"category": "graphql"
341341
},
342342
"locations": [
343343
{
344344
"line": 2,
345-
"column": 51
345+
"column": 53
346346
}
347347
]
348348
}

features/jsonapi/related-resouces-inclusion.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,11 +363,11 @@ Feature: JSON API Inclusion of Related Resources
363363
"dummyDate": null,
364364
"dummyBoolean": null,
365365
"embeddedDummy": {
366-
"dummyName": null,
367366
"dummyBoolean": null,
368367
"dummyDate": null,
369368
"dummyFloat": null,
370369
"dummyPrice": null,
370+
"dummyName": null,
371371
"symfony": null
372372
},
373373
"_id": 1,

features/main/input_output.feature

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
@!mongodb
2+
Feature: DTO input and output
3+
In order to use an hypermedia API
4+
As a client software developer
5+
I need to be able to use DTOs on my resources as Input or Output objects.
6+
7+
@createSchema
8+
Scenario: Create a resource with a custom Input.
9+
When I add "Content-Type" header equal to "application/ld+json"
10+
And I send a "POST" request to "/dummy_dto_customs" with body:
11+
"""
12+
{
13+
"foo": "test",
14+
"bar": 1
15+
}
16+
"""
17+
Then the response status code should be 201
18+
And the response should be in JSON
19+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
20+
And the JSON should be equal to:
21+
"""
22+
{
23+
"@context": "/contexts/DummyDtoCustom",
24+
"@id": "/dummy_dto_customs/1",
25+
"@type": "DummyDtoCustom",
26+
"lorem": "test",
27+
"ipsum": "1",
28+
"id": 1
29+
}
30+
"""
31+
32+
@createSchema
33+
Scenario: Get an item with a custom output
34+
Given there is a DummyCustomDto
35+
When I send a "GET" request to "/dummy_dto_custom_output/1"
36+
Then the response status code should be 200
37+
And the response should be in JSON
38+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
39+
And the JSON should be a superset of:
40+
"""
41+
{
42+
"@context": {
43+
"@vocab": "http://example.com/docs.jsonld#",
44+
"hydra": "http://www.w3.org/ns/hydra/core#",
45+
"foo": {
46+
"@type": "@id"
47+
},
48+
"bar": {
49+
"@type": "@id"
50+
}
51+
},
52+
"@type": "CustomOutputDto",
53+
"foo": "test",
54+
"bar": 0
55+
}
56+
"""
57+
58+
@createSchema
59+
Scenario: Get a collection with a custom output
60+
Given there are 2 DummyCustomDto
61+
When I send a "GET" request to "/dummy_dto_custom_output"
62+
Then the response status code should be 200
63+
And the response should be in JSON
64+
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
65+
And the JSON should be a superset of:
66+
"""
67+
{
68+
"@context": "/contexts/DummyDtoCustom",
69+
"@id": "/dummy_dto_customs",
70+
"@type": "hydra:Collection",
71+
"hydra:member": [
72+
{
73+
"foo": "test",
74+
"bar": 1
75+
},
76+
{
77+
"foo": "test",
78+
"bar": 2
79+
}
80+
],
81+
"hydra:totalItems": 2
82+
}
83+
"""
84+
85+
@createSchema
86+
Scenario: Create a DummyCustomDto object without output
87+
When I add "Content-Type" header equal to "application/ld+json"
88+
And I send a "POST" request to "/dummy_dto_custom_post_without_output" with body:
89+
"""
90+
{
91+
"lorem": "test",
92+
"ipsum": "1"
93+
}
94+
"""
95+
Then the response status code should be 201
96+
And the response should be empty
97+
98+
@createSchema
99+
Scenario: Create and update a DummyInputOutput
100+
When I add "Content-Type" header equal to "application/ld+json"
101+
And I send a "POST" request to "/dummy_dto_input_outputs" with body:
102+
"""
103+
{
104+
"foo": "test",
105+
"bar": 1
106+
}
107+
"""
108+
Then the response status code should be 201
109+
And the JSON should be a superset of:
110+
"""
111+
{
112+
"@context": {
113+
"@vocab": "http://example.com/docs.jsonld#",
114+
"hydra": "http://www.w3.org/ns/hydra/core#",
115+
"baz": {
116+
"@type": "@id"
117+
},
118+
"bat": {
119+
"@type": "@id"
120+
}
121+
},
122+
"@type": "OutputDto",
123+
"baz": 1,
124+
"bat": "test"
125+
}
126+
"""
127+
Then I add "Content-Type" header equal to "application/ld+json"
128+
And I send a "PUT" request to "/dummy_dto_input_outputs/1" with body:
129+
"""
130+
{
131+
"foo": "test",
132+
"bar": 2
133+
}
134+
"""
135+
Then the response status code should be 200
136+
And the JSON should be a superset of:
137+
"""
138+
{
139+
"@context": {
140+
"@vocab": "http:\/\/example.com\/docs.jsonld#",
141+
"hydra": "http:\/\/www.w3.org\/ns\/hydra\/core#",
142+
"baz": {
143+
"@type": "@id"
144+
},
145+
"bat": {
146+
"@type": "@id"
147+
}
148+
},
149+
"@type": "OutputDto",
150+
"baz": 2,
151+
"bat": "test"
152+
}
153+
"""
154+
155+
@createSchema
156+
Scenario: Use DTO with relations on User
157+
When I add "Content-Type" header equal to "application/ld+json"
158+
And I send a "POST" request to "/users" with body:
159+
"""
160+
{
161+
"username": "soyuka",
162+
"plainPassword": "a real password",
163+
"email": "soyuka@example.com"
164+
}
165+
"""
166+
Then the response status code should be 201
167+
Then I add "Content-Type" header equal to "application/ld+json"
168+
And I send a "PUT" request to "/users/recover/1" with body:
169+
"""
170+
{
171+
"user": "/users/1"
172+
}
173+
"""
174+
Then the response status code should be 200
175+
And the JSON should be a superset of:
176+
"""
177+
{
178+
"@context": {
179+
"@vocab": "http://example.com/docs.jsonld#",
180+
"hydra": "http://www.w3.org/ns/hydra/core#",
181+
"user": {
182+
"@type": "@id"
183+
}
184+
},
185+
"@type": "RecoverPasswordOutput",
186+
"user": {
187+
"@id": "/users/1",
188+
"@type": "User",
189+
"email": "soyuka@example.com",
190+
"fullname": null,
191+
"username": "soyuka"
192+
}
193+
}
194+
"""
195+
196+
# @createSchema
197+
# Scenario: Execute a GraphQL query on DTO
198+
# Given there are 2 DummyCustomDto
199+
# When I send the following GraphQL request:
200+
# """
201+
# {
202+
# dummyDtoCustom(id: "/dummy_dto_customs/1") {
203+
# lorem
204+
# ipsum
205+
# }
206+
# }
207+
# """
208+
# Then the response status code should be 200
209+
# And the response should be in JSON
210+
# And the header "Content-Type" should be equal to "application/json"
211+
# Then print last JSON response
212+
# And the JSON node "data.dummy.id" should be equal to "/dummies/1"
213+
# And the JSON node "data.dummy.name" should be equal to "Dummy #1"

features/main/table_inheritance.feature

Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -292,34 +292,34 @@ Feature: Table inheritance
292292
}
293293
"""
294294

295-
Scenario: Get the parent interface collection
296-
When I send a "GET" request to "/resource_interfaces"
297-
Then the response status code should be 200
298-
And the response should be in JSON
299-
And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
300-
And the JSON should be valid according to this schema:
301-
"""
302-
{
303-
"type": "object",
304-
"properties": {
305-
"hydra:member": {
306-
"type": "array",
307-
"items": {
308-
"type": "object",
309-
"properties": {
310-
"@type": {
311-
"type": "string",
312-
"pattern": "^ResourceInterface$"
313-
},
314-
"foo": {
315-
"type": "string",
316-
"required": "true"
317-
}
318-
}
319-
},
320-
"minItems": 1
321-
}
322-
},
323-
"required": ["hydra:member"]
324-
}
325-
"""
295+
# Scenario: Get the parent interface collection
296+
# When I send a "GET" request to "/resource_interfaces"
297+
# Then the response status code should be 200
298+
# And the response should be in JSON
299+
# And the header "Content-Type" should be equal to "application/ld+json; charset=utf-8"
300+
# And the JSON should be valid according to this schema:
301+
# """
302+
# {
303+
# "type": "object",
304+
# "properties": {
305+
# "hydra:member": {
306+
# "type": "array",
307+
# "items": {
308+
# "type": "object",
309+
# "properties": {
310+
# "@type": {
311+
# "type": "string",
312+
# "pattern": "^ResourceInterface$"
313+
# },
314+
# "foo": {
315+
# "type": "string",
316+
# "required": "true"
317+
# }
318+
# }
319+
# },
320+
# "minItems": 1
321+
# }
322+
# },
323+
# "required": ["hydra:member"]
324+
# }
325+
# """

0 commit comments

Comments
 (0)