Skip to content

Commit

Permalink
Use examples from JSON schemas (GREsau#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
GREsau authored and mautamu committed Apr 23, 2021
1 parent 2c34343 commit aba17a7
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/json-web-api/Cargo.toml
Expand Up @@ -5,7 +5,7 @@ authors = ["Graham Esau <gesau@hotmail.co.uk>"]
edition = "2018"

[dependencies]
schemars = { version = "0.8.0-alpha-2", features = ["preserve_order"] }
schemars = { version = "0.8.0-alpha-4", features = ["preserve_order"] }
okapi = { version = "0.4.0", path = "../../okapi" }
rocket_okapi = { version = "0.4.1", path = "../../rocket-okapi" }
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", default-features = false }
Expand Down
5 changes: 5 additions & 0 deletions examples/json-web-api/src/main.rs
Expand Up @@ -16,9 +16,14 @@ struct User {
user_id: u64,
username: String,
#[serde(default)]
#[schemars(example = "example_email")]
email: Option<String>,
}

fn example_email() -> &'static str {
"test@example.com"
}

/// # Get all users
///
/// Returns all users in the system.
Expand Down
2 changes: 1 addition & 1 deletion okapi/Cargo.toml
Expand Up @@ -12,6 +12,6 @@ keywords = ["rust", "openapi", "swagger"]
derive_json_schema = ["schemars/derive_json_schema"]

[dependencies]
schemars = { version = "0.8.0-alpha-2" }
schemars = { version = "0.8.0-alpha-4" }
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0"
2 changes: 1 addition & 1 deletion rocket-okapi/Cargo.toml
Expand Up @@ -9,7 +9,7 @@ license = "MIT"
keywords = ["rust", "openapi", "swagger", "rocket"]

[dependencies]
schemars = { version = "0.8.0-alpha-2" }
schemars = { version = "0.8.0-alpha-4" }
okapi = { version = "0.4.0", path = "../okapi" }
rocket_okapi_codegen = { version = "=0.4.1", path = "../rocket-okapi-codegen" }
rocket = { git = "https://github.com/SergioBenitez/Rocket.git", branch = "async", default-features = false }
Expand Down
16 changes: 10 additions & 6 deletions rocket-okapi/src/gen.rs
Expand Up @@ -63,6 +63,15 @@ impl OpenApiGenerator {

/// Generate an `OpenApi` specification for all added operations.
pub fn into_openapi(self) -> OpenApi {
let mut schema_generator = self.schema_generator;
let mut schemas = schema_generator.take_definitions();

for visitor in schema_generator.visitors_mut() {
for schema in schemas.values_mut() {
visitor.visit_schema(schema)
}
}

OpenApi {
openapi: "3.0.0".to_owned(),
paths: {
Expand All @@ -76,12 +85,7 @@ impl OpenApiGenerator {
paths
},
components: Some(Components {
schemas: Map::from_iter(
self.schema_generator
.into_definitions()
.into_iter()
.map(|(k, v)| (k, v.into())),
),
schemas: Map::from_iter(schemas.into_iter().map(|(k, v)| (k, v.into()))),
..Default::default()
}),
..Default::default()
Expand Down

0 comments on commit aba17a7

Please sign in to comment.