Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update schemars for preserve_order #31

Merged
merged 2 commits into from
Jun 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions examples/json-web-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Graham Esau <[email protected]>"]
edition = "2018"

[dependencies]
schemars = "0.7.0"
schemars = { version = "0.8.0-alpha-2", features = ["preserve_order"] }
okapi = { version = "0.4.0", path = "../../okapi" }
rocket_okapi = { version = "0.5.1", path = "../../rocket-okapi" }
rocket = { version = "0.4.3", default-features = false }
Expand All @@ -15,4 +15,3 @@ serde = "1.0"
version = "0.4.3"
default-features = false
features = ["json"]

2 changes: 1 addition & 1 deletion okapi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ keywords = ["rust", "openapi", "swagger"]
derive_json_schema = ["schemars/derive_json_schema"]

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

[dependencies]
schemars = "0.7.0"
schemars = { version = "0.8.0-alpha-2" }
okapi = { version = "0.4.0", path = "../okapi" }
rocket_okapi_codegen = { version = "=0.5.1", path = "../rocket-okapi-codegen" }
rocket = { version = "0.4.3", default-features = false }
Expand Down
36 changes: 22 additions & 14 deletions rocket-okapi/src/gen.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
use crate::settings::OpenApiSettings;
use crate::OperationInfo;
use okapi::openapi3::*;
use okapi::Map;
use rocket::http::Method;
use schemars::gen::SchemaGenerator;
use schemars::schema::SchemaObject;
use schemars::JsonSchema;
use std::collections::{hash_map::Entry as HashEntry, HashMap};
use schemars::{Map, MapEntry};
use std::collections::HashMap;
use std::iter::FromIterator;

/// A struct that visits all `rocket::Route`s, and aggregates information about them.
#[derive(Debug, Clone)]
pub struct OpenApiGenerator {
settings: OpenApiSettings,
schema_generator: SchemaGenerator,
operations: HashMap<(String, Method), Operation>,
operations: Map<String, HashMap<Method, Operation>>,
}

impl OpenApiGenerator {
Expand All @@ -33,15 +33,21 @@ impl OpenApiGenerator {
// TODO do this outside add_operation
op.operation.operation_id = Some(op_id.trim_start_matches(':').replace("::", "_"));
}
match self.operations.entry((op.path, op.method)) {
HashEntry::Occupied(e) => {
let (path, method) = e.key();
panic!(
"An OpenAPI operation has already been added for {} {}",
method, path
);
match self.operations.entry(op.path) {
MapEntry::Occupied(mut e) => {
let map = e.get_mut();
if map.insert(op.method, op.operation).is_some() {
// This will trow a warning if 2 routes have the same path and method
// This is allowed by Rocket when a ranking is given for example: `#[get("/user", rank = 2)]`
// See: https://rocket.rs/v0.4/guide/requests/#forwarding
println!("Warning: Operation replaced for {}:{}", op.method, e.key());
}
}
MapEntry::Vacant(e) => {
let mut map = HashMap::new();
map.insert(op.method, op.operation);
e.insert(map);
}
HashEntry::Vacant(e) => e.insert(op.operation),
};
}

Expand All @@ -66,9 +72,11 @@ impl OpenApiGenerator {
openapi: "3.0.0".to_owned(),
paths: {
let mut paths = Map::new();
for ((path, method), op) in self.operations {
let path_item = paths.entry(path).or_default();
set_operation(path_item, method, op);
for (path, map) in self.operations {
for (method, op) in map {
let path_item = paths.entry(path.clone()).or_default();
set_operation(path_item, method, op);
}
}
paths
},
Expand Down
4 changes: 2 additions & 2 deletions rocket-okapi/src/response/responder_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ impl<T: JsonSchema + Serialize> OpenApiResponder<'_> for Json<T> {
}

impl OpenApiResponder<'_> for JsonValue {
fn responses(gen: &mut OpenApiGenerator) -> Result {
fn responses(_gen: &mut OpenApiGenerator) -> Result {
let mut responses = Responses::default();
let schema = gen.schema_generator().schema_for_any();
let schema = schemars::schema::Schema::Bool(true);
add_schema_response(&mut responses, 200, "application/json", schema.into())?;
Ok(responses)
}
Expand Down
2 changes: 1 addition & 1 deletion rocket-okapi/src/settings.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use schemars::gen::SchemaSettings;

/// Settings which are used to customise the behaviour of the `OpenApiGenerator`.
#[derive(Debug, PartialEq, Clone)]
#[derive(Debug, Clone)]
pub struct OpenApiSettings {
/// Settings to customise how JSON Schemas are generated.
pub schema_settings: SchemaSettings,
Expand Down