@@ -45,34 +45,71 @@ public function generate(JsonApi $api): array
45
45
46
46
foreach ($ api ->resources as $ resource ) {
47
47
$ schema = ['attributes ' => [], 'relationships ' => []];
48
+ $ writableSchema = ['attributes ' => [], 'relationships ' => []];
48
49
49
50
foreach ($ resource ->fields () as $ field ) {
50
51
$ schema [location ($ field )]['properties ' ][$ field ->name ] = $ field ->getSchema ($ api );
52
+ $ schema [location ($ field )]['required ' ][] = $ field ->name ;
51
53
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
+ }
54
62
}
55
63
}
56
64
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
+
57
80
$ schemas ["{$ resource ->type ()}Create " ] = [
58
81
'type ' => 'object ' ,
59
82
'required ' => ['type ' ],
60
83
'properties ' => [
61
84
'type ' => ['type ' => 'string ' , 'const ' => $ resource ->type ()],
62
85
'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
+ : [],
65
95
],
66
96
];
67
97
68
- $ schemas [$ resource ->type ()] = [
98
+ $ schemas ["{ $ resource ->type ()} Update " ] = [
69
99
'type ' => 'object ' ,
70
100
'required ' => ['type ' , 'id ' ],
71
101
'properties ' => [
72
102
'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
+ : [],
76
113
],
77
114
];
78
115
}
0 commit comments