feat(import): preserve supported HTTP contracts
This commit is contained in:
1 parent
083974c27f
commit
f8ee7c9bfe
87 files changed
+7496
-534
No files matched your search
@@ -4,6 +4,8 @@ use thiserror::Error;
|
||||
pub enum SchemaError {
|
||||
#[error("missing required field at {path}")]
|
||||
MissingField { path: String },
|
||||
#[error("unexpected field at {path}")]
|
||||
UnexpectedField { path: String },
|
||||
#[error("invalid value at {path}: expected {expected}, got {actual}")]
|
||||
InvalidType {
|
||||
path: String,
|
||||
@@ -25,6 +27,10 @@ impl SchemaError {
|
||||
Self::MissingField { path: path.into() }
|
||||
}
|
||||
|
||||
pub fn unexpected_field(path: impl Into<String>) -> Self {
|
||||
Self::UnexpectedField { path: path.into() }
|
||||
}
|
||||
|
||||
pub fn invalid_type(
|
||||
path: impl Into<String>,
|
||||
expected: &'static str,
|
||||
|
||||
@@ -4,4 +4,7 @@ mod normalize;
|
||||
mod validate;
|
||||
|
||||
pub use error::SchemaError;
|
||||
pub use model::{Schema, SchemaKind, XmlNodeKind, XmlQualifiedName, XmlSchemaBinding};
|
||||
pub use model::{
|
||||
CLOSED_OBJECT_SCHEMA_FORMAT, Schema, SchemaKind, XmlNodeKind, XmlQualifiedName,
|
||||
XmlSchemaBinding,
|
||||
};
|
||||
@@ -3,6 +3,8 @@ use std::collections::BTreeMap;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
pub const CLOSED_OBJECT_SCHEMA_FORMAT: &str = "crank-closed-object-v1";
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum XmlNodeKind {
|
||||
@@ -50,6 +52,9 @@ pub enum SchemaKind {
|
||||
pub struct Schema {
|
||||
#[serde(rename = "type")]
|
||||
pub kind: SchemaKind,
|
||||
/// OpenAPI/JSON Schema format hint, preserved for REST projections.
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub format: Option<String>,
|
||||
#[serde(default, skip_serializing_if = "Option::is_none")]
|
||||
pub description: Option<String>,
|
||||
#[serde(default)]
|
||||
@@ -109,6 +114,7 @@ mod tests {
|
||||
"email".to_owned(),
|
||||
Schema {
|
||||
kind: SchemaKind::String,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -122,6 +128,7 @@ mod tests {
|
||||
|
||||
let schema = Schema {
|
||||
kind: SchemaKind::Object,
|
||||
format: None,
|
||||
description: Some("User input".to_owned()),
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -144,6 +151,7 @@ mod tests {
|
||||
fn schema_serializes_type_field() {
|
||||
let schema = Schema {
|
||||
kind: SchemaKind::Array,
|
||||
format: None,
|
||||
description: None,
|
||||
required: false,
|
||||
nullable: false,
|
||||
@@ -151,6 +159,7 @@ mod tests {
|
||||
fields: BTreeMap::new(),
|
||||
items: Some(Box::new(Schema {
|
||||
kind: SchemaKind::String,
|
||||
format: None,
|
||||
description: None,
|
||||
required: false,
|
||||
nullable: false,
|
||||
@@ -174,6 +183,7 @@ mod tests {
|
||||
fn schema_roundtrips_through_yaml() {
|
||||
let schema = Schema {
|
||||
kind: SchemaKind::Object,
|
||||
format: None,
|
||||
description: Some("Lead output".to_owned()),
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -182,6 +192,7 @@ mod tests {
|
||||
"id".to_owned(),
|
||||
Schema {
|
||||
kind: SchemaKind::String,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
|
||||
@@ -9,6 +9,7 @@ impl Schema {
|
||||
match value {
|
||||
Value::Null => Self {
|
||||
kind: SchemaKind::Null,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: true,
|
||||
@@ -33,6 +34,7 @@ impl Schema {
|
||||
fn scalar_schema(kind: SchemaKind) -> Schema {
|
||||
Schema {
|
||||
kind,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -47,6 +49,7 @@ fn scalar_schema(kind: SchemaKind) -> Schema {
|
||||
fn array_schema(items: &[Value]) -> Schema {
|
||||
Schema {
|
||||
kind: SchemaKind::Array,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -63,6 +66,7 @@ fn array_schema(items: &[Value]) -> Schema {
|
||||
fn object_schema(fields: &Map<String, Value>) -> Schema {
|
||||
Schema {
|
||||
kind: SchemaKind::Object,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -149,6 +153,7 @@ mod tests {
|
||||
schema,
|
||||
Schema {
|
||||
kind: SchemaKind::Null,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: true,
|
||||
|
||||
@@ -57,6 +57,20 @@ fn validate_object(schema: &Schema, value: &Value, path: &str) -> Result<(), Sch
|
||||
}
|
||||
}
|
||||
|
||||
// The inline-file envelope is a closed contract. In particular paths,
|
||||
// URLs, and artifact references must not be smuggled beside `data_base64`.
|
||||
if matches!(
|
||||
schema.format.as_deref(),
|
||||
Some("binary" | crate::CLOSED_OBJECT_SCHEMA_FORMAT)
|
||||
) && let Some(unexpected) = object
|
||||
.keys()
|
||||
.find(|field| !schema.fields.contains_key(*field))
|
||||
{
|
||||
return Err(SchemaError::unexpected_field(format!(
|
||||
"{path}.{unexpected}"
|
||||
)));
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -162,6 +176,7 @@ mod tests {
|
||||
fn validates_nested_object_shape() {
|
||||
let schema = Schema {
|
||||
kind: SchemaKind::Object,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -170,6 +185,7 @@ mod tests {
|
||||
"lead".to_owned(),
|
||||
Schema {
|
||||
kind: SchemaKind::Object,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -178,6 +194,7 @@ mod tests {
|
||||
"email".to_owned(),
|
||||
Schema {
|
||||
kind: SchemaKind::String,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -211,6 +228,7 @@ mod tests {
|
||||
fn rejects_missing_required_field() {
|
||||
let schema = Schema {
|
||||
kind: SchemaKind::Object,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -219,6 +237,7 @@ mod tests {
|
||||
"email".to_owned(),
|
||||
Schema {
|
||||
kind: SchemaKind::String,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
@@ -243,6 +262,7 @@ mod tests {
|
||||
fn rejects_invalid_scalar_type() {
|
||||
let schema = Schema {
|
||||
kind: SchemaKind::Boolean,
|
||||
format: None,
|
||||
description: None,
|
||||
required: true,
|
||||
nullable: false,
|
||||
|
||||
Reference in new issue
Block a user