symfony - Api 平台自定义 swagger/openapi 上下文主体
问题描述
我正在使用带有 Symfony 4 的 Api 平台,并且我想创建一个自定义端点。一切正常,但我无法更改两件事:正文和响应格式(在 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"},
* },
* },
* },
* },
* },
* },
* },
* }
* )
推荐阅读
- regex - 如何过滤 Sitefinity 表单段落文本框以禁止 URL?
- python - SQLAlchemy:多个 ForeignKeyConstraint 引用相同的目标列
- dart - mediaDevices.enumerateDevices() 在浏览器中生成运行时错误
- kubernetes - GKE 上的抢先式 Cloud Run
- excel - 交换两个相邻的单元格
- typescript - 打字稿,类型保护在异步函数中无法正常工作?
- python - 我不明白为什么会出现“太多值无法解包”错误
- python - jupyter-console 无法启动,但是 jupyter-notebook 很好
- sql - 在表名称中使用连字符 (-) 创建 Hive 表
- c# - 我没有收到发送到 SignalR Group 的消息