Nelmioapidocbundle: Required field in Class Input

Created on 8 Oct 2014  路  6Comments  路  Source: nelmio/NelmioApiDocBundle

Im having a problem when trying to make a field in an entity a required one. I have the following Class :

use JMS\Serializer\Annotation as Rest;
use Symfony\Component\Validator\Constraints as Assert;

class ActivateSIMRequest
{

    /**
     * Array of ICCIDs
     * @Rest\Type("array<string>")
     */
    private $iccids;

    /**
     * Customer Solution Name
     * @Rest\Type("string")
     * @Assert\NotBlank()
     */
    private $customerSolution;
}

And the following controller method :

/**
 * Activates SIM(s) onto a specified Customer Solution
 * 
 * @ApiDoc(
 *  resource=true,
 *  description="Activate SIMs",
 *  requirements={
 *      {
 *          "name"="_format",
 *          "dataType"="string",
 *          "requirement"="json|xml",
 *          "description"="Request format - either json or xml"
 *      }
 *  },
 *  input="WL\APIBundle\Types\ActivateSIMRequest",
 *  output="WL\APIBundle\Types\RequestReturn",
 * )
 * @Rest\Post("/api/{_format}/activateSIM")
 */
public function activateSIMAction(Request $request, $_format)

The API Doc is duplicating the CustomerSolution Field :

Parameter            Type                  Required?          Format Description
customerSolution     string                true               {not blank}  
iccids[]             array of strings      false              Array of ICCIDs 
customer_solution    string                false              Customer Solution Name 

As you can see the $customerSolution field is being added twice - how can I get a required field added just once ?

Most helpful comment

What I ended up doing is using a custom parser. I only used the postParse function. It transforms every camelCase properties to snake_case. This solved the issue for me. You can see it here.

All 6 comments

you can add Rest\SerializedName("customerSolution"), it should resolve the problem

i want the field to be customer_solution - so I set Rest\SerializedName("customer_solution") and I still get both - im guessing the issue is with the validation serializer that the bundle uses

This is still an issue.
The pull request #733 will probably fix it, but doesn't seem to be could solution to add the JMS dependency inside ApiDocExtractor.
Any suggestions when to execute the naming strategy? Now it's only executed for the JMSMetadataParser.

Any workarounds for this?

What I ended up doing is using a custom parser. I only used the postParse function. It transforms every camelCase properties to snake_case. This solved the issue for me. You can see it here.

version 3 is a complete rewrite. please check if this is still a problem with version 3 and a recent symfony version.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abidichrak picture abidichrak  路  5Comments

andydandy80 picture andydandy80  路  4Comments

GuilhemN picture GuilhemN  路  6Comments

jhkchan picture jhkchan  路  4Comments

astronati picture astronati  路  3Comments