Skip to content

Commit

Permalink
Merge branch 'openapi-v2'
Browse files Browse the repository at this point in the history
  • Loading branch information
sunli829 committed May 15, 2022
2 parents 63a4372 + 62556b8 commit 7d51938
Show file tree
Hide file tree
Showing 31 changed files with 337 additions and 637 deletions.
1 change: 0 additions & 1 deletion examples/openapi/auth-apikey/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ async fn api_checker(req: &Request, api_key: ApiKey) -> Option<User> {
}

#[derive(Object)]
#[oai(inline)]
struct LoginRequest {
username: String,
}
Expand Down
11 changes: 11 additions & 0 deletions examples/openapi/generics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[package]
name = "example-openapi-generics"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
poem = { path = "../../../poem" }
poem-openapi = { path = "../../../poem-openapi", features = ["swagger-ui"] }
tokio = { version = "1.17.0", features = ["macros", "rt-multi-thread"] }
tracing-subscriber = { version = "0.3.9", features = ["env-filter"] }
42 changes: 42 additions & 0 deletions examples/openapi/generics/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
use poem::{listener::TcpListener, Route, Server};
use poem_openapi::{
payload::Json,
types::{ParseFromJSON, ToJSON},
Object, OpenApi, OpenApiService,
};

#[derive(Object)]
struct MyObject<T: ParseFromJSON + ToJSON> {
value: T,
}

struct Api;

#[OpenApi]
impl Api {
#[oai(path = "/i32", method = "post")]
async fn i32(&self, value: Json<MyObject<i32>>) -> Json<MyObject<i32>> {
value
}

#[oai(path = "/string", method = "post")]
async fn string(&self, value: Json<MyObject<String>>) -> Json<MyObject<String>> {
value
}
}

#[tokio::main]
async fn main() -> Result<(), std::io::Error> {
if std::env::var_os("RUST_LOG").is_none() {
std::env::set_var("RUST_LOG", "poem=debug");
}
tracing_subscriber::fmt::init();

let api_service =
OpenApiService::new(Api, "Hello World", "1.0").server("http://localhost:3000/api");
let ui = api_service.swagger_ui();

Server::new(TcpListener::bind("127.0.0.1:3000"))
.run(Route::new().nest("/api", api_service).nest("/", ui))
.await
}
1 change: 0 additions & 1 deletion examples/openapi/uniform-response/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ struct Resource {
}

#[derive(Object)]
#[oai(inline)]
struct ResponseObject<T: ParseFromJSON + ToJSON + Send + Sync> {
code: i32,
msg: String,
Expand Down
9 changes: 3 additions & 6 deletions poem-openapi-derive/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "poem-openapi-derive"
version = "1.3.30"
version = "2.0.0-alpha.1"
authors = ["sunli <[email protected]>"]
edition = "2021"
description = "Macros for poem-openapi"
Expand All @@ -9,10 +9,7 @@ documentation = "https://docs.rs/poem/"
homepage = "https://github.com/poem-web/poem"
repository = "https://github.com/poem-web/poem"
keywords = ["http", "async", "openapi", "swagger"]
categories = [
"network-programming",
"asynchronous",
]
categories = ["network-programming", "asynchronous"]

[lib]
proc-macro = true
Expand All @@ -25,7 +22,7 @@ quote = "1.0.9"
syn = { version = "1.0.77", features = ["full", "visit-mut"] }
Inflector = "0.11.4"
thiserror = "1.0.29"
indexmap = "~1.6.2" # https://github.com/tkaitchuck/aHash/issues/95
indexmap = "~1.6.2" # https://github.com/tkaitchuck/aHash/issues/95
regex = "1.5.5"
http = "0.2.5"
mime = "0.3.16"
8 changes: 0 additions & 8 deletions poem-openapi-derive/src/common_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ impl RenameRuleExt for Option<RenameRule> {
}
}

#[derive(FromMeta)]
pub(crate) struct ConcreteType {
pub(crate) name: String,
pub(crate) params: PathList,
#[darling(default)]
pub(crate) example: Option<Path>,
}

pub(crate) struct PathList(pub(crate) Vec<Path>);

impl FromMeta for PathList {
Expand Down
4 changes: 2 additions & 2 deletions poem-openapi-derive/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,11 @@ pub(crate) fn generate(args: DeriveInput) -> GeneratorResult<TokenStream> {
}

fn schema_ref() -> #crate_name::registry::MetaSchemaRef {
#crate_name::registry::MetaSchemaRef::Reference(#oai_typename)
#crate_name::registry::MetaSchemaRef::Reference(Self::name().into_owned())
}

fn register(registry: &mut #crate_name::registry::Registry) {
registry.create_schema::<Self, _>(#oai_typename, |registry| #crate_name::registry::MetaSchema {
registry.create_schema::<Self, _>(Self::name().into_owned(), |registry| #crate_name::registry::MetaSchema {
description: #description,
external_docs: #external_docs,
deprecated: #deprecated,
Expand Down

0 comments on commit 7d51938

Please sign in to comment.