question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Array of Instances in Annotations

See original GitHub issue

I have two objects.

Order and OrderItems. One order contains mutliple orderItems.

How can I define this relation without using doctrine (defining a ManyToOne relation), that swagger graphs the right attributes?

My Order entity:

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource(
 *     collectionOperations={
 *         "post"={
 *              "method"="POST",
 *              "path"="/orders/check",
 *              "controller"=App\Controller\Order\CheckOrderController::class,
 *              "denormalization_context"={"groups"={"check_order"}}
 *         }
 *     },
 *     itemOperations={}
 * )
 */
class Order
{
    /**
     * @var float
     * @Groups({"check_order"})
     */
    private $price;

    /**
     * @var OrderItem[] <-- does not work
     * @Groups({"check_order"})
     */
    private $orderItems = [];

    // ...
}
<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiResource;
use Symfony\Component\Serializer\Annotation\Groups;

/**
 * @ApiResource()
 */
class OrderItem
{
    /**
     * @var string
     * @Groups({"check_order"})
     */
    private $productId;

    /**
     * @var string
     * @Groups({"check_order"})
     */
    private $title;

    // ...
}

Here the swagger preview: swagger

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
dhernandezcommented, Jan 9, 2019

I found a workaround that works in my case using swagger_context.

Lodging Entity:

<?php
namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;

/**
 * Class Lodging
 * @package App\Entity
 *
 * @ApiResource()
 */
class Lodging
{
    /**
     * @var string
     *
     * @ApiProperty(identifier=true)
     */
    public $id;

    /**
     * @var string[]
     *
     * @ApiProperty()
     */
    public $channels;

    /**
     * @var string
     *
     * @ApiProperty()
     */
    public $name;

    /**
     * @var \App\Entity\Manager[]
     *
     * @ApiProperty(
     *     attributes={
     *         "swagger_context"={"type"="array", "items"={"$ref"="#/definitions/Manager"}}
     *     }
     * )
     */
    public $manager;
}

Manager Entity:

<?php

namespace App\Entity;

use ApiPlatform\Core\Annotation\ApiProperty;
use ApiPlatform\Core\Annotation\ApiResource;

/**
 * Class Manager
 * @package App\Entity
 *
 * @ApiResource()
 */
class Manager
{
    /**
     * @var string
     *
     * @ApiProperty(identifier=true)
     */
    public $id;

    /**
     * @var string
     *
     * @ApiProperty()
     */
    public $name;

    /**
     * @var int
     *
     * @ApiProperty
     */
    public $sampleInteger;
} 

Result: result

0reactions
dhernandezcommented, Jan 9, 2019

Without Doctrine it doesn’t work, I’ve just tried it. Thank you anyway.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Java Annotation - array of Objects or toString values
I need to write an Annotation to exclude certain values from a result set. Background: Distinct values are selected from fields and are ......
Read more >
How to Add Type and Repeating Annotations to Code in Java 8
Notice the @Repeatable annotation indicates repeating instances of musical instrument can be stored in a collection (e.g., an array) returned by the value ......
Read more >
How to Create Array of Objects in Java? - GeeksforGeeks
An Array of Objects is created using the Object class, and we know Object class is the root class of all Classes. We...
Read more >
Allow specifying array annotation attribute single value without ...
Kotlin should allow specifying annotation attribute single value without arrayOf() . I thought initially that Kotlin did not handle that use case like...
Read more >
Array types - Documentation - Psalm
In Psalm this annotation is equivalent to @psalm-return array<array-key, ... the key offsets are known: array shapes, also known as "object-like arrays".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found