55use Psr \Http \Message \ResponseInterface ;
66use RuntimeException ;
77use Tobyz \JsonApiServer \Context ;
8+ use Tobyz \JsonApiServer \Endpoint \Concerns \BuildsOpenApiPaths ;
89use Tobyz \JsonApiServer \Endpoint \Concerns \SavesData ;
910use Tobyz \JsonApiServer \Endpoint \Concerns \ShowsResources ;
1011use Tobyz \JsonApiServer \Exception \ForbiddenException ;
1112use Tobyz \JsonApiServer \Exception \MethodNotAllowedException ;
12- use Tobyz \JsonApiServer \JsonApi ;
1313use Tobyz \JsonApiServer \OpenApi \OpenApiPathsProvider ;
1414use Tobyz \JsonApiServer \Resource \Collection ;
1515use Tobyz \JsonApiServer \Resource \Creatable ;
@@ -26,6 +26,7 @@ class Create implements Endpoint, OpenApiPathsProvider
2626 use SavesData;
2727 use ShowsResources;
2828 use HasDescription;
29+ use BuildsOpenApiPaths;
2930
3031 private array $ afterCallbacks = [];
3132
@@ -62,7 +63,7 @@ public function handle(Context $context): ?ResponseInterface
6263 ->withResource ($ resource = $ context ->resource ($ data ['type ' ]))
6364 ->withModel ($ model = $ collection ->newModel ($ context ));
6465
65- $ this ->assertFieldsValid ($ context , $ data );
66+ $ this ->assertFieldsValid ($ context , $ data, true );
6667 $ this ->fillDefaultValues ($ context , $ data );
6768 $ this ->deserializeValues ($ context , $ data );
6869 $ this ->assertDataValid ($ context , $ data , true );
@@ -105,54 +106,30 @@ private function fillDefaultValues(Context $context, array &$data): void
105106
106107 public function getOpenApiPaths (Collection $ collection ): array
107108 {
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-
118109 return [
119110 "/ {$ collection ->name ()}" => [
120111 'post ' => [
121112 'description ' => $ this ->getDescription (),
122113 'tags ' => [$ collection ->name ()],
123114 '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- ],
138115 'required ' => true ,
116+ 'content ' => $ this ->buildOpenApiContent (
117+ array_map (
118+ fn ($ resource ) => [
119+ '$ref ' => "#/components/schemas/ {$ resource }Create " ,
120+ ],
121+ $ collection ->resources (),
122+ ),
123+ ),
139124 ],
140125 'responses ' => [
141126 '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+ ),
156133 ],
157134 ],
158135 ],
0 commit comments