我正在使用带有Symfony 4的Api平台,我想创建一个自定义端点。一切正常,但我无法更改 2 件事:正文和响应格式(在 openapi 文档中(。
参数和响应状态代码工作正常。
* "login"={
* "route_name"="api_login",
* "method" = "post",
* "openapi_context" = {
* "parameters" = {},
* "body" = {
* "description" ="Username and password",
* "schema" = {
* "type" = "object",
* "required" = {"email","password"},
* "properties" = {
* "email" = {
* "type" = "string"
* },
* "password" = {
* "type" = "string"
* }
* }
* }
* },
* "responses" = {
* "200" = {
* "description" = "User logged in",
* "schema" = {
* "type" = "object",
* "required" = {
* "token",
* "refresh_token"
* },
* "properties" = {
* "token" = {
* "type" = "string"
* },
* "refresh_token" = {
* "type" = "string"
* }
* }
* }
* },
* "401" = {
* "description" = "invalid password or email"
* }
* },
* "summary" = "Login user in application",
* "consumes" = {
* "application/json",
* "text/html",
* },
* "produces" = {
* "application/json"
* }
* }
* }
这对我有用,请参阅文档。 https://swagger.io/docs/specification/describing-request-body/
* @ApiResource(
* collectionOperations={
* "get": {
* "method": "GET",
* "access_control": "is_granted('ROLE_USER', object)",
* },
* "post": {
* "method": "POST",
* "access_control": "is_granted('ROLE_USER', object)",
* "openapi_context": {
* "requestBody": {
* "content": {
* "application/ld+json": {
* "schema": {
* "type": "object",
* "properties": {
* "token": {"type": "string", "example": "email@example.com"},
* "refresh_token": {"type": "string", "example": "123456"},
* },
* },
* },
* },
* },
* },
* },
* }
* )
看看这个关于 api 平台问题的响应(这里的文档在 yaml 中格式非常好,而不是将它们保存在 php 数组中,这是个不错的主意(并阅读文档,它们可能会帮助您以您想要的任何方式装饰文档。