5
5
use Psr \Http \Message \ResponseInterface ;
6
6
use RuntimeException ;
7
7
use Tobyz \JsonApiServer \Context ;
8
+ use Tobyz \JsonApiServer \Endpoint \Concerns \BuildsOpenApiPaths ;
8
9
use Tobyz \JsonApiServer \Endpoint \Concerns \SavesData ;
9
10
use Tobyz \JsonApiServer \Endpoint \Concerns \ShowsResources ;
10
11
use Tobyz \JsonApiServer \Exception \ForbiddenException ;
11
12
use Tobyz \JsonApiServer \Exception \MethodNotAllowedException ;
12
- use Tobyz \JsonApiServer \JsonApi ;
13
13
use Tobyz \JsonApiServer \OpenApi \OpenApiPathsProvider ;
14
14
use Tobyz \JsonApiServer \Resource \Collection ;
15
15
use Tobyz \JsonApiServer \Resource \Creatable ;
@@ -26,6 +26,7 @@ class Create implements Endpoint, OpenApiPathsProvider
26
26
use SavesData;
27
27
use ShowsResources;
28
28
use HasDescription;
29
+ use BuildsOpenApiPaths;
29
30
30
31
private array $ afterCallbacks = [];
31
32
@@ -62,7 +63,7 @@ public function handle(Context $context): ?ResponseInterface
62
63
->withResource ($ resource = $ context ->resource ($ data ['type ' ]))
63
64
->withModel ($ model = $ collection ->newModel ($ context ));
64
65
65
- $ this ->assertFieldsValid ($ context , $ data );
66
+ $ this ->assertFieldsValid ($ context , $ data, true );
66
67
$ this ->fillDefaultValues ($ context , $ data );
67
68
$ this ->deserializeValues ($ context , $ data );
68
69
$ this ->assertDataValid ($ context , $ data , true );
@@ -105,54 +106,30 @@ private function fillDefaultValues(Context $context, array &$data): void
105
106
106
107
public function getOpenApiPaths (Collection $ collection ): array
107
108
{
108
- $ resourcesCreate = array_map (
109
- fn ($ resource ) => ['$ref ' => "#/components/schemas/ {$ resource }Create " ],
110
- $ collection ->resources (),
111
- );
112
-
113
- $ resources = array_map (
114
- fn ($ resource ) => ['$ref ' => "#/components/schemas/ $ resource " ],
115
- $ collection ->resources (),
116
- );
117
-
118
109
return [
119
110
"/ {$ collection ->name ()}" => [
120
111
'post ' => [
121
112
'description ' => $ this ->getDescription (),
122
113
'tags ' => [$ collection ->name ()],
123
114
'requestBody ' => [
124
- 'content ' => [
125
- JsonApi::MEDIA_TYPE => [
126
- 'schema ' => [
127
- 'type ' => 'object ' ,
128
- 'required ' => ['data ' ],
129
- 'properties ' => [
130
- 'data ' =>
131
- count ($ resourcesCreate ) === 1
132
- ? $ resourcesCreate [0 ]
133
- : ['oneOf ' => $ resourcesCreate ],
134
- ],
135
- ],
136
- ],
137
- ],
138
115
'required ' => true ,
116
+ 'content ' => $ this ->buildOpenApiContent (
117
+ array_map (
118
+ fn ($ resource ) => [
119
+ '$ref ' => "#/components/schemas/ {$ resource }Create " ,
120
+ ],
121
+ $ collection ->resources (),
122
+ ),
123
+ ),
139
124
],
140
125
'responses ' => [
141
126
'200 ' => [
142
- 'content ' => [
143
- JsonApi::MEDIA_TYPE => [
144
- 'schema ' => [
145
- 'type ' => 'object ' ,
146
- 'required ' => ['data ' ],
147
- 'properties ' => [
148
- 'data ' =>
149
- count ($ resources ) === 1
150
- ? $ resources [0 ]
151
- : ['oneOf ' => $ resources ],
152
- ],
153
- ],
154
- ],
155
- ],
127
+ 'content ' => $ this ->buildOpenApiContent (
128
+ array_map (
129
+ fn ($ resource ) => ['$ref ' => "#/components/schemas/ $ resource " ],
130
+ $ collection ->resources (),
131
+ ),
132
+ ),
156
133
],
157
134
],
158
135
],
0 commit comments