规范
ApiHug swagger 扩展使用手册
把 protobuf service / rpc 映射为 HTTP API,并生成 OpenAPI 文档。
apihug/protobuf/swagger/swagger.proto + annotations.protoservice / rpc / message / field / enumimport "apihug/protobuf/swagger/annotations.proto";
如果字段或响应需要 mock 规则,再额外引入:
import "apihug/protobuf/mock/mock.proto";
| 层级 | proto 元素 | 扩展点 |
|---|---|---|
| Service | service | (hope.swagger.svc) |
| Method | rpc | (hope.swagger.operation) |
| Message | message | (hope.swagger.schema) |
| Field | field | (hope.swagger.field) |
| Enum | enum | (hope.swagger.enm) |
service PetService {
option (hope.swagger.svc) = {
path: "/pet";
description: "宠物管理服务";
};
}
rpc ListPets (PetQueryRequest) returns (Pet) {
option (hope.swagger.operation) = {
post: "/list";
description: "分页查询宠物列表";
tags: "pet";
};
}
支持:
getpostputpatchdelete| 字段 | 说明 |
|---|---|
description | 接口说明 |
summary | 短摘要 |
tags | 分组标签 |
request_name | 请求体名称 |
pageable | 分页查询 |
input_repeated | 请求为列表 |
output_repeated | 响应为列表 |
raw | 返回原始响应,不再包装 |
session | 运行时需要 HttpSession |
group | CUSTOMER / TENANT / PLATFORM |
authorization | 授权规则 |
priority | 优先级 |
response_media_type | 响应媒体类型 |
request_schema | 请求体 schema 补充描述 |
response_schema | 响应 schema 补充描述 |
body_empty | 允许空响应体 |
multipart | multipart 请求 |
questions | 面向 AI 的自然语言提示 |
internal | 仅内部使用 |
hide | 文档中隐藏 |
兼容旧字段:
input_plural 已废弃,请使用 input_repeatedout_plural 已废弃,请使用 output_repeatedmultiple 已废弃,请使用 multipart分页查询:
option (hope.swagger.operation) = {
post: "/pets/search";
pageable: true;
};
普通列表:
option (hope.swagger.operation) = {
post: "/pets/batch";
input_repeated: true;
output_repeated: true;
};
路径、查询、Header、Cookie、Session 参数统一放在 parameters 中。
parameters: {
parameter: {
name: "id";
in: PATH;
schema: {
format: LONG;
minimum: 1;
};
};
parameter: {
name: "include-deleted";
in: QUERY;
schema: {
format: BOOLEAN;
};
};
}
参数数组使用 is_repeated,不要再写 plural。
parameter: {
name: "user-id";
in: QUERY;
is_repeated: true;
schema: {
format: INTEGER;
};
}
注意:session: true 是 operation 级运行时开关,不等于在 OpenAPI 参数列表里声明一个 SESSION 参数。
常用 response_media_type:
| 枚举值 | MIME |
|---|---|
APPLICATION_JSON | application/json |
TEXT_PLAIN | text/plain |
TEXT_HTML | text/html |
APPLICATION_PDF | application/pdf |
APPLICATION_ZIP | application/zip |
APPLICATION_VND_OPEN_XML_FORMATS_XLSX | application/vnd.openxmlformats-officedocument.spreadsheetml.sheet |
IMAGE_PNG | image/png |
MULTIPART_FORM_DATA | multipart/form-data |
APPLICATION_OCTET_STREAM | application/octet-stream |
TEXT_CSV | text/csv |
APPLICATION_XML | application/xml |
authorization: {
rbac: {
authorities: ["PET_VIEW"];
};
}
也可以使用:
low_limit_risky_moderolesexpressionmessage PlaceOrderRequest {
option (hope.swagger.schema) = {
json_schema: {
title: "PlaceOrderRequest";
description: "下单请求";
};
};
}
uint64 id = 1 [(hope.swagger.field) = {
description: "请求 ID";
empty: false;
minimum: 1;
maximum: 12345;
example: "1111";
}];
true / false正确示例:
max_length: 64;
maximum: 12345;
nullable: false;
错误示例:
max_length: {value: 64}
maximum: {value: 12345}
nullable: "false"
field_configuration.path_param_name当字段被用作路径参数,且默认生成的参数名不合适时,可以显式指定:
string owner_id = 1 [(hope.swagger.field) = {
field_configuration: {
path_param_name: "owner-id";
};
}];
字段 mock:
string phone = 1 [(hope.swagger.field) = {
mock: {
nature: CN_PHONE;
};
}];
operation 级响应 mock:
option (hope.swagger.operation) = {
post: "/ping";
mock: {
nature: GUID;
};
};
更完整的 mock 规则请看 Mock 指令规范。
pageable,普通列表用 input_repeated / output_repeatedis_repeatedhide、internal{value: ...} 包装写法