From aba17a7216614f17a0a83a539cfb65cbfd792f24 Mon Sep 17 00:00:00 2001 From: Graham Esau Date: Fri, 12 Jun 2020 21:43:35 +0100 Subject: [PATCH] Use examples from JSON schemas (#11) --- examples/json-web-api/Cargo.toml | 2 +- examples/json-web-api/src/main.rs | 5 +++++ okapi/Cargo.toml | 2 +- rocket-okapi/Cargo.toml | 2 +- rocket-okapi/src/gen.rs | 16 ++++++++++------ 5 files changed, 18 insertions(+), 9 deletions(-) diff --git a/examples/json-web-api/Cargo.toml b/examples/json-web-api/Cargo.toml index 4560722..b112ca8 100644 --- a/examples/json-web-api/Cargo.toml +++ b/examples/json-web-api/Cargo.toml @@ -5,7 +5,7 @@ authors = ["Graham Esau "] 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 } diff --git a/examples/json-web-api/src/main.rs b/examples/json-web-api/src/main.rs index f7201c5..6d8be62 100644 --- a/examples/json-web-api/src/main.rs +++ b/examples/json-web-api/src/main.rs @@ -16,9 +16,14 @@ struct User { user_id: u64, username: String, #[serde(default)] + #[schemars(example = "example_email")] email: Option, } +fn example_email() -> &'static str { + "test@example.com" +} + /// # Get all users /// /// Returns all users in the system. diff --git a/okapi/Cargo.toml b/okapi/Cargo.toml index 4440f1a..de1ede6 100644 --- a/okapi/Cargo.toml +++ b/okapi/Cargo.toml @@ -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" diff --git a/rocket-okapi/Cargo.toml b/rocket-okapi/Cargo.toml index 0fd9759..fa82494 100644 --- a/rocket-okapi/Cargo.toml +++ b/rocket-okapi/Cargo.toml @@ -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 } diff --git a/rocket-okapi/src/gen.rs b/rocket-okapi/src/gen.rs index a28000a..bea4ddd 100644 --- a/rocket-okapi/src/gen.rs +++ b/rocket-okapi/src/gen.rs @@ -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: { @@ -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()