3个不稳定版本
0.2.0 | 2024年4月19日 |
---|---|
0.1.1 | 2023年9月27日 |
0.1.0 | 2023年8月18日 |
#103 在 模板引擎
每月166次下载
用于 geosuggest-core
14KB
224 行
OAPH
帮助将查询参数和模式定义替换到openapi3 yaml中。
这是一个功能简单的crate,没有野心支持整个openapi 3规范和覆盖所有情况(至少覆盖我的个人用例)。
use serde::{Serialize, Deserialize};
use oaph::{OpenApiPlaceHolder, schemars::{self, JsonSchema}};
#[allow(dead_code)]
#[derive(Deserialize, JsonSchema)]
struct SearchQuery {
/// some description for this flag (see https://graham.cool/schemars/examples/6-doc_comments/)
flag: bool,
}
#[allow(dead_code)]
#[derive(Deserialize, JsonSchema)]
struct SearchResponse {
success: bool,
count: usize,
items: Vec<Item>,
}
#[allow(dead_code)]
#[derive(Deserialize, JsonSchema)]
struct Item {
id: usize,
value: String,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let openapi3_yaml = OpenApiPlaceHolder::new()
.query_params::<SearchQuery>("SearchQuery")?
.schema::<SearchResponse>("SearchResponse")?
.render_to(r#"
openapi: 3.0.0
info:
title: oaph example
version: 1.0.0
paths:
/search:
get:
tags:
- demo
description: demo api
parameters:
{{SearchQuery}}
responses:
'201':
content:
application/json:
schema:
{{SearchResponse}}
definitions:
{{oaph::definitions}}
"#)?;
println!("{}", openapi3_yaml);
Ok(())
}
输出将会是
openapi: 3.0.0
info:
title: oaph example
version: 1.0.0
paths:
/search:
get:
tags:
- demo
description: demo api
parameters:
- in: query
name: flag
description: some description for this flag (see https://graham.cool/schemars/examples/6-doc_comments/)
required: true
schema:
type: boolean
responses:
'201':
content:
application/json:
schema:
title: SearchResponse
type: object
required:
- count
- items
- success
properties:
success:
type: boolean
count:
type: integer
format: uint
minimum: 0.0
items:
type: array
items:
$ref: "#/definitions/Item"
definitions:
Item:
type: object
required:
- id
- value
properties:
id:
type: integer
format: uint
minimum: 0.0
value:
type: string
感谢
许可证
本项目的许可证为
- MIT许可证 (LICENSE 或 http://opensource.org/licenses/MIT)
依赖
~2.5–3.5MB
~73K SLoC