Skip to content

Commit 1c40f4d

Browse files
authored
Update schemars for preserve_order (#31)
1 parent 3e4d4cc commit 1c40f4d

File tree

6 files changed

+28
-21
lines changed

6 files changed

+28
-21
lines changed

examples/json-web-api/Cargo.toml

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ authors = ["Graham Esau <[email protected]>"]
55
edition = "2018"
66

77
[dependencies]
8-
schemars = "0.7.0"
8+
schemars = { version = "0.8.0-alpha-2", features = ["preserve_order"] }
99
okapi = { version = "0.4.0", path = "../../okapi" }
1010
rocket_okapi = { version = "0.5.1", path = "../../rocket-okapi" }
1111
rocket = { version = "0.4.3", default-features = false }
@@ -15,4 +15,3 @@ serde = "1.0"
1515
version = "0.4.3"
1616
default-features = false
1717
features = ["json"]
18-

okapi/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ keywords = ["rust", "openapi", "swagger"]
1212
derive_json_schema = ["schemars/derive_json_schema"]
1313

1414
[dependencies]
15-
schemars = "0.7.0"
15+
schemars = { version = "0.8.0-alpha-2" }
1616
serde = { version = "1.0", features = ["derive"] }
1717
serde_json = "1.0"

rocket-okapi/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ license = "MIT"
99
keywords = ["rust", "openapi", "swagger", "rocket"]
1010

1111
[dependencies]
12-
schemars = "0.7.0"
12+
schemars = { version = "0.8.0-alpha-2" }
1313
okapi = { version = "0.4.0", path = "../okapi" }
1414
rocket_okapi_codegen = { version = "=0.5.1", path = "../rocket-okapi-codegen" }
1515
rocket = { version = "0.4.3", default-features = false }

rocket-okapi/src/gen.rs

+22-14
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
11
use crate::settings::OpenApiSettings;
22
use crate::OperationInfo;
33
use okapi::openapi3::*;
4-
use okapi::Map;
54
use rocket::http::Method;
65
use schemars::gen::SchemaGenerator;
76
use schemars::schema::SchemaObject;
87
use schemars::JsonSchema;
9-
use std::collections::{hash_map::Entry as HashEntry, HashMap};
8+
use schemars::{Map, MapEntry};
9+
use std::collections::HashMap;
1010
use std::iter::FromIterator;
1111

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

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

@@ -66,9 +72,11 @@ impl OpenApiGenerator {
6672
openapi: "3.0.0".to_owned(),
6773
paths: {
6874
let mut paths = Map::new();
69-
for ((path, method), op) in self.operations {
70-
let path_item = paths.entry(path).or_default();
71-
set_operation(path_item, method, op);
75+
for (path, map) in self.operations {
76+
for (method, op) in map {
77+
let path_item = paths.entry(path.clone()).or_default();
78+
set_operation(path_item, method, op);
79+
}
7280
}
7381
paths
7482
},

rocket-okapi/src/response/responder_impls.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ impl<T: JsonSchema + Serialize> OpenApiResponder<'_> for Json<T> {
2020
}
2121

2222
impl OpenApiResponder<'_> for JsonValue {
23-
fn responses(gen: &mut OpenApiGenerator) -> Result {
23+
fn responses(_gen: &mut OpenApiGenerator) -> Result {
2424
let mut responses = Responses::default();
25-
let schema = gen.schema_generator().schema_for_any();
25+
let schema = schemars::schema::Schema::Bool(true);
2626
add_schema_response(&mut responses, 200, "application/json", schema.into())?;
2727
Ok(responses)
2828
}

rocket-okapi/src/settings.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use schemars::gen::SchemaSettings;
22

33
/// Settings which are used to customise the behaviour of the `OpenApiGenerator`.
4-
#[derive(Debug, PartialEq, Clone)]
4+
#[derive(Debug, Clone)]
55
pub struct OpenApiSettings {
66
/// Settings to customise how JSON Schemas are generated.
77
pub schema_settings: SchemaSettings,

0 commit comments

Comments
 (0)