G: G4 document every error code and both content types per response

This commit is contained in:
ari committed 2026-10-20 15:54:13 -04:00
1 parent 34a1e1644c
commit 214e074cca
5 files changed
+82 -22

No files matched your search

+58 -16
View File
@@ -294,6 +294,9 @@ reflected_schema!(ContainerSummary, ContainerStatus, LogLine);
/// schemars output, so a keyword this function does not know fails a test
/// instead of vanishing from the document.
fn convert(value: &Value) -> RefOr<Schema> {
// `reflected` inlines every nested schema, so a `$ref` cannot occur; a
// keyword this function does not know is dropped, which the parity test
// turns into a failure as soon as a DTO starts emitting one.
let object = value.as_object().cloned().unwrap_or_default();
let description = object.get("description").and_then(Value::as_str);
if let Some(items) = list(&object, "oneOf") {
@@ -368,22 +371,27 @@ fn list<'a>(object: &'a Map<String, Value>, key: &str) -> Option<&'a Vec<Value>>
}
fn schema_type(value: Option<&Value>) -> SchemaType {
match value {
None => SchemaType::AnyValue,
Some(Value::String(name)) => SchemaType::Type(scalar_type(name)),
Some(Value::Array(names)) => names
.iter()
.filter_map(Value::as_str)
.map(scalar_type)
.collect::<Vec<_>>()
.into_iter()
.collect::<SchemaType>(),
Some(_) => SchemaType::AnyValue,
let names: Vec<&str> = match value {
Some(Value::String(name)) => vec![name.as_str()],
Some(Value::Array(names)) => names.iter().filter_map(Value::as_str).collect(),
_ => return SchemaType::AnyValue,
};
match names
.iter()
.filter_map(|name| scalar_type(name))
.collect::<Vec<Type>>()
.as_slice()
{
[] => SchemaType::AnyValue,
[single] => SchemaType::Type(single.clone()),
many => SchemaType::Array(many.to_vec()),
}
}
fn scalar_type(name: &str) -> Type {
match name {
/// `None` for a name utoipa cannot express, which leaves the type out rather
/// than inventing one.
fn scalar_type(name: &str) -> Option<Type> {
Some(match name {
"string" => Type::String,
"integer" => Type::Integer,
"number" => Type::Number,
@@ -391,8 +399,8 @@ fn scalar_type(name: &str) -> Type {
"object" => Type::Object,
"array" => Type::Array,
"null" => Type::Null,
_ => Type::Object,
}
_ => return None,
})
}
fn number_value(value: &Value) -> Option<utoipa::Number> {
@@ -461,7 +469,9 @@ handwritten_schema!(
"bad_request",
"unauthorized",
"unavailable",
"internal"
"internal",
"method_not_allowed",
"forbidden_host",
]
.into_iter()
.map(Value::from),
@@ -799,6 +809,38 @@ mod tests {
);
}
/// The `code` enum is handwritten, so it is tied to the codes the server
/// can emit: one per `AppError` variant plus the two status-only bodies.
#[test]
fn every_error_code_is_documented() {
let doc = document(false);
let schema = component::<ErrorBody>(&doc, "ErrorBody");
let documented: Vec<&str> = schema["properties"]["error"]["properties"]["code"]["enum"]
.as_array()
.unwrap()
.iter()
.map(|v| v.as_str().unwrap())
.collect();
let errors = [
crate::error::AppError::NotFound(String::new()),
crate::error::AppError::BadRequest(String::new()),
crate::error::AppError::Unauthorized(String::new()),
crate::error::AppError::Unavailable(String::new()),
crate::error::AppError::Internal(String::new()),
];
for error in &errors {
assert!(
documented.contains(&error.code()),
"{} is not documented",
error.code()
);
}
for code in ["method_not_allowed", "forbidden_host"] {
assert!(documented.contains(&code), "{code} is not documented");
}
assert_eq!(documented.len(), errors.len() + 2);
}
#[test]
fn auth_state_enum_matches_serialization() {
let doc = document(true);