0

Symfony 4 プロジェクトで REST API の end2end テストを作成しています。

PHP 7.4、Swagger アノテーション、nelmio/api-doc-bundle 3.6.1、nelmio/cors-bundle 1.5.6 を使用しています。

これは、メソッドのコントローラー コードです。

/**
 * @Route(path="", name="create", methods={"POST"})
 * @ValidationGroups({"create"})
 *
 * @SWG\Post(
 *     path="/api/professions",
 *     tags={"Endpoint: Professions"},
 *     summary="save a profession",
 *     @SWG\Parameter(
 *         name="ProfessionDto",
 *         required=true,
 *         in="body",
 *         @Model(type=ProfessionDto::class)
 *     ),
 *     @SWG\Response(
 *         response=201,
 *         description="created",
 *     )
 * )
 */
public function add(ProfessionDto $professionDto): CreatedView
{
    $professionDto = $this->professionService->insert($professionDto);

    return $this->created(['profession' => $professionDto]);
}

ProfessionDto は、交換されるデータを定義するオブジェクトです。キーと値のペアのヒープだけでなく、返されたデータに何らかの構造を持たせたいため、プロパティとしてさらにいくつかのオブジェクトが含まれています。

クラス ProfessionDto で、他のオブジェクトに関連するプロパティを定義しました。


namespace App\Api\Dto;

use Nelmio\ApiDocBundle\Annotation\Model;
use Swagger\Annotations as SWG;
use Symfony\Component\Serializer\Annotation\Groups as SerializerGroups;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @SWG\Definition()
 */
class ProfessionDto
{
    use HistoryDto;

    /**
     * @SWG\Property(description="Id", type="integer")
     *
     * @SerializerGroups({"view", "update", "collection"})
     */
    public ?int $id = null;

    /**
     * @Assert\NotBlank(groups={"create"})
     * @Assert\Type("string")
     * @SWG\Property(description="...", type="string")
     */
    public string $name;

    /**
     * @Assert\Type("boolean")
     * @SWG\Property(description="ist aktiviert/deaktiviert", type="boolean")
     */
    public bool $active;

    /**
     * @SWG\Property(
     *     description="...",
     *     type="object",
     *     ref=@Model(type=ProfessionAreaDto::class)
     * )
     *
     * @SerializerGroups({"view", "create", "update"})
     */
    public ?ProfessionAreaDto $professionArea = null;

    /**
     * @SWG\Property(
     *     description="...",
     *     type="object",
     *     ref=@Model(type=ProfessionalActivityDto::class)
     * )
     *
     * @SerializerGroups({"view", "create", "update"})
     */
    public ?ProfessionalActivityDto $professionalActivity = null;

    /**
     * @SWG\Property(
     *     description="...",
     *     type="object",
     *     ref=@Model(type=IntroductionDto::class)
     * )
     *
     * @SerializerGroups({"view", "create", "update"})
     */
    public ?IntroductionDto $introduction = null;

    /**
     * @SWG\Property(
     *     description="...",
     *     type="object",
     *     ref=@Model(type=PerformanceBehaviourDto::class)
     * )
     *
     * @SerializerGroups({"view", "create", "update"})
     */
    public ?PerformanceBehaviourDto $performanceBehaviour = null;
}

郵便配達員またはテストを介してAPIを呼び出し、データをjsonとして渡す場合

{
  "id":null,
  "name":"fancy new data",
  "active":true,
  "professionArea":{
    "id":48,
    "name":"Vitae nulla aperiam aut enim.",
    "active":true,
    "signature":null,
    "signatureTypeId":null,
    "transition":null,
    "infotextCheck":null
  },
  "professionalActivity":{
    "id":null,
    "textMResignationTestimonial":null,
    ...
    "changedBy":null
  },
  "introduction":{
    "id":null,
    "textMResignationTestimonial":null,
    "textMInterimTestimonial":null,
    ...
    "changedOn":null,
    "changedBy":null
  },
  "performanceBehaviour":{
    "id":null,
    "textMResignationGrade1":null,
    "textMInterimGrade1":null,
    ...
    "changedOn":null,
    "changedBy":null
  },
  "createdOn":null,
  "createdBy":null,
  "changedOn":null,
  "changedBy":null
}

次のエラー メッセージが表示されます: TypeError: Typed property App\Api\Dto\ProfessionDto::$professionArea must be an instance of App\Api\Dto\ProfessionAreaDto or null, array used

私は何を間違えましたか?期待しすぎですか?オブジェクト内のオブジェクトは不可能ですか?

4

1 に答える 1