Skip to content

Commit 1140874

Browse files
committed
Separate update schema
1 parent dbc441c commit 1140874

File tree

2 files changed

+53
-11
lines changed

2 files changed

+53
-11
lines changed

src/Endpoint/Update.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,11 @@ public function handle(Context $context): ?ResponseInterface
7878

7979
public function getOpenApiPaths(Collection $collection): array
8080
{
81+
$resourcesUpdate = array_map(
82+
fn($resource) => ['$ref' => "#/components/schemas/{$resource}Update"],
83+
$collection->resources(),
84+
);
85+
8186
$resources = array_map(
8287
fn($resource) => [
8388
'$ref' => "#/components/schemas/$resource",
@@ -106,9 +111,9 @@ public function getOpenApiPaths(Collection $collection): array
106111
'required' => ['data'],
107112
'properties' => [
108113
'data' =>
109-
count($resources) === 1
110-
? $resources[0]
111-
: ['oneOf' => $resources],
114+
count($resourcesUpdate) === 1
115+
? $resourcesUpdate[0]
116+
: ['oneOf' => $resourcesUpdate],
112117
],
113118
],
114119
],

src/OpenApi/OpenApiGenerator.php

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,34 +45,71 @@ public function generate(JsonApi $api): array
4545

4646
foreach ($api->resources as $resource) {
4747
$schema = ['attributes' => [], 'relationships' => []];
48+
$writableSchema = ['attributes' => [], 'relationships' => []];
4849

4950
foreach ($resource->fields() as $field) {
5051
$schema[location($field)]['properties'][$field->name] = $field->getSchema($api);
52+
$schema[location($field)]['required'][] = $field->name;
5153

52-
if ($field->required) {
53-
$schema[location($field)]['required'][] = $field->name;
54+
if ($field->writable) {
55+
$writableSchema[location($field)]['properties'][
56+
$field->name
57+
] = $field->getSchema($api);
58+
59+
if ($field->required) {
60+
$writableSchema[location($field)]['required'][] = $field->name;
61+
}
5462
}
5563
}
5664

65+
$schemas[$resource->type()] = [
66+
'type' => 'object',
67+
'required' => ['type', 'id'],
68+
'properties' => [
69+
'type' => ['type' => 'string', 'const' => $resource->type()],
70+
'id' => ['type' => 'string', 'readOnly' => true],
71+
...$schema['attributes']
72+
? ['attributes' => ['type' => 'object'] + $schema['attributes']]
73+
: [],
74+
...$schema['relationships']
75+
? ['relationships' => ['type' => 'object'] + $schema['relationships']]
76+
: [],
77+
],
78+
];
79+
5780
$schemas["{$resource->type()}Create"] = [
5881
'type' => 'object',
5982
'required' => ['type'],
6083
'properties' => [
6184
'type' => ['type' => 'string', 'const' => $resource->type()],
6285
'id' => ['type' => 'string'],
63-
'attributes' => ['type' => 'object'] + $schema['attributes'],
64-
'relationships' => ['type' => 'object'] + $schema['relationships'],
86+
...$writableSchema['attributes']
87+
? ['attributes' => ['type' => 'object'] + $writableSchema['attributes']]
88+
: [],
89+
...$writableSchema['relationships']
90+
? [
91+
'relationships' =>
92+
['type' => 'object'] + $writableSchema['relationships'],
93+
]
94+
: [],
6595
],
6696
];
6797

68-
$schemas[$resource->type()] = [
98+
$schemas["{$resource->type()}Update"] = [
6999
'type' => 'object',
70100
'required' => ['type', 'id'],
71101
'properties' => [
72102
'type' => ['type' => 'string', 'const' => $resource->type()],
73-
'id' => ['type' => 'string', 'readOnly' => true],
74-
'attributes' => ['type' => 'object'] + $schema['attributes'],
75-
'relationships' => ['type' => 'object'] + $schema['relationships'],
103+
'id' => ['type' => 'string'],
104+
...$writableSchema['attributes']
105+
? ['attributes' => ['type' => 'object'] + $writableSchema['attributes']]
106+
: [],
107+
...$writableSchema['relationships']
108+
? [
109+
'relationships' =>
110+
['type' => 'object'] + $writableSchema['relationships'],
111+
]
112+
: [],
76113
],
77114
];
78115
}

0 commit comments

Comments
 (0)