1

I have the following JSON as input to my custom API. But I can't set interface to get this done.

{
    "locations": [
        {
            "location_id": 1000,
            "location_name": "XYZ"
        },
        {
            "location_id": 1001,
            "location_name": "XYZ1"
        }
    ]
}

I'm still getting the following error message:

Message: Property "0" does not have corresponding setter in class "Magecart\Productapi\Api\Data\LocationsInterface"

Please point me to where am doing wrong.

I tried following:


app\code\Test\Productapi\etc\webapi.xml

<route url="/V1/location" method="POST">
    <service class="Magecart\Productapi\Api\ProductapiInterface" method="postLocation"/>
    <resources>
        <resource ref="anonymous"/>
    </resources>
</route>

app\code\Magecart\Productapi\etc\di.xml

<preference for="Magecart\Productapi\Api\ProductapiInterface" type="Magecart\Productapi\Model\Productapi" />
<preference for="Magecart\Productapi\Api\Data\LocationInterface" type="Magecart\Productapi\Model\Location" />
<preference for="Magecart\Productapi\Api\Data\LocationsInterface" type="Magecart\Productapi\Model\Locations" />

app\code\Magecart\Productapi\Api\ProductapiInterface.php

namespace Magecart\Productapi\Api;

use Magecart\Productapi\Api\Data\LocationInterface;
use Magecart\Productapi\Api\Data\LocationsInterface;

interface ProductapiInterface
{
    /**
     * get locations.
     *
     * @param \Magecart\Productapi\Api\Data\LocationsInterface[] $locations
     * @return \Magecart\Productapi\Api\Data\LocationsInterface
     */
    public function postLocation(LocationsInterface[] $locations);
}

app\code\Magecart\Productapi\Model\Productapi.php

namespace Magecart\Productapi\Model;

use Magecart\Productapi\Api\ProductapiInterface;

class Productapi implements ProductapiInterface
{
    public function postLocation(\Magecart\Productapi\Api\Data\LocationsInterface $location) {
        //code
    }
}

app\code\Magecart\Productapi\Api\Data\LocationsInterface.php

namespace Magecart\Productapi\Api\Data;
/**
 * @api
 */
interface LocationsInterface
{
    /**
     * Gets list location
     *
     * @return \Magecart\Productapi\Api\Data\LocationInterface|null
     */
    public function getLocations();

    /**
     * Sets list location
     *
     * @param \Magecart\Productapi\Api\Data\LocationInterface $locations
     * @return $this
     */
    public function setLocations($locations = null);
}

app\code\Magecart\Productapi\Model\Locations.php

namespace Magecart\Productapi\Model;

use Magecart\Productapi\Api\Data\LocationsInterface;

class Locations implements LocationsInterface
{
    /**
     * Gets list of product location
     *
     * @return \Magecart\Productapi\Api\Data\LocationInterface|null
     */
    public function getLocations()
    {
        return $this;
    }

    /**
     * Sets list of product location
     *
     * @param \Magecart\Productapi\Api\Data\LocationInterface[] $location
     * @return $this
     */
    public function setLocations($location = null)
    {
        return $location;
    }
}

app\code\Magecart\Productapi\Api\Data\LocationInterface.php

namespace Magecart\Productapi\Api\Data;
/**
 * @api
 */
interface LocationInterface
{
    /**
     * Get location_id.
     *
     * @return int|null
     */
    public function getLocationId();

    /**
     * Set location_id.
     *
     * @param int $locationId
     * @return $this
     */
    public function setLocationId($locationId);

    /**
     * Get location_name.
     *
     * @return string|null
     */
    public function getLocationName();

    /**
     * Set location_name.
     *
     * @param string $locationName
     * @return $this
     */
    public function setLocationName($locationName);
}

app\code\Magecart\Productapi\Model\Location.php

namespace Magecart\Productapi\Model;

use Magecart\Productapi\Api\Data\LocationInterface;

class Location implements LocationInterface
{
    private $locationId;
    private $locationName;

    public function __construct() {
        $this->locationId = 0;
        $this->locationName = '';
    }

    /**
     * Get the locationId.
     *
     * @api
     * @return int The locationId.
     */
    public function getLocationId() {
        return $this->locationId;
    }

    /**
     * Set the locationId.
     *
     * @api
     * @param $value int The new locationId.
     * @return null
     */
    public function setLocationId($value) {
        $this->locationId = $value;
    }

    /**
     * Get the locationName.
     *
     * @api
     * @return string The locationName.
     */
    public function getLocationName() {
        return $this->locationName;
    }

    /**
     * Set the locationName.
     *
     * @api
     * @param $value string The new locationName.
     * @return null
     */
    public function setLocationName($value) {
        $this->locationName = $value;
    }
}
1
  • 3
    I edits the question @Marius Feb 17, 2017 at 10:45

1 Answer 1

2

from your question Property "0" does not have corresponding setter in class "Magecart\Productapi\Api\Data\LocationsInterface"

you cant sent an array at very first object.

fieldname is required.

You need to change your inputs like:

{ "locations":{ "locations": [ { "location_id": 1000, "location_name": "XYZ" }, { "location_id": 1001, "location_name": "XYZ1" } ] } }

Message: Property "0" does not have corresponding setter in class "Magecart\Productapi\Api\Data\LocationsInterface"

then this error will removed.


then have a try

app\code\Test\Productapi\etc\webapi.xml

<?xml version="1.0"?>
<routes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Webapi:etc/webapi.xsd">
    <route url="/V1/location" method="POST">
        <service class="Test\Productapi\Api\ProductapiInterface" method="postLocation"/>
        <resources>
            <resource ref="anonymous"/>
        </resources>
    </route>
</routes>

app\code\Test\Productapi\etc\di.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <preference for="Test\Productapi\Api\ProductapiInterface" type="Test\Productapi\Model\Productapi" />
    <preference for="Test\Productapi\Api\Data\LocationInterface" type="Test\Productapi\Model\Location" />
    <preference for="Test\Productapi\Api\Data\LocationsInterface" type="Test\Productapi\Model\Locations" />
</config>

app\code\Test\Productapi\Api\ProductapiInterface.php

namespace Test\Productapi\Api;
    use Test\Productapi\Api\Data\LocationInterface;
    use Test\Productapi\Api\Data\LocationsInterface;

    interface ProductapiInterface
    {
        /**
         * get locations.
         *
         * @param \Test\Productapi\Api\Data\LocationsInterface $locations
         * @return \Test\Productapi\Api\Data\LocationsInterface
         */
        public function postLocation($locations);
    }

app\code\Test\Productapi\Api\Data\LocationsInterface.php

namespace Test\Productapi\Api\Data;
/**
 * @api
 */
interface LocationsInterface
{
    /**
     * Gets list location
     *
     * @return \Test\Productapi\Api\Data\LocationInterface|null
     */
    public function getLocations();

    /**
     * Sets list location
     *
     * @param \Test\Productapi\Api\Data\LocationInterface[] $locations
     * @return $this
     */
    public function setLocations(array $locations = null);
}

app\code\Test\Productapi\Api\Data\LocationInterface.php

namespace Test\Productapi\Api\Data;
    /**
     * @api
     */
    interface LocationInterface
    {
        /**
         * Get location_id.
         *
         * @return int|null
         */
        public function getLocationId();

        /**
         * Set location_id.
         *
         * @param int $locationId
         * @return $this
         */
        public function setLocationId($locationId);

        /**
         * Get location_name.
         *
         * @return string|null
         */
        public function getLocationName();

        /**
         * Set location_name.
         *
         * @param string $locationName
         * @return $this
         */
        public function setLocationName($locationName);
    }

app\code\Test\Productapi\Model\Productapi.php

namespace Test\Productapi\Model;

use Test\Productapi\Api\ProductapiInterface;

class Productapi implements ProductapiInterface
{
    public function postLocation($location) {
        var_dump($location->getLocations());
        //your results will get
        //code
    }
}

app\code\Test\Productapi\Model\Locations.php

namespace Test\Productapi\Model;

use Test\Productapi\Api\Data\LocationsInterface;

class Locations implements LocationsInterface
{
   private $locations;

    /**
     * Gets list of product location
     *
     * @return \Test\Productapi\Api\Data\LocationInterface|null
     */
    public function getLocations()
    {
        return $this->locations;
    }

    /**
     * Sets list of product location
     *
     * @param \Test\Productapi\Api\Data\LocationInterface[] $location
     * @return $this
     */
    public function setLocations(array $location = null)
    {
        $this->locations = $locations;
    }
}

app\code\Test\Productapi\Model\Location.php

namespace Test\Productapi\Model;

use Test\Productapi\Api\Data\LocationInterface;

class Location implements LocationInterface
{
    private $locationId;
    private $locationName;

    public function __construct() {
        $this->locationId = 0;
        $this->locationName = '';
    }

    /**
     * Get the locationId.
     *
     * @api
     * @return int The locationId.
     */
    public function getLocationId() {
        return $this->locationId;
    }

    /**
     * Set the locationId.
     *
     * @api
     * @param $value int The new locationId.
     * @return null
     */
    public function setLocationId($value) {
        $this->locationId = $value;
    }

    /**
     * Get the locationName.
     *
     * @api
     * @return string The locationName.
     */
    public function getLocationName() {
        return $this->locationName;
    }

    /**
     * Set the locationName.
     *
     * @api
     * @param $value string The new locationName.
     * @return null
     */
    public function setLocationName($value) {
        $this->locationName = $value;
    }
}

Note: updated few files, i hope you this will help to acheive your needs.

thanks

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.