Im using Open Api 3 (exactly NelmioApiDocBundle) in Symfony to document APIs and I have the following code:
* @Route("/login", name="user_login", methods={"POST"})
*
* @OA\Response(
* response=200,
* @OA\JsonContent(
* type="object",
* @OA\Property(property="code", type="number"),
* @OA\Property(property="error", type="boolean"),
* @OA\Property(property="message", type="string")
* )
* )
This works and shows me the following:
What I want to know is if there is an easier way to define these responses so I don't have to write so many lines at each endpoint.
I wish there was a way to define a JSON file as an interface and have it read from there. I've tried a thousand things but nothing works for me and I don't know how to do it and I've already given up.
Thanks a lot!
I have tried to use @Model, @Schema, modify YAML files... etc... I have read the documentation, but I have not seen any valid example for my case in this version of Open Api.
CodePudding user response:
If you use models, you can put in the following
@OA\JsonContent(ref=@Model(type=LoginResponse::class))
Your response class should be something like this:
use OpenApi\Annotations as OA;
class LoginResponse
{
/** @OA\Property(type="number") */
public $code;
/** @OA\Property(type="boolean") */
public $error;
/** @OA\Property(type="string") */
public $message;
}
Make sure the properties are publicly accessible and you have symfony/property-info
installed. You can also use jms/serializer, but that's a bit more work
More info at: https://symfony.com/bundles/NelmioApiDocBundle/current/index.html#use-models