@@ -330,6 +330,93 @@ async def test_select_custom_fields_with_includes(
330
330
),
331
331
}
332
332
333
+ async def test_select_custom_fields_with_includes_other_direction (
334
+ self ,
335
+ app : FastAPI ,
336
+ async_session : AsyncSession ,
337
+ client : AsyncClient ,
338
+ user_1 : User ,
339
+ user_2 : User ,
340
+ ):
341
+ url = app .url_path_for ("get_post_list" )
342
+ user_1 , user_2 = sorted ((user_1 , user_2 ), key = lambda x : x .id )
343
+
344
+ user_2_post = await create_post (async_session , user_2 )
345
+ user_1_post = await create_post (async_session , user_1 )
346
+
347
+ queried_user_fields = "name"
348
+ queried_post_fields = "title"
349
+
350
+ params = QueryParams (
351
+ [
352
+ ("fields[user]" , queried_user_fields ),
353
+ ("fields[post]" , queried_post_fields ),
354
+ ("include" , "user" ),
355
+ ],
356
+ )
357
+ response = await client .get (url , params = f"{ params } " )
358
+
359
+ assert response .status_code == status .HTTP_200_OK , response .text
360
+ response_data = response .json ()
361
+ response_data ["data" ] = sorted (response_data ["data" ], key = lambda x : (x ["type" ], x ["id" ]))
362
+ response_data ["included" ] = sorted (response_data ["included" ], key = lambda x : (x ["type" ], x ["id" ]))
363
+
364
+ assert response_data == {
365
+ "data" : [
366
+ {
367
+ "id" : f"{ user_2_post .id } " ,
368
+ "type" : "post" ,
369
+ "attributes" : PostAttributesBaseSchema .model_validate (user_2_post ).model_dump (
370
+ include = set (queried_post_fields .split ("," ))
371
+ ),
372
+ "relationships" : {
373
+ "user" : {
374
+ "data" : {
375
+ "id" : f"{ user_2 .id } " ,
376
+ "type" : "user"
377
+ }
378
+ }
379
+ }
380
+ },
381
+ {
382
+ "id" : f"{ user_1_post .id } " ,
383
+ "type" : "post" ,
384
+ "attributes" : PostAttributesBaseSchema .model_validate (user_1_post ).model_dump (
385
+ include = set (queried_post_fields .split ("," ))
386
+ ),
387
+ "relationships" : {
388
+ "user" : {
389
+ "data" : {
390
+ "id" : f"{ user_1 .id } " ,
391
+ "type" : "user"
392
+ }
393
+ }
394
+ }
395
+ },
396
+ ],
397
+ "jsonapi" : {"version" : "1.0" },
398
+ "meta" : { "count" : 2 , "totalPages" : 1 },
399
+ "included" : sorted (
400
+ [
401
+ {
402
+ "id" : f"{ user_1 .id } " ,
403
+ "type" : "user" ,
404
+ "attributes" : UserAttributesBaseSchema .model_validate (user_1 ).model_dump (
405
+ include = set (queried_user_fields .split ("," )),
406
+ ),
407
+ },
408
+ {
409
+ "id" : f"{ user_2 .id } " ,
410
+ "type" : "user" ,
411
+ "attributes" : UserAttributesBaseSchema .model_validate (user_2 ).model_dump (
412
+ include = set (queried_user_fields .split ("," )),
413
+ ),
414
+ },
415
+ ],
416
+ key = lambda x : (x ["type" ], x ["id" ]),
417
+ ),
418
+ }
419
+
333
420
async def test_select_custom_fields_for_includes_without_requesting_includes (
334
421
self ,
335
422
app : FastAPI ,
0 commit comments