【问题标题】:ApiPlatform Annotations normalizationContext not working as expectedApiPlatform Annotations normalizationContext 未按预期工作
【发布时间】:2022-01-19 14:04:28
【问题描述】:

我正在尝试为我的报告实体添加一个路由并为其定义一个自定义控制器。控制器似乎作为断点在其中工作。 不幸的是, normalization_context 组根本不起作用。我正在尝试为正常的“get”和“generic”创建一个组。 当我对 /api/reports/ 进行 GET 调用时,我得到了包含正确数据的响应

{    
  "@id": "/api/reports/16e9e2d1-faf1-4fed-92e2-dd855f9f6848",  
  "@type": "Report",  
  "year": "2022-01-19T00:00:00+01:00"
  "settings": {  
    "@id": "/api/settings/cf062dba-1150-4881-87d8-b510e57e499f",
    "@type": "Settings",
    "baseYear": "2022-01-19T00:00:00+01:00",
    "location": "/api/locations/5f0a3011-87da-4c23-8c43-e9f749856fcf"
  },
}

另一方面,对 api/reports/generic 的调用无法按预期工作。

预期结果:

{    
  "company": "11112222-11aa-4442-8791-dd87e9abb7c2" 
  "year": "2022-01-19T00:00:00+01:00"
  "settings": {  
    "baseYear": "2022-01-19T00:00:00+01:00",
    "location": "/api/locations/5f0a3011-87da-4c23-8c43-e9f749856fcf"
  },
}

当前结果:

{    
  "year": "2022-01-19T00:00:00+01:00",
  "settings": {},
}

报告.php

<?php

namespace App\Entity;

/**
 * @ApiResource(
 *     normalizationContext={"groups"={"rep_read"}},
 *     denormalizationContext={"groups"={"report:write"}},
 *     collectionOperations={
 *          "get",
 *          "generic"={
 *                   "method"="GET",
 *                   "path"="/reports/generic",
 *                   "normalization_context"={"groups"={"rep_read", "generic"}},
 *                   "openapi_context"={
 *                       "summary"="Get a generic report for a company. if none exists, create one"
 *                   },
 *                   "controller"="App\Controller\RouteController::getCompanyReport",
 *          }
 *     }
 * )
 */
class Report
{
    /**
     * @ORM\Column(type="uuid")
     * @Groups({"generic"})
     */
    private $company;

    /**
     * @ORM\Column(type="date")
     * @Groups({"rep_read", "generic"})
     */
    private $year;

    /**
     * @ORM\ManyToOne(targetEntity=Settings::class)
     * @ORM\JoinColumn(nullable=false)
     * @Groups({"rep_read"})
     */
    private $settings;

}

RouteController.php

<?php

namespace App\Controller;

class RouteController extends AbstractController
{

    public function getCompanyReport(Request $request): Response
    {
        $report = $this->repositories['report']->findOneBy(['company' => $company]);
        
        $debug = $request->attributes->get('_api_normalization_context')['groups']; 
        // --> [0 = "rep_read", 1 = "generic"]
        return $this->json([
                'report' => $report
        ], Response::HTTP_OK);
    }
}

我的临时 $debug 变量表明它在技术上知道 context_groups。但由于某种原因,它没有包含在响应中。 我对 groups/normalizationContext 做错了什么?

【问题讨论】:

    标签: php symfony api-platform.com


    【解决方案1】:

    您嵌套的generic normalization_context (!) 是蛇形案例。应该是骆驼案。

    /**
     * @ApiResource(
     *     normalizationContext={"groups"={"rep_read"}},
     *     denormalizationContext={"groups"={"report:write"}},
     *     collectionOperations={
     *          "get",
     *          "generic"={
     *                   "method"="GET",
     *                   "path"="/reports/generic",
     *          ---->>>  "normalization_context"={"groups"={"rep_read", "generic"}},
     *                   "openapi_context"={
     *                       "summary"="Get a generic report for a company. if none exists, create one"
     *                   },
     *                   "controller"="App\Controller\RouteController::getCompanyReport",
     *          }
     *     }
     * )
     */
    

    【讨论】:

    • 感谢您的回复。我暂时避免使用这个结构,但根据docs,它应该是snake_case。至少提供了带有“put”方法上下文的示例。
    猜你喜欢
    • 2021-06-04
    • 2022-01-24
    • 2015-05-11
    • 2020-05-15
    • 2014-10-31
    • 2018-02-12
    • 2014-01-20
    • 2015-01-13
    • 2013-08-01
    相关资源
    最近更新 更多