【问题标题】:API Platform - How to add response code in Swagger UI using decorator to add 400, 404, 500 response code so client know the response in adwanceAPI 平台 - 如何使用装饰器在 Swagger UI 中添加响应代码以添加 400、404、500 响应代码,以便客户端提前知道响应
【发布时间】:2021-05-28 06:23:42
【问题描述】:

我想使用 为 swagger UI 中的每个路由添加响应代码(400,404,500),以便用户提前知道这些错误代码的响应。

我正在关注以下链接,但不确定如何修改 Response 对象并添加 400、404、401 等的响应。

https://api-platform.com/docs/core/openapi/#overriding-the-openapi-specification

我在写这个问题时使用的是 symfony 5.2 和 apiplatfrom 2.6。

【问题讨论】:

    标签: symfony decorator openapi api-platform.com


    【解决方案1】:

    当您使用装饰器时,您实际上是在扩展现有的服务代码。 首先,您获取路径,然后使用给定的代码在 swagger 上下文中设置您的自定义操作。

    如果你注意到代码块:

    $openApi->getPaths()->addPath('/api/grumpy_pizzas/{id}', $pathItem->withGet(
                $operation->withParameters(array_merge(
                    $operation->getParameters(),
                    [new Model\Parameter('fields', 'query', 'Fields to remove of the output')]
                ))
            ));
    

    路径项将Operation 对象作为参数,该对象具有withResponses 方法,可用于修改路径生成的任何响应。 将此作为每个光标,您可以检查整个源代码并查看最适合您要求的内容。查看来源:PathItem.php

    您可以根据Response对象构建响应并将其添加到操作和路径中。

    提示:任何具有良好 linter 的编辑器都会使您的编码更容易,因为它能够向您显示可用的方法。

    【讨论】:

      【解决方案2】:

      虽然迟到了,但我得到了在我的 Swagger Ui 中添加响应代码的解决方案,它可以帮助一些寻找解决方案的人。

      private function setErrorResponseDescriptions(OpenApi $openApi): OpenApi
      {
          $schemas = $openApi->getComponents()->getSchemas();
      
          $schemas['Error'] = [
              'type'       => 'object',
              'properties' => [
                  'type' => [
                      'type'     => 'string',
                      'readOnly' => true,
                  ],
                  'title' => [
                      'type'     => 'string',
                      'readOnly' => true,
                  ],
                  'detail' => [
                      'type'     => 'string',
                      'readOnly' => true,
                  ],
              ],
          ];
      
          $schemas['ValidationError'] = [
              'type'       => 'object',
              'properties' => [
                  'type' => [
                      'type'     => 'string',
                      'readOnly' => true,
                  ],
                  'title' => [
                      'type'     => 'string',
                      'readOnly' => true,
                  ],
                  'detail' => [
                      'type'     => 'string',
                      'readOnly' => true,
                  ],
                  'validations' => [
                      'type'     => 'array',
                      'readOnly' => true,
                      'items'    => [
                          'type'       => 'object',
                          'properties' => [
                              'propertyPath' => [
                                  'type'     => 'string',
                                  'readOnly' => true,
                              ],
                              'message' => [
                                  'type'     => 'string',
                                  'readOnly' => true,
                              ],
                          ]
                      ]
                  ]
              ],
          ];
      
          foreach ($openApi->getPaths()->getPaths() as $path => $pathItem) {
              foreach (['PUT', 'POST', 'PATCH', 'GET'] as $method) {
                  $item = $pathItem->{'get' . ucfirst($method)}();
      
                  if ($item) {
                      $item->addResponse(
                          new Model\Response(
                              description: 'Unauthorized',
                              content: new \ArrayObject([
                                  'application/problem+json' => [
                                      'schema' => [
                                          '$ref' => '#/components/schemas/Error',
                                      ],
                                  ],
                              ])
                          ),
                          '401'
                      );
                      if ('GET' !== $method) {
                          $item->addResponse(
                              new Model\Response(
                                  description: 'Bad request',
                                  content: new \ArrayObject([
                                      'application/problem+json' => [
                                          'schema' => [
                                              '$ref' => '#/components/schemas/ValidationError',
                                          ],
                                      ],
                                  ])
                              ),
                              '400'
                          );
                      } else {
                          $item->addResponse(
                              new Model\Response(
                                  description: 'Not Found',
                                  content: new \ArrayObject([
                                      'application/problem+json' => [
                                          'schema' => [
                                              '$ref' => '#/components/schemas/Error',
                                          ],
                                      ],
                                  ])
                              ),
                              '404'
                          );
                      }
                  }
              }
          }
      
          return $openApi;
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-08-14
        • 1970-01-01
        • 1970-01-01
        • 2018-05-11
        • 2023-02-21
        • 1970-01-01
        • 1970-01-01
        • 2016-03-23
        相关资源
        最近更新 更多