genai/openapi 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

genai/openapi

Composer 安装命令:

composer require genai/openapi

包简介

Generates an OpenAPI 3 document at build time from your #[RestController] routes, request-body Forms/DTOs and validation constraints — baked into a reflection-free Cache\OpenApi — and ships a controller that serves /openapi.json and a Swagger UI page at /docs. PHP 5.3-safe at runtime.

README 文档

README

Auto-generates an OpenAPI 3 document for your JSON API and serves Swagger UI — the same compile-time-scan approach as the rest of the stack, so the docs come for free with zero runtime cost.

How it works

A build processor (OpenApiProcessor) runs during composer compile and reads the attributes you already have:

  • Paths + methods#[Route] / #[GetMapping] / #[PostMapping] … on #[RestController] classes (HTML #[Controller]s are ignored — they're not an API).
  • Path params{braces} in the route path.
  • Request body schema ← an action's GenAI\Web\Form parameter → a component schema built from that class's fields.
  • Schemas ← a #[Dto] is described by its getters and their return types (matching how the serializer emits it; nested DTOs become $refs). A request Form is described by its properties + validation constraints (anything exposing rule()): #[NotBlank]required, #[Email]format: email, #[Length(min/max)]minLength/maxLength, #[Pattern]pattern.
  • Summary / description ← optional args on the mapping itself (#[GetMapping('/games', summary: '…', description: '…')]).
  • Tag (groups endpoints) ← #[RestController(tag: '…', description: '…')].

The document is baked into Cache\OpenApi::json() (always emitted), and a shipped controller exposes it:

  • GET /openapi.json — the spec
  • GET /docs — Swagger UI (loads swagger-ui-dist from a CDN → needs internet)

Use it

composer require genai/openapi
composer compile

Then annotate a JSON endpoint:

#[RestController(path: '/api', tag: 'KidSafe API', description: 'Public JSON endpoints.')]
class ApiController
{
    #[GetMapping('/games', summary: 'List the published games')]
    public function games() { return $this->games->all(); }       // RestController -> JSON

    #[PostMapping('/feedback', summary: 'Send feedback')]
    public function feedback(FeedbackForm $form) { ... }           // body schema from FeedbackForm
}

Open …/docs to explore. …/openapi.json is the raw document (feed it to client generators, Postman, etc.).

Notes

  • Build-time, zero runtime cost — the spec is baked, like Cache\Router.

  • REST only#[Controller] (HTML/view) actions are intentionally excluded.

  • Success response: point the mapping at a response DTO — #[GetMapping('/games', response: GameView::class . '[]')] (append [] for an array). The DTO is a plain documentation class; field types come from @var docblocks (props can't be typed on PHP 5.3). If no response: is given, the processor reads a declared return type (: array, : GameView) — but that's PHP 7+ syntax that breaks a 5.3 runtime, so on 5.3 use response:. Otherwise a generic 200.

  • Error responses: list the statuses on the mapping with errorCodes: [...] (validated at compile time — a value outside 100–599 fails the build). Each is documented with the one shared Error component ({ code, message }), so every error in the API looks the same; the description is the standard HTTP reason phrase. Example:

    #[PostMapping('/feedback', summary: 'Send feedback', response: FeedbackResult::class, errorCodes: [422, 500])]
    public function feedback(FeedbackForm $form) { ... }
  • Decoupled — validation facets are read by duck-typing rule(), so it works with or without genai/validation installed.

  • Swagger UI is loaded from a CDN for convenience; for an offline/CSP setup, vendor swagger-ui-dist and point the /docs page at the local assets.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2026-07-10

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固