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

Unset a reference field of an existing record triggers a 400 - Bad request #153

Closed
bakulf opened this issue Feb 24, 2019 · 0 comments
Closed

Comments

@bakulf
Copy link
Contributor

bakulf commented Feb 24, 2019

If there is class with an optional ManyToOne property, setting the corresponding field of an existing record to null/0/empty returns a 400 - Bad Request, with error message: ""Expected IRI or nested document for attribute \u0022foobar\u0022, \u0022string\u0022 given."

This happens because the admin panel sends an empty string instead of a null value.

STR:

  1. 2 classes:
class A {
  /**
   * @ORM\Id
   * @ORM\GeneratedValue
   * @ORM\Column(type="integer")
   */
  private $id; 
  /**
   * @ORM\ManyToOne(targetEntity="B", inversedBy="foos")
   */
    public $foobar; 
    public function getId(): int { return $this->id; }
}
class B
{
  /**
   * @ORM\Id
   * @ORM\GeneratedValue
   * @ORM\Column(type="integer")
   */                                                                                                          
   private $id;                                                                                                             
   /**
    * @ORM\OneToMany(targetEntity="A", mappedBy="foobar")
    */                                                                                                          
   public $foos;                                                                                                              
   public function __construct() { $this->foos = new ArrayCollection(); }                                                                                                           
   public function getId(): int { return $this->id; }                                       
}
  1. create a record in B:
    curl 'http://localhost:8000/api/bs' --data '{}' -H 'content-type: application/ld+json'
  2. create a record in A referring to the B's record:
    curl 'http://localhost:8000/api/as' --data '{"foobar":"/api/bs/XXX"}' -H 'content-type: application/ld+json'
  3. update A's record from the admin panel:
    Using the admin, deselect the field 'foobar' of the record /api/as/YYY and submit.

Expected results:
the A's record should have an empty 'foobar' relationship with class B.

Actual results:
400 Bad Request.

I suspect that the bug is in convertReactAdminDataToHydraData:
https://github.com/api-platform/admin/blob/master/src/hydra/hydraClient.js#L123-L128

We should normalize the empty string value to a null.
Here what I wrote to fix the issue locally:

    resource.fields.forEach(function (_ref2) {
      var name = _ref2.name,
          normalizeData = _ref2.normalizeData;

      if (!(name in data)) {
        return;
      }

      if (_ref2.reference && data[name] === "") {
        data[name] = null;
      }

      if (undefined === normalizeData) {
        return;
      }

      fieldData[name] = normalizeData(data[name]);
    });
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

2 participants