How to define an associative array in Nelmio API DOC vs 3? - symfony

I am using Symfomy 3.4 with Nelmio API DOC vs 3.
I have a GET API with a multiplier parameter of the format:
multiplier[14]=2&multiplier[15]=1. I want to be able to make the request from /api-doc panel with the keys of the array, not only the values. At this moment, I can add only values.
The way I am defining the relationship at this moment is:
* #SWG\Parameter(
* name="multiplier",
* in="query",
* type="array",
* description="multiply the received reward",
* #SWG\Items(
* type="integer",
* ),
* required=false
* )
I tried several things, like
* #SWG\Property(
* type="string"
* ),
but it seems not to work.
How can I make the key appear in the panel so I can complete it there, without using something external, such as Postman?

Related

Multi optional parameters in route symfony

I have route like this:
* #Route(
* "/search/{ro}/{mind}/{his}",
* name="search",
* requirements={"ro"="[a-zA-Z]{2,}", "mind"="()|new|old", "his"="()|yes|ok|no"}
* )
if I want this url: /search/yes I need to enter this address: /search///yes to show page.
how can remove // from original url?
welcome to stackoverflow,
you know you can have multiple routes for one action?
Just be aware of the order of the routes - the more parameters the higher - or a lesser route will catch first.
* #Route(
* "/search/{ro}/{mind}/{his}",
* name="search",
* requirements={"ro"="[a-zA-Z]{2,}", "mind"="()|new|old", "his"="yes|ok|no"}
* )
* #Route(
* "/search/{his}",
* name="search_short",
* requirements={"his"="yes|ok|no"}
* )

Swagger annotation for query parameters for file upload

I am trying to make an api documentation for my symfony based api and I would like to add info about the upload endpoint for my api. I am using nelmioapidocbundle. Can anyone please provide a link to the documentation where I can find it? or better if there is an example. Thank you.
/**
* #Route("/products/{id}/images", methods={"POST"})
*
* #SWG\Parameter(
* name="file",
* in="body",
* type="file",
* ---- Im not sure what to put here to make this work
* )
*
* #SWG\Response(
* response=201,
* description="File is uploaded."
* )
* )
*/
public function uploadImages(Request $request, FileUploadService $fileUploadService) {}
For anyone who might encounter this. this formData in value fixed the problem:
* #SWG\Parameter(
* name="file",
* in="formData", <----
* required=true,
* type="file",
* description="product image"

Doctrine : Fetch "EAGER" and "Hydrate Array"

With Doctrine, I have fetch=EAGER in my entity :
class TrainingOrganization
{
/**
* #var TrainingOrganizationVersion[]|ArrayCollection
*
* #ORM\OneToMany(
* targetEntity="AppBundle\Entity\TrainingOrganizationVersion",
* mappedBy="trainingOrganization",
* cascade={"persist"},
* fetch="EAGER"
* )
* #ORM\OrderBy({"id" = "ASC"})
* #Assert\Valid()
* #Versionable
*/
private $versions;
Why when i do "hydrate array" it does not work ?
Screen of my dump for same entity (Second is "Hydrate array") :
With Hydration mode Query::HYDRATE_ARRAY, Doctrine will only return information about that 'row'. Since your versions attribute is not a field but a collection, it won't be returned.
If you want to have Collections included, use Objects instead (like your first screenshot).
If you really need your entities serialized (returning a multidimensional array instead of objects), use a serializer. Since you're using Symfony, you can easily use Symfony's Serializer Component. The JMSSerializerBundle is a popular alternative.

Symfony2 & FOSRestBundle: Getting UUID packed in a BINARY(16) field from MySQL

I am facing a weird problem relating to UUIDs.
I have developed a REST API using Symfony2+FOSRestBundle+JMSSerializer. As I need to update some tables from two sources I thought of using UUID as primary key for one entity.
I did a doctrine:mapping:import to generate entities in my Symfony project. Everything correct. I ended up with the following entity (only exposing the key field and generated getter for simplicity):
<?php
namespace Stardigita\TgaAPIBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* TgaBookings
*
* #ORM\Table(name="tga_bookings", indexes={[...]})
* #ORM\Entity
*/
class TgaBookings
{
/**
* #var string
*
* #ORM\Column(name="book_cd_booking_UUID", type="blob", length=16, nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $bookCdBookingUuid;
/**
* Get bookCdBookingUuid
*
* #return string
*/
public function getBookCdBookingUuid()
{
return $this->bookCdBookingUuid;
}
...
No setter was generated. I can still do it myself and I will, as I will need to know the key beforehand.
The data for this field is correctly stored in the table as a BINARY(16). When I recover the data calling the REST GET method, I get this:
[
{
"book_cd_booking_uuid": "Resource id #1244",
"book_cd_booking": 8,
....
My question is: how can I get the actual data from the field?
I suppose something has to be done in the field getter, but I tried some solutions without any success.
Thanks.
UPDATE:
I've managed to get the actual data logged, modifying the getBookCdBookingUuid method this way:
/**
* Get bookCdBookingUuid
*
* #return string
*/
public function getBookCdBookingUuid()
{
return bin2hex($this->bookCdBookingUuid);
}
and changed the type to "guid" in the property annotation:
/**
* #var string
*
* #ORM\Column(name="book_cd_booking_UUID", type="guid", length=16, nullable=false)
* #ORM\Id
* #ORM\GeneratedValue(strategy="IDENTITY")
*/
private $bookCdBookingUuid;
I have represented the hex UUID correctly in the log before returning the results in the controller:
[2014-11-03 19:52:07] app.ERROR: 1046684e5f6711e4a09f00089bce936a [] []
But still getting an exception relating UTF invalid characters:
request.CRITICAL: Uncaught PHP Exception RuntimeException: "Your data could not be encoded because it contains invalid UTF8 characters." at /var/www/tga_api/vendor/jms/serializer/src/JMS/Serializer/JsonSerializationVisitor.php line 36 {"exception":"[object] (RuntimeException: Your data could not be encoded because it contains invalid UTF8 characters. at /var/www/tga_api/vendor/jms/serializer/src/JMS/Serializer/JsonSerializationVisitor.php:36)"} []
Also I got no response from the service. A 500 error is returned.
Please, I need to solve this issue. Any ideas are welcome.
Thanks.
GeneratedValue
I notice you're using the annotation #ORM\GeneratedValue(strategy="IDENTITY") for the UUID property. IDENTITY means the database should/will use auto-increments, which shouldn't be done when using UUIDs. Please change it to #ORM\GeneratedValue(strategy="NONE") or remove it completely.
Conversion
The string form of a UUID (like 01234567-89ab-cdef-0123-456789abcdef) should be converted to binary when it's persisted in the database, and converted back when fetched from the database.
The easiest way to do this is to introduce a custom type. See here for an example.
Bugs
Doctrine (even master/2.5) has some issues with using UUIDs in associations. I'm attempting to fix these issues in PR #1178.
If you need UUIDs in associations and can't wait till it's fixed, then use regular integer ids and have the UUID is a separate column.

Set 'parameters' annotation in NelmioApiDocBundle

I create an API RESTfull using Symfony2.1 with FOSRESTBundle and I am using NelmioApiDocBundle to generate automatic documentation.
I have a PUT request in which the user should send one parameter, but I don't need to create a Form for this purpose. All works perfectly but when I generate the documentation I don't know how to add this parameter to the documentation because I don't have a 'input' form.
I tried this but seems doesn't work:
/**
* #ApiDoc(
* description="description",
* statusCodes={
* 200="Success",
* 400="Bad request"},
* parameters={
* {"name"="parameter_name", "dataType"="integer"}
* }
* )
...
In the documentation of NelmioApiDocBundle I didn't see any solution for this...
use filters key instead of parameters
* filters={
* {"name"="parameter_name", "dataType"="integer"},
* },

Resources