validator-api: add swagger openapi

This commit is contained in:
Jon Häggblad
2022-04-27 13:26:01 +02:00
parent c429e67d68
commit 433604dec7
11 changed files with 151 additions and 19 deletions
+52 -6
View File
@@ -3,9 +3,15 @@
use crate::node_status_api::utils::NodeUptimes;
use crate::storage::models::NodeStatus;
use okapi::openapi3::{Responses, SchemaObject};
use rocket::http::{ContentType, Status};
use rocket::response::{self, Responder, Response};
use rocket::Request;
use rocket_okapi::gen::OpenApiGenerator;
use rocket_okapi::response::OpenApiResponderInner;
use schemars::JsonSchema;
use schemars::gen::SchemaGenerator;
use schemars::schema::{Schema, InstanceType};
use serde::{Deserialize, Serialize};
use std::convert::TryFrom;
use std::fmt::{self, Display, Formatter};
@@ -17,7 +23,7 @@ use time::OffsetDateTime;
pub struct InvalidUptime;
// value in range 0-100
#[derive(Clone, Copy, Serialize, Deserialize, Debug, Default)]
#[derive(Clone, Copy, Serialize, Deserialize, Debug, Default, JsonSchema)]
pub struct Uptime(u8);
impl Uptime {
@@ -97,7 +103,7 @@ impl TryFrom<i64> for Uptime {
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
pub struct MixnodeStatusReport {
pub(crate) identity: String,
pub(crate) owner: String,
@@ -134,7 +140,7 @@ impl MixnodeStatusReport {
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
pub struct GatewayStatusReport {
pub(crate) identity: String,
pub(crate) owner: String,
@@ -171,7 +177,7 @@ impl GatewayStatusReport {
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
pub struct MixnodeUptimeHistory {
pub(crate) identity: String,
pub(crate) owner: String,
@@ -189,7 +195,7 @@ impl MixnodeUptimeHistory {
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
pub struct GatewayUptimeHistory {
pub(crate) identity: String,
pub(crate) owner: String,
@@ -207,7 +213,7 @@ impl GatewayUptimeHistory {
}
}
#[derive(Clone, Serialize, Deserialize, Debug)]
#[derive(Clone, Serialize, Deserialize, Debug, JsonSchema)]
pub struct HistoricalUptime {
// ISO 8601 date string
// I think this is more than enough, we don't need the uber precision of timezone offsets, etc
@@ -240,6 +246,46 @@ impl<'r, 'o: 'r> Responder<'r, 'o> for ErrorResponse {
}
}
impl JsonSchema for ErrorResponse {
fn schema_name() -> String {
"ErrorResponse".to_owned()
}
fn json_schema(gen: &mut SchemaGenerator) -> Schema {
let mut schema_object = SchemaObject {
instance_type: Some(InstanceType::Object.into()),
..SchemaObject::default()
};
let object_validation = schema_object.object();
object_validation
.properties
.insert("error_message".to_owned(), gen.subschema_for::<String>());
object_validation
.required
.insert("error_message".to_owned());
// WIP(JON): is this valid? Using the inner type u16 explicitly
object_validation
.properties
.insert("status".to_owned(), gen.subschema_for::<u16>());
object_validation
.required
.insert("status".to_owned());
Schema::Object(schema_object)
}
}
impl OpenApiResponderInner for ErrorResponse {
fn responses(_gen: &mut OpenApiGenerator) -> rocket_okapi::Result<Responses> {
let responses = Responses::default();
// WIP(JON)
Ok(responses)
}
}
#[derive(Debug, thiserror::Error)]
pub enum ValidatorApiStorageError {
MixnodeReportNotFound(String),