Skip to content

Commit

Permalink
Bump schemars from 0.8.17 to 0.8.19 (#1003)
Browse files Browse the repository at this point in the history
* Bump schemars from 0.8.17 to 0.8.19

Bumps [schemars](https://github.com/GREsau/schemars) from 0.8.17 to 0.8.19.
- [Release notes](https://github.com/GREsau/schemars/releases)
- [Changelog](https://github.com/GREsau/schemars/blob/master/CHANGELOG.md)
- [Commits](GREsau/schemars@v0.8.17...v0.8.19)

---
updated-dependencies:
- dependency-name: schemars
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

* hand-write impl JsonSchema for HttpErrorResponseBody

---------

Signed-off-by: dependabot[bot] <support@github.com>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Adam H. Leventhal <ahl@oxide.computer>
  • Loading branch information
dependabot[bot] and ahl committed May 14, 2024
1 parent 3a57aa5 commit cc3ccfc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
8 changes: 4 additions & 4 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions dropshot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ version = "1.8.0"
features = [ "serde", "v4" ]

[dependencies.schemars]
version = "0.8.17"
version = "0.8.19"
features = [ "uuid1" ]

[dev-dependencies]
Expand All @@ -103,7 +103,7 @@ version = "1.7.0"
features = ["alloc"]

[dev-dependencies.schemars]
version = "0.8.17"
version = "0.8.19"
features = [ "chrono", "uuid1" ]

[dev-dependencies.slog]
Expand Down
56 changes: 47 additions & 9 deletions dropshot/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// Copyright 2020 Oxide Computer Company
// Copyright 2024 Oxide Computer Company

//! Generic server error handling facilities
//!
//! Error handling in an API
Expand Down Expand Up @@ -98,21 +99,58 @@ pub struct HttpError {
/// Body of an HTTP response for an `HttpError`. This type can be used to
/// deserialize an HTTP response corresponding to an error in order to access the
/// error code, message, etc.
// TODO: does this need to be pub if it's going to be expressed in the OpenAPI
// output?
#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[schemars(rename = "Error")]
#[schemars(description = "Error information from a response.")]
#[derive(Debug, Deserialize, Serialize)]
pub struct HttpErrorResponseBody {
pub request_id: String,
// The combination of default and required removes "nullable" from the
// OpenAPI-flavored JSON Schema output.
#[schemars(default, required)]
#[serde(skip_serializing_if = "Option::is_none")]
pub error_code: Option<String>,
pub message: String,
}

// We hand-roll our JSON schema to avoid `error_code` being "nullable".
impl JsonSchema for HttpErrorResponseBody {
fn schema_name() -> String {
"Error".to_string()
}

fn json_schema(
gen: &mut schemars::gen::SchemaGenerator,
) -> schemars::schema::Schema {
let str_schema = String::json_schema(gen);

schemars::schema::SchemaObject {
metadata: Some(
schemars::schema::Metadata {
description: Some(
"Error information from a response.".into(),
),
..Default::default()
}
.into(),
),
instance_type: Some(schemars::schema::InstanceType::Object.into()),
object: Some(
schemars::schema::ObjectValidation {
required: ["message".into(), "request_id".into()]
.into_iter()
.collect(),
properties: [
("error_code".into(), str_schema.clone()),
("message".into(), str_schema.clone()),
("request_id".into(), str_schema.clone()),
]
.into_iter()
.collect(),
..Default::default()
}
.into(),
),
..Default::default()
}
.into()
}
}

impl From<HyperError> for HttpError {
fn from(error: HyperError) -> Self {
// TODO-correctness dig deeper into the various cases to make sure this
Expand Down

0 comments on commit cc3ccfc

Please sign in to comment.