Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More clear error when including an entity is not configured correctly for JsonApiBundle #13

Open
drewclauson opened this issue Sep 11, 2015 · 4 comments

Comments

@drewclauson
Copy link
Contributor

I inadvertently tried to serialize an entity that wasn't configure for JsonApiBundle correctly and got the message: "Error: Call to a member function getType() on null" on line 334 of JsonEventSubscriber.php.

Once I configured the entity with the correct decorations, it worked.

It'd be nice to have it return some sort of error that is a little more descriptive as to the issue it's encountered. I'd take a stab at a PR, and I'm willing to, but wanted to see if you thought this would be helpful?

@drewclauson drewclauson changed the title More clear error when including an entity not configured correctly More clear error when including an entity is not configured correctly for JsonApiBundle Sep 11, 2015
@silverqx
Copy link

I think any PR is helpful. More clear error messages are always welcome.

@koemeet
Copy link
Owner

koemeet commented Sep 12, 2015

@drewclauson What was the mapping you had when you encounterd this error? I can't reproduce any weird errors when leaving out some mappings.

@drewclauson
Copy link
Contributor Author

I'll post my actual entities on Monday that cause the error. Thanks!

On Sep 12, 2015, 7:25 AM -0400, Steffen [email protected], wrote:

@drewclauson(https://github.com/drewclauson)What was the mapping you had when you encounterd this error? I can't reproduce any weird errors when leaving out some mappings.


Reply to this email directly orview it on GitHub(#13 (comment)).

@drewclauson
Copy link
Contributor Author

Here is the actual entity, sorry it's pretty long. Note that there aren't any JsonApiBundle decorations.

<?php

namespace REST\InvoiceBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use JMS\Serializer\Annotation\ExclusionPolicy;
use JMS\Serializer\Annotation\Expose;
use Money\Money;
use REST\CommonBundle\Entity\Traits\TimestampAndUserstampTrait;
use REST\CommonBundle\Helpers\MoneyHelper;
use REST\InvoiceBundle\Model\InvoiceInterface;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;

/**
 * Invoice
 *
 * @ORM\Table()
 * @ORM\Entity()
 * @ORM\HasLifecycleCallbacks()
 * @ORM\EntityListeners({"REST\CommonBundle\Listener\InvoiceLegacyListener"})
 * @ORM\Entity(repositoryClass="REST\InvoiceBundle\Repository\InvoiceRepository")
 * @UniqueEntity("invoiceNumber")
 * @ExclusionPolicy("ALL")
 */
class Invoice implements InvoiceInterface
{
    use TimestampAndUserstampTrait;

    /**
     * @var integer
     *
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @Expose()
     */
    private $id;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\OneToMany(targetEntity="REST\BillBundle\Entity\Charge", mappedBy="invoice")
     * @Expose()
     */
    private $charges;


    /**
     *
     * @ORM\PreFlush()
     */
//    public function updateLegacyCustomerInvoiceFields()
//    {
//        foreach($this->charges as $charge) {
//            ChargeSyncLegacyListener::updateInvoiceNumberAndDate($charge);
//        }
//    }

    /**
     * @var \DateTime
     *
     * @ORM\Column(type="date", nullable=true)
     * @Expose()
     */
    private $invoiceDate;
    /**
     * @var integer
     * @ORM\Column(type="bigint", nullable=false)
     * TODO: unique constraints here are not possible because we are using an unsupported version of SQL Server connection via FreeTDS.  Once we move to another DB, need to make this a unique constraint see http://www.doctrine-project.org/jira/browse/DBAL-1068
     * @Expose()
     */
    private $invoiceNumber;
    /**
     * @var \DateTime
     *
     * @ORM\Column(type="datetime", nullable=true)
     * @Expose()
     */
    private $approvedAt;
    /**
     * @var integer
     *
     * @ORM\Column(type="integer", nullable=true)
     */
    private $approvedUser;
    /**
     * @var integer
     *
     * @ORM\Column(type="integer", nullable=false)
     */
    private $chargesTotalAmount;
    /**
     * @var integer
     *
     * @ORM\Column(type="string", nullable=false, length=64)
     */
    private $chargesTotalCurrency;

    public function __construct()
    {
        $this->charges = new ArrayCollection();
    }

    /**
     * @return integer
     */
    public function getInvoiceNumber()
    {
        return $this->invoiceNumber;
    }

    /**
     * @param integer $invoiceNumber
     * @return Invoice
     */
    public function setInvoiceNumber($invoiceNumber)
    {
        $this->invoiceNumber = $invoiceNumber;
    }

    /**
     * @return int
     */
    public function getApprovedUser()
    {
        return $this->approvedUser;
    }

    /**
     * @param int $approvedUser
     * @return Invoice
     */
    public function setApprovedUser($approvedUser)
    {
        $this->approvedUser = $approvedUser;
    }

    /**
     * @return \DateTime
     */
    public function getInvoiceDate()
    {
        return $this->invoiceDate;
    }

    /**
     * @param \DateTime $invoiceDate
     * @return Invoice
     */
    public function setInvoiceDate($invoiceDate)
    {
        $this->invoiceDate = $invoiceDate;
    }

    /**
     * get charges
     *
     * @return \Doctrine\Common\Collections\Collection
     */
    public function getCharges()
    {
        return $this->charges;
    }

    /**
     * set charges
     *
     * @param mixed $charges
     * @return Invoice
     */
    public function setCharges($charges)
    {
        $this->charges = $charges;
    }

    /**
     * Get id
     *
     * @return integer
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * @return \DateTime
     */
    public function getApprovedAt()
    {
        return $this->approvedAt;
    }

    /**
     * @param \DateTime $approvedAt
     * @return Invoice
     */
    public function setApprovedAt($approvedAt)
    {
        $this->approvedAt = $approvedAt;
    }

    /**
     * get total charges (check value to be compared against the sum of charges in Charges table)
     *
     * @return Money
     */
    public function getChargesTotal()
    {
        return MoneyHelper::getterHelper($this->chargesTotalAmount, $this->chargesTotalCurrency);
    }

    /**
     * Set total charges (check value to be compared against the sum of charges in Charges table)
     *
     * @param Money $amount
     * @return Invoice
     *
     */
    public function setChargesTotal(Money $amount)
    {
        $this->chargesTotalAmount = $amount->getAmount();
        $this->chargesTotalCurrency = $amount->getCurrency()->getName();

        return $this;
    }
}

Here is my controller:

/**
     *
     * @QueryParam(name="limit", requirements="\d+", default="15", description="Number of records to return")
     * @QueryParam(name="offset", requirements="\d+", nullable=true, default="0", description="The offset of the records to return")
     *
     * @param Request $request
     * @param ParamFetcherInterface $paramFetcher
     * @return array|\REST\InvoiceBundle\Entity\Invoice[]
     * @param Request $request
     *
     */
    public function getInvoicesAction(Request $request, ParamFetcherInterface $paramFetcher)
    {
        $limit = $paramFetcher->get('limit');
        $offset = $paramFetcher->get('offset');

        $data = $this->getHandler()->all($limit, $offset);

        return $data;
    }

When doing a GET on /invoices, I get this response:

{
  "error": {
    "code": 500,
    "message": "Internal Server Error",
    "exception": [
      {
        "message": "Error: Call to a member function getType() on null",
        "class": "Symfony\Component\Debug\Exception\FatalErrorException",
        "trace": [
          {
            "namespace": "",
            "short_class": "",
            "class": "",
            "type": "",
            "function": "",
            "file": "/var/www/WinStreamAPI/vendor/drewclauson/Json-Api-Bundle/EventListener/Serializer/JsonEventSubscriber.php",
            "line": 336,
            "args": []
          }
        ]
      }
    ]
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants