-
Notifications
You must be signed in to change notification settings - Fork 77
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "example" to openapi schemas output (via schemars) (#325)
- Loading branch information
Showing
6 changed files
with
255 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/*! | ||
* Example use of dropshot to output OpenAPI compatible JSON. This program | ||
* specifically illustrates how to add examples to each schema using schemars, | ||
* and how that will be reflected in the resultant JSON generated when ran. | ||
*/ | ||
|
||
use dropshot::{ | ||
endpoint, ApiDescription, HttpError, HttpResponseOk, RequestContext, | ||
}; | ||
use schemars::JsonSchema; | ||
use serde::{Deserialize, Serialize}; | ||
use std::sync::Arc; | ||
|
||
/* | ||
* Define 2 structs here - Bar is nested inside Foo and should result in an | ||
* example that looks like: | ||
* | ||
* { | ||
* "id": 1, | ||
* "bar": { | ||
* "id: 2 | ||
* } | ||
* } | ||
*/ | ||
|
||
#[derive(Deserialize, Serialize, JsonSchema)] | ||
#[schemars(example = "foo_example")] | ||
/// # Foo Object | ||
struct Foo { | ||
/// Foo ID | ||
id: u32, | ||
|
||
/// All Foo's have a Bar | ||
bar: Bar, | ||
} | ||
|
||
#[derive(Deserialize, Serialize, JsonSchema)] | ||
#[schemars(example = "bar_example")] | ||
/// # Bar Object | ||
struct Bar { | ||
/// Bar ID | ||
id: u32, | ||
} | ||
|
||
/// Used by schemars to generate the `Foo` example. | ||
fn foo_example() -> Foo { | ||
Foo { id: 1, bar: bar_example() } | ||
} | ||
|
||
/// Used by schemars to generate the `Bar` example. | ||
fn bar_example() -> Bar { | ||
Bar { id: 2 } | ||
} | ||
|
||
fn main() -> Result<(), String> { | ||
let mut api = ApiDescription::new(); | ||
api.register(get_foo).unwrap(); | ||
|
||
api.openapi("Examples", "0.0.0") | ||
.write(&mut std::io::stdout()) | ||
.map_err(|e| e.to_string())?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[endpoint { | ||
method = GET, | ||
path = "/foo", | ||
tags = [ "foo" ], | ||
}] | ||
/// Get a foo | ||
async fn get_foo( | ||
_rqctx: Arc<RequestContext<()>>, | ||
) -> Result<HttpResponseOk<Foo>, HttpError> { | ||
let foo = foo_example(); | ||
Ok(HttpResponseOk(foo)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters