Add OpenAPI import backend
This commit is contained in:
1 parent
87cdb95b80
commit
a8aa3248c9
34 files changed
+2569
-27
No files matched your search
@@ -0,0 +1,79 @@
|
||||
use crank_mapping::{MappingRule, MappingSet};
|
||||
use crank_schema::{Schema, SchemaKind};
|
||||
|
||||
use crate::rest::model::{RestImportParameter, RestParameterLocation};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub struct BodyFieldMapping {
|
||||
pub input_name: String,
|
||||
pub body_path: String,
|
||||
pub required: bool,
|
||||
}
|
||||
|
||||
pub fn input_mapping(
|
||||
parameters: &[RestImportParameter],
|
||||
body_fields: &[BodyFieldMapping],
|
||||
) -> MappingSet {
|
||||
let mut rules = Vec::new();
|
||||
for parameter in parameters {
|
||||
let target_root = match parameter.location {
|
||||
RestParameterLocation::Path => "$.request.path",
|
||||
RestParameterLocation::Query => "$.request.query",
|
||||
RestParameterLocation::Header => "$.request.headers",
|
||||
};
|
||||
rules.push(MappingRule {
|
||||
source: format!("$.mcp.{}", parameter.name),
|
||||
target: format!("{target_root}.{}", parameter.name),
|
||||
required: parameter.required,
|
||||
default_value: None,
|
||||
transform: None,
|
||||
condition: None,
|
||||
notes: None,
|
||||
});
|
||||
}
|
||||
for field in body_fields {
|
||||
let target = if field.body_path.is_empty() {
|
||||
"$.request.body".to_owned()
|
||||
} else {
|
||||
format!("$.request.body.{}", field.body_path)
|
||||
};
|
||||
rules.push(MappingRule {
|
||||
source: format!("$.mcp.{}", field.input_name),
|
||||
target,
|
||||
required: field.required,
|
||||
default_value: None,
|
||||
transform: None,
|
||||
condition: None,
|
||||
notes: None,
|
||||
});
|
||||
}
|
||||
MappingSet { rules }
|
||||
}
|
||||
|
||||
pub fn output_mapping(output_schema: &Schema) -> MappingSet {
|
||||
let mut rules = Vec::new();
|
||||
if output_schema.kind == SchemaKind::Object && !output_schema.fields.is_empty() {
|
||||
for (name, field) in &output_schema.fields {
|
||||
rules.push(MappingRule {
|
||||
source: format!("$.response.body.{name}"),
|
||||
target: format!("$.output.{name}"),
|
||||
required: field.required,
|
||||
default_value: None,
|
||||
transform: None,
|
||||
condition: None,
|
||||
notes: None,
|
||||
});
|
||||
}
|
||||
} else {
|
||||
rules.push(MappingRule {
|
||||
source: "$.response.body".to_owned(),
|
||||
target: "$.output".to_owned(),
|
||||
required: true,
|
||||
default_value: None,
|
||||
transform: None,
|
||||
condition: None,
|
||||
notes: None,
|
||||
});
|
||||
}
|
||||
MappingSet { rules }
|
||||
}
|
||||
Reference in new issue
Block a user