@@ -269,17 +269,46 @@ class ContentHandler {
269269 * @return array This ContentHandler as an OpenAPI 'content' schema for this MIME type in a given Response object.
270270 */
271271 public function to_openapi_schema (Response $ response , Endpoint $ endpoint ): array {
272- # Format the data schema based on the endpoint for successful responses
273- if ($ response instanceof Success and $ endpoint ->many ) {
272+ # Default to an empty schema in the event we cannot determine the schema
273+ $ data_schema = [
274+ 'oneOf ' => [['type ' => 'array ' , 'items ' => ['type ' => 'object ' ]], ['type ' => 'object ' ]],
275+ ];
276+
277+ # Ensure the 'id' field is included for success responses on many models without a parent
278+ if ($ response instanceof Success and $ endpoint ->model ->many and !$ endpoint ->model ->parent_model_class ) {
274279 $ data_schema = [
275- 'type ' => 'array ' ,
276- 'items ' => ['$ref ' => "#/components/schemas/ {$ endpoint ->model ->get_class_shortname ()}" ],
280+ 'allOf ' => [
281+ ['type ' => 'object ' , 'properties ' => ['id ' => ['type ' => $ endpoint ->model ->id_type ]]],
282+ ['$ref ' => "#/components/schemas/ {$ endpoint ->model ->get_class_shortname ()}" ],
283+ ],
277284 ];
278- } elseif ($ response instanceof Success and !$ endpoint ->many ) {
285+ }
286+ # Ensure the 'parent_id' and 'id' fields are included for success responses on many models with a parent
287+ elseif ($ response instanceof Success and $ endpoint ->model ->many and $ endpoint ->model ->parent_model_class ) {
288+ $ parent_type = $ endpoint ->model ->get_parent_model ()->id_type ;
289+ $ data_schema = [
290+ 'allOf ' => [
291+ [
292+ 'type ' => 'object ' ,
293+ 'properties ' => [
294+ 'parent_id ' => ['type ' => $ parent_type ],
295+ 'id ' => ['type ' => $ endpoint ->model ->id_type ],
296+ ],
297+ ],
298+ ['$ref ' => "#/components/schemas/ {$ endpoint ->model ->get_class_shortname ()}" ],
299+ ],
300+ ];
301+ }
302+ # Do not include any extra fields for non many models
303+ elseif ($ response instanceof Success and !$ endpoint ->model ->many ) {
279304 $ data_schema = ['$ref ' => "#/components/schemas/ {$ endpoint ->model ->get_class_shortname ()}" ];
280- } else {
305+ }
306+
307+ # If this is a many endpoint, nest the data schema in an array
308+ if ($ response instanceof Success and $ endpoint ->many ) {
281309 $ data_schema = [
282- 'oneOf ' => [['type ' => 'array ' , 'items ' => ['type ' => 'object ' ]], ['type ' => 'object ' ]],
310+ 'type ' => 'array ' ,
311+ 'items ' => $ data_schema ,
283312 ];
284313 }
285314
0 commit comments