10
10
use Tobyz \JsonApiServer \Endpoint \Concerns \ShowsResources ;
11
11
use Tobyz \JsonApiServer \Exception \ForbiddenException ;
12
12
use Tobyz \JsonApiServer \Exception \MethodNotAllowedException ;
13
+ use Tobyz \JsonApiServer \JsonApi ;
14
+ use Tobyz \JsonApiServer \OpenApi \OpenApiPathsProvider ;
15
+ use Tobyz \JsonApiServer \Resource \Collection ;
13
16
use Tobyz \JsonApiServer \Resource \Updatable ;
17
+ use Tobyz \JsonApiServer \Schema \Concerns \HasDescription ;
14
18
use Tobyz \JsonApiServer \Schema \Concerns \HasVisibility ;
15
19
16
20
use function Tobyz \JsonApiServer \json_api_response ;
17
21
18
- class Update implements Endpoint
22
+ class Update implements Endpoint, OpenApiPathsProvider
19
23
{
20
24
use HasVisibility;
21
25
use FindsResources;
22
26
use SavesData;
23
27
use ShowsResources;
28
+ use HasDescription;
24
29
25
30
public static function make (): static
26
31
{
@@ -41,17 +46,19 @@ public function handle(Context $context): ?ResponseInterface
41
46
42
47
$ model = $ this ->findResource ($ context , $ segments [1 ]);
43
48
44
- $ context = $ context ->withResource (
45
- $ resource = $ context ->resource ($ context ->collection ->resource ($ model , $ context )),
46
- );
49
+ $ context = $ context
50
+ ->withModel ($ model )
51
+ ->withResource (
52
+ $ resource = $ context ->resource ($ context ->collection ->resource ($ model , $ context )),
53
+ );
47
54
48
55
if (!$ resource instanceof Updatable) {
49
56
throw new RuntimeException (
50
57
sprintf ('%s must implement %s ' , get_class ($ resource ), Updatable::class),
51
58
);
52
59
}
53
60
54
- if (!$ this ->isVisible ($ context = $ context -> withModel ( $ model ) )) {
61
+ if (!$ this ->isVisible ($ context )) {
55
62
throw new ForbiddenException ();
56
63
}
57
64
@@ -68,4 +75,66 @@ public function handle(Context $context): ?ResponseInterface
68
75
69
76
return json_api_response ($ this ->showResource ($ context , $ model ));
70
77
}
78
+
79
+ public function getOpenApiPaths (Collection $ collection ): array
80
+ {
81
+ $ resources = array_map (
82
+ fn ($ resource ) => [
83
+ '$ref ' => "#/components/schemas/ $ resource " ,
84
+ ],
85
+ $ collection ->resources (),
86
+ );
87
+
88
+ return [
89
+ "/ {$ collection ->name ()}/{id} " => [
90
+ 'patch ' => [
91
+ 'description ' => $ this ->getDescription (),
92
+ 'tags ' => [$ collection ->name ()],
93
+ 'parameters ' => [
94
+ [
95
+ 'name ' => 'id ' ,
96
+ 'in ' => 'path ' ,
97
+ 'required ' => true ,
98
+ 'schema ' => ['type ' => 'string ' ],
99
+ ],
100
+ ],
101
+ 'requestBody ' => [
102
+ 'content ' => [
103
+ JsonApi::MEDIA_TYPE => [
104
+ 'schema ' => [
105
+ 'type ' => 'object ' ,
106
+ 'required ' => ['data ' ],
107
+ 'properties ' => [
108
+ 'data ' =>
109
+ count ($ resources ) === 1
110
+ ? $ resources [0 ]
111
+ : ['oneOf ' => $ resources ],
112
+ ],
113
+ ],
114
+ ],
115
+ ],
116
+ 'required ' => true ,
117
+ ],
118
+ 'responses ' => [
119
+ '200 ' => [
120
+ 'content ' => [
121
+ JsonApi::MEDIA_TYPE => [
122
+ 'schema ' => [
123
+ 'type ' => 'object ' ,
124
+ 'required ' => ['data ' ],
125
+ 'properties ' => [
126
+ 'data ' =>
127
+ count ($ resources ) === 1
128
+ ? $ resources [0 ]
129
+ : ['oneOf ' => $ resources ],
130
+ ],
131
+ ],
132
+ ],
133
+ ],
134
+ ],
135
+ ],
136
+ ],
137
+ ],
138
+ ];
139
+ }
71
140
}
0 commit comments