api-platform.com Unable to generate an IRI for the item of type


api-platform.com Unable to generate an IRI for the item of type



I'm trying to setup my first api-platform instance and have run into some presumably noob questions around IDE. What i'm trying to do is setting up an register and reset entity in order to deal with registrations and password reminders from an app. And then have the entity's passed to a custom controller to do some checks, balances and mails afterwards (haven’t gotten around to that yet). But I keep running into this error when posting to my custom entities:


{
"@context": "/api/contexts/Error",
"@type": "hydra:Error",
"hydra:title": "An error occurred",
"hydra:description": "Unable to generate an IRI for the item of type "AppXXXUserBundleEntityRegister"",
"trace": [
{
"namespace": "",
"short_class": "",
"class": "",
"type": "",
"function": "",
"file": "/var/www/webservice/vendor/api-platform/core/src/Bridge/Symfony/Routing/IriConverter.php",
"line": 116,
"args":
},
{
"namespace": "ApiPlatformCoreBridgeSymfonyRouting",
"short_class": "IriConverter",
"class": "ApiPlatformCoreBridgeSymfonyRoutingIriConverter",
"type": "->",
"function": "getIriFromItem",
"file": "/var/www/webservice/vendor/api-platform/core/src/JsonLd/Serializer/ItemNormalizer.php",
"line": 71,
"args": [
[
"object",
"AppXXXUserBundleEntityRegister"
]
]
},
{
"namespace": "ApiPlatformCoreJsonLdSerializer",
"short_class": "ItemNormalizer",
"class": "ApiPlatformCoreJsonLdSerializerItemNormalizer",
"type": "->",
"function": "normalize",
"file": "/var/www/webservice/vendor/symfony/serializer/Serializer.php",
"line": 138,
"args": [
[
"object",
"AppXXXUserBundleEntityRegister"
],
[
"string",
"jsonld"
],
[
"array",
{
"collection_operation_name": [
"string",
"post"
],
"operation_type": [
"string",
"collection"
],
"resource_class": [
"string",
"AppXXXUserBundleEntityRegister"
],
"request_uri": [
"string",
"/api/register"
],
"uri": [
"string",
"http://webservice.local/api/register"
],
"resources": [
"object",
"ApiPlatformCoreSerializerResourceList"
],
"jsonld_has_context": [
"boolean",
true
]
}
]
]
},
{
"namespace": "SymfonyComponentSerializer",
"short_class": "Serializer",
"class": "SymfonyComponentSerializerSerializer",
"type": "->",
"function": "normalize",
"file": "/var/www/webservice/vendor/symfony/serializer/Serializer.php",
"line": 111,
"args": [
[
"object",
"AppXXXUserBundleEntityRegister"
],
[
"string",
"jsonld"
],
[
"array",
{
"collection_operation_name": [
"string",
"post"
],
"operation_type": [
"string",
"collection"
],
"resource_class": [
"string",
"AppXXXUserBundleEntityRegister"
],
"request_uri": [
"string",
"/api/register"
],
"uri": [
"string",
"http://webservice.local/api/register"
],
"resources": [
"object",
"ApiPlatformCoreSerializerResourceList"
]
}
]
]
},
{
"namespace": "SymfonyComponentSerializer",
"short_class": "Serializer",
"class": "SymfonyComponentSerializerSerializer",
"type": "->",
"function": "serialize",
"file": "/var/www/webservice/vendor/api-platform/core/src/EventListener/SerializeListener.php",
"line": 67,
"args": [
[
"object",
"AppXXXUserBundleEntityRegister"
],
[
"string",
"jsonld"
],
[
"array",
{
"collection_operation_name": [
"string",
"post"
],
"operation_type": [
"string",
"collection"
],
"resource_class": [
"string",
"AppXXXUserBundleEntityRegister"
],
"request_uri": [
"string",
"/api/register"
],
"uri": [
"string",
"http://webservice.local/api/register"
],
"resources": [
"object",
"ApiPlatformCoreSerializerResourceList"
]
}
]
]
},
{
"namespace": "ApiPlatformCoreEventListener",
"short_class": "SerializeListener",
"class": "ApiPlatformCoreEventListenerSerializeListener",
"type": "->",
"function": "onKernelView",
"file": "/var/www/webservice/vendor/symfony/event-dispatcher/EventDispatcher.php",
"line": 212,
"args": [
[
"object",
"SymfonyComponentHttpKernelEventGetResponseForControllerResultEvent"
],
[
"string",
"kernel.view"
],
[
"object",
"SymfonyComponentEventDispatcherEventDispatcher"
]
]
},
{
"namespace": "SymfonyComponentEventDispatcher",
"short_class": "EventDispatcher",
"class": "SymfonyComponentEventDispatcherEventDispatcher",
"type": "->",
"function": "doDispatch",
"file": "/var/www/webservice/vendor/symfony/event-dispatcher/EventDispatcher.php",
"line": 44,
"args": [
[
"array",
[
[
"array",
[
[
"object",
"ApiPlatformCoreValidatorEventListenerValidateListener"
],
[
"string",
"onKernelView"
]
]
],
[
"array",
[
[
"object",
"ApiPlatformCoreEventListenerWriteListener"
],
[
"string",
"onKernelView"
]
]
],
[
"array",
[
[
"object",
"ApiPlatformCoreEventListenerSerializeListener"
],
[
"string",
"onKernelView"
]
]
],
[
"array",
[
[
"object",
"ApiPlatformCoreEventListenerRespondListener"
],
[
"string",
"onKernelView"
]
]
]
]
],
[
"string",
"kernel.view"
],
[
"object",
"SymfonyComponentHttpKernelEventGetResponseForControllerResultEvent"
]
]
},
{
"namespace": "SymfonyComponentEventDispatcher",
"short_class": "EventDispatcher",
"class": "SymfonyComponentEventDispatcherEventDispatcher",
"type": "->",
"function": "dispatch",
"file": "/var/www/webservice/vendor/symfony/http-kernel/HttpKernel.php",
"line": 154,
"args": [
[
"string",
"kernel.view"
],
[
"object",
"SymfonyComponentHttpKernelEventGetResponseForControllerResultEvent"
]
]
},
{
"namespace": "SymfonyComponentHttpKernel",
"short_class": "HttpKernel",
"class": "SymfonyComponentHttpKernelHttpKernel",
"type": "->",
"function": "handleRaw",
"file": "/var/www/webservice/vendor/symfony/http-kernel/HttpKernel.php",
"line": 66,
"args": [
[
"object",
"SymfonyComponentHttpFoundationRequest"
],
[
"integer",
1
]
]
},
{
"namespace": "SymfonyComponentHttpKernel",
"short_class": "HttpKernel",
"class": "SymfonyComponentHttpKernelHttpKernel",
"type": "->",
"function": "handle",
"file": "/var/www/webservice/vendor/symfony/http-kernel/Kernel.php",
"line": 188,
"args": [
[
"object",
"SymfonyComponentHttpFoundationRequest"
],
[
"integer",
1
],
[
"boolean",
true
]
]
},
{
"namespace": "SymfonyComponentHttpKernel",
"short_class": "Kernel",
"class": "SymfonyComponentHttpKernelKernel",
"type": "->",
"function": "handle",
"file": "/var/www/webservice/public/index.php",
"line": 37,
"args": [
[
"object",
"SymfonyComponentHttpFoundationRequest"
]
]
}
]
}



I did of course do some research on my own and had a look at both
- Unable to generate an IRI for the item of type
- api platform - Unable to generate an IRI for the item
- Request validator : validate an IRI
- https://github.com/api-platform/core/issues/830



So i've added an id (wich is dont need since this entity is just used for post's), cleared the cash, checked the getters and setters but all to no avail.



The entity currently looks like this


/**
* The register Entity
*
* @ApiResource(
* collectionOperations={
* "post"={"method"="POST", "path"="/register", "controller" = RegisterController::class}
* }
* )
*/

class Register implements Serializable
{
/**
* The unique identifier of this user <br /><b>Schema:</b> <a href="http://schema.org/identifier">https://schema.org/identifier</a>
*
* @var integer
*
* @ApiProperty(iri="https://schema.org/identifier", identifier=true)
*/
protected $id;

/**
* The unique username of this user, used as a security credential alongside the password
*
* @var string
*
*/
protected $username;

/**
* The non-unique e-mail address, used to contact the user on security issues
*
* @var string
*
* @ApiProperty(iri="http://schema.org/email")
*/
protected $email;

/**
* The non-unique password of this user, used as a security credential alongside the e-mail. Passwords are hashed in the database and can therefore never be renderd or viewd
*
* @var string
*
*/
protected $password;

/**
* Serializing the user data that is set into the session
*/
/** @see Serializable::serialize() */
public function serialize()
{
return serialize(array(
$this->id,
$this->username,
$this->password,
$this->email
));
}

/** @see Serializable::unserialize() */
public function unserialize($serialized)
{
list (
$this->id,
$this->username,
$this->password,
$this->email,
) = unserialize($serialized);
}


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

/**
* Get id.
*
* @return User
*/
public function setId($id)
{
$this->id= $id;

return $this;
}

/**
* Set username
*
* @param string $username
*
* @return User
*/
public function setUsername($username)
{
$this->username = $username;

return $this;
}

public function getUsername()
{
return $this->username;
}

/**
* Set password
*
* @param string $password
*
* @return User
*/
public function setPassword($password)
{
$this->password = $password;

return $this;
}

public function getPassword()
{
return $this->password;
}


/**
* Set email.
*
* @param string $email
*
* @return User
*/
public function setEmail($email)
{
$this->email = $email;

return $this;
}

/**
* Get email.
*
* @return string
*/
public function getEmail()
{
return $this->email;
}
}



The controller (not quite finished but trying to solve this first):


/**
* Register controller
*
* @category Controller
* @package XXXUserBundle
* @subpackage Controller
*
*/

namespace AppXXXUserBundleController;


use SymfonyBundleFrameworkBundleControllerController;
use AppXXXUserBundleServiceUserService;
use AppXXXUserBundleEntityUser;
use AppXXXUserBundleEntityRegister;

class RegisterController extends Controller
{
private $userService;

public function __construct(UserService $userService)
{
$this->userService= $userService;
}

public function __invoke(Register $data): Register
{
//$this->myService->doSomething($data);

/*
$user = $this->userService->register($data);

if($user){
// $token = JWTManager::create($user)

// voorbeeld

// return serialized token
// @var JWTManager $jwtManager
$jwtManager = $this->get('lexik_jwt_authentication.jwt_manager');
$jwt = $jwtManager->create($user);
$response = new JsonResponse();
$response->setData(array('token' => $jwt));
return $response;
}
*/
return $data;
}


}



and last but not least config/routes/api_platform.yaml


api_platform:
resource: .
type: api_platform
prefix: /api


user_register:
path: '/api/register'
methods: ['POST']
defaults:
_controller: 'AppXXXUserBundleRegisterController::class'
_api_resource_class: 'AppXXXUserBundleEntityRegister'
_api_item_operation_name: 'register'

user_reminder:
path: '/api/reset'
methods: ['POST']
defaults:
_controller: 'AppXXXUserBundleResetController::class'
_api_resource_class: 'AppXXXUserBundleEntityReset'
_api_item_operation_name: 'rest'

user_login:
path: '/api/login'
methods: ['POST']
defaults:
_controller: 'AppXXXUserBundleLoginController::class'
_api_resource_class: 'AppXXXUserBundleEntityLogin'
_api_item_operation_name: 'login'



I'm probably overlooking something basic here, but i've spend some hours on it and just cant seem to get it to work.



Kind Regards



--- Update ---
Wel I changed everything back to basic (for testing and error hunting) and it just wont fly. Added get/set and even made ID public, it just wont work. Even this simple class below gets me the IRI error. So basically everything I try that isn't an ORM object fails :(


use ApiPlatformCoreAnnotationApiProperty;
use ApiPlatformCoreAnnotationApiResource;

/**
* The user Entity
*
* @ApiResource(
* itemOperations={
* "get"={"method"="GET"},
* },
* collectionOperations={
* "get"={"method"="GET"},
* "register"={"route_name"="register_register"},
* },
* )
*/

class Register
{
/**
* The unique identifier of this user <br /><b>Schema:</b> <a href="http://schema.org/identifier">https://schema.org/identifier</a>
*
* @var integer
*
* @ApiProperty(identifier=true)
*/
public $id;


/**
* Get id.
*
* @return int
*/
public function setId($id)
{
$this->id = $id;

return $this;
}


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









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

How to set up datasource with Spring for HikariCP?

PHP contact form sending but not receiving emails