承接 fusonic/api-documentation-bundle 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

fusonic/api-documentation-bundle

Composer 安装命令:

composer require fusonic/api-documentation-bundle

包简介

Symfony bundle for automated documentation with NelmioApiDocBundle.

README 文档

README

License Latest Version Total Downloads php 8.2+

About

This bundle makes generating documentation of your (json) API routes easier. It provides a custom route annotation that can parse the input and output model of a route to generate documentation definitions for NelmioApiDocBundle. If you are using type hints for the input and output it can be detected automatically, see Usage on how to do this.

With just NelmioApiDocBundle you will often find yourself writing many annotations with a repetitive pattern. With this bundle common annotation combinations are bundled into one single route attribute.

This bundle can work well together with the http-kernel-bundle.

Install

Use composer to install the bundle from packagist.

composer require fusonic/api-documentation-bundle

Requirements:

  • PHP 8.2+
  • Symfony 5.4+

In case Symfony did not add the bundle to the bundle configuration, add the following (by default located in config/bundles.php):

<?php

return [
    // ...
    Fusonic\ApiDocumentationBundle\FusonicApiDocumentationBundle::class => ['all' => true],
];

Next you need to configure NelmioApiDocBundle.

There is one optional configuration for this bundle:

fusonic_api_documentation:
    # An attribute, class, or interface that is used to detect which object to parse the "input" model from.
    # If you do not configure this, automatic input detection will not work.
    request_object_class: Fusonic\ApiDocumentationBundle\Tests\App\FromRequest

Usage

Different examples can be found in the tests.

Example route with automatic type detection

If you have some kind of response listener that allows you to return objects directly from your controller then you can use the automatic output detection based on the return type or return annotation.

    #[DocumentedRoute(path: '/test-return-type/{id}', methods: ['GET'])]
    public function testReturnType(#[FromRequest] TestRequest $query): TestResponse
    {
        return new TestResponse($query->id);
    }

If you return an array or a generic type, you can set the return type (e.g.: SomeType[] or `SomeGeneric). The only requirement here is that you can only have one return type. Multiple return types are not supported.

Example route with manual input/output

If you do not support argument resolving and returning objects directly you can define the input and output classes manually.

    #[DocumentedRoute(path: '/test-return-type/{id}', methods: ['GET'], input: TestRequest::class, output: TestResponse::class)]
    public function testReturnType(int $id): JsonResponse
    {
    return new JsonResponse(['id' => $query->id], 200);
    }

You can also specify builtin types for the output, for example string:

#[DocumentedRoute(path: '/test-return-type/{id}', methods: ['GET'], input: TestRequest::class, output: 'string')]

If your manually defined output is a collection, you can set outputIsCollection: true in addition to the output:

#[DocumentedRoute(
    path: '/test-return-type/{id}',
    methods: ['GET'],
    input: TestRequest::class,
    description: 'custom description',
    statusCode: 200,
    output: 'string',
    outputIsCollection: true
)]

Documenting errors

Use the repeatable #[DocumentedError] attribute to document a route's error responses. Works on #[DocumentedRoute] and plain Symfony #[Route]:

#[DocumentedRoute(path: '/orders/{id}', methods: ['GET'])]
#[DocumentedError(exceptionClass: OrderNotFoundException::class, statusCode: 404)]
public function order(#[FromRequest] OrderRequest $request): OrderResponse

To document the error response body too, point the bundle at an exception method returning the context:

fusonic_api_documentation:
    exception_context_class:
        class: Fusonic\HttpKernelBundle\Exception\ContextAwareExceptionInterface
        method: getContext

The return type of getContext() is then used as the response schema.

Contributing

This is a subtree split of fusonic/php-extensions repository. Please create your pull requests there.

fusonic/api-documentation-bundle 适用场景与选型建议

fusonic/api-documentation-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 23.41k 次下载、GitHub Stars 达 3, 最近一次更新时间为 2022 年 06 月 07 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 fusonic/api-documentation-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 fusonic/api-documentation-bundle 我们能提供哪些服务?
定制开发 / 二次开发

基于 fusonic/api-documentation-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 23.41k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 3
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 1

GitHub 信息

  • Stars: 3
  • Watchers: 7
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-06-07