1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//! OpenAPI support for Poem.

#![forbid(unsafe_code)]
#![deny(private_in_public, unreachable_pub)]
#![cfg_attr(docsrs, feature(doc_cfg))]
#![warn(missing_docs)]

mod base;
mod error;
mod openapi;
#[doc(hidden)]
pub mod param;
pub mod payload;
#[doc(hidden)]
pub mod registry;
pub mod types;
#[doc(hidden)]
pub mod ui;

pub use base::{CombinedAPI, Request, Response, Schema, API};
pub use error::ParseRequestError;
#[doc(hidden)]
pub use indexmap;
pub use openapi::OpenAPI;
#[doc(hidden)]
pub use poem;
#[doc(hidden)]
pub use serde;
#[doc(hidden)]
pub use serde_json;

/// Define a OpenAPI enum
///
/// # Macro parameters
///
/// | Attribute     | description               | Type     | Optional |
/// |---------------|---------------------------|----------|----------|
/// | name          | Object name               | string   | Y        |
/// | rename_items | Rename all the items according to the given case convention. The possible values are "lowercase", "UPPERCASE", "PascalCase", "camelCase", "snake_case", "SCREAMING_SNAKE_CASE". | string   | Y        |
///
/// # Item parameters
///
/// | Attribute   | description               | Type     | Optional |
/// |-------------|---------------------------|----------|----------|
/// | name        | Item name                 | string   | Y        |
///
/// # Examples
///
/// ```
/// use poem_openapi::Enum;
///
/// #[derive(Enum)]
/// enum PetStatus {
///     Available,
///     Pending,
///     Sold,
/// }
/// ```
#[rustfmt::skip]
pub use poem_openapi_derive::Enum;

/// Define a OpenAPI request.
///
/// # Examples
///
/// ```
/// use poem_openapi::{
///     payload::{Json, PlainText},
///     Request, Schema,
/// };
/// 
/// #[derive(Schema)]
/// struct Pet {
///     id: String,
///     name: String,
/// }
///
/// #[derive(Request)]
/// enum CreatePet {
///     /// This request receives a pet in JSON format(application/json).
///     CreateByJSON(Json<Pet>),
///     /// This request receives a pet in text format(text/plain).
///     CreateByPlainText(PlainText),
/// }
/// ```
#[rustfmt::skip]
pub use poem_openapi_derive::Request;

/// Define a OpenAPI response.
///
/// # Macro parameters
///
/// | Attribute     | description               | Type     | Optional |
/// |---------------|---------------------------|----------|----------|
/// | bad_request_handler | Sets a custom bad request handler, it can convert error to the value of the this response type. | string   | Y
///
/// # Item parameters
///
/// | Attribute   | description               | Type     | Optional |
/// |-------------|---------------------------|----------|----------|
/// | status      | HTTP status code. If omitted, it is a default response type. | u16   | Y        |
/// 
/// # Examples
/// 
/// ```
/// use poem_openapi::{payload::PlainText, Response};
/// use poem::Error;
///
/// #[derive(Response)]
/// #[oai(bad_request_handler = "bad_request_handler")]
/// enum CreateUserResponse {
///     /// Returns when the user is successfully created.
///     #[oai(status = 200)]
///     Ok,
///     /// Returns when the user already exists.
///     #[oai(status = 409)]
///     UserAlreadyExists,
///     /// Returns when the request parameters is incorrect.
///     #[oai(status = 400)]
///     BadRequest(PlainText),
/// }
/// 
/// // Convert error to `CreateUserResponse::BadRequest`.
/// fn bad_request_handler(err: Error) -> CreateUserResponse {
///     CreateUserResponse::BadRequest(PlainText(format!("error: {}", err)))
/// }
/// ```
#[rustfmt::skip]
pub use poem_openapi_derive::Response;

/// Define a OpenAPI schema
///
/// # Macro parameters
///
/// | Attribute     | description               | Type     | Optional |
/// |---------------|---------------------------|----------|----------|
/// | name          | Object name               | string   | Y        |
/// | rename_fields | Rename all the fields according to the given case convention. The possible values are "lowercase", "UPPERCASE", "PascalCase", "camelCase", "snake_case", "SCREAMING_SNAKE_CASE". | string   | Y        |
/// | concretes     | Specify how the concrete type of the generic Schema should be implemented. | ConcreteType |  Y |
/// | deprecation   | Schema deprecated          | bool     | Y        |
///
/// # Field parameters
///
/// | Attribute     | description               | Type     | Optional |
/// |---------------|---------------------------|----------|----------|
/// | skip          | Skip this field           | bool     | Y        |
/// | name          | Field name                | string   | Y        |
/// 
/// # Examples
/// 
/// ```
/// use poem_openapi::Schema;
/// 
/// /// Pet
/// #[derive(Schema)]
/// struct Pet {
///     /// The id of this pet.
///     id: String,
/// 
///     /// The name of this pet.
///     name: String,
/// }
/// ```
#[rustfmt::skip]
pub use poem_openapi_derive::Schema;

/// Define a OpenAPI schema
///
/// # Operation parameters
///
/// | Attribute     | description               | Type     | Optional |
/// |---------------|---------------------------|----------|----------|
/// | path          | HTTP uri.                 | string   | N        |
/// | method        | HTTP method. The possible values are "get", "post", "put", "delete", "head", "options", "connect", "patch", "trace". | string   | N        |
/// | deprecation   | Operation deprecated      | bool     | Y        |
/// | tag           | Operation tag             | string   | Y        |
///
/// # Operation argument parameters
///
/// | Attribute     | description               | Type     | Optional |
/// |---------------|---------------------------|----------|----------|
/// | name          | Parameter name. When this value is set, it means that this is an OpenAPI parameter type.           | string   | Y        |
/// | in            | Where to parse the parameter. The possible values are "query", "path", "header", "cookie". | string   | Y        |
/// | extract       | It means that this parameter is a Poem extractor. | bool | Y |
/// | desc          | Argument description      | string   | Y        |
/// | deprecation   | Argument deprecated       | bool     | Y        |
///
/// # Examples
/// 
/// ```
/// use poem_openapi::{
///     payload::{Json, PlainText},
///     Request, Schema, API, Response,
/// };
///
/// #[derive(Schema)]
/// struct Pet {
///     id: String,
///     name: String,
/// }
///
/// #[derive(Request)]
/// enum CreatePetRequest {
///     /// This request receives a pet in JSON format(application/json).
///     CreateByJSON(Json<Pet>),
///     /// This request receives a pet in text format(text/plain).
///     CreateByPlainText(PlainText),
/// }
///
/// #[derive(Response)]
/// enum CreatePetResponse {
///     /// Returns when the pet is successfully created.
///     #[oai(status = 200)]
///     Ok,
///     /// Returns when the pet already exists.
///     #[oai(status = 409)]
///     PetAlreadyExists,
/// }
/// 
/// struct PetAPI;
/// 
/// #[API]
/// impl PetAPI {
///     /// Create a new pet.
///     #[oai(path = "/pet", method = "post")]
///     async fn create_pet(
///         &self,
///         #[oai(name = "TOKEN", in = "header")] token: String,
///         req: CreatePetRequest
///     ) -> CreatePetResponse {
///         todo!() 
///     }
/// }
/// ```
#[rustfmt::skip]
pub use poem_openapi_derive::API;