use serde_json::Value; use crate::rest::{ model::{ImportFinding, RestImportParameter, SourceLocation}, recommendations::{document_blocker, operation_finding}, }; pub(super) fn servers(root: &Value) -> Vec<(ImportFinding, SourceLocation)> { let mut findings = Vec::new(); if let Some(host) = root.get("host") && (!host.is_string() || host.as_str().is_some_and(|host| { host.is_empty() || host.contains('@') || host.chars().any(char::is_whitespace) })) { push( &mut findings, "invalid_server", "Swagger host неверен или содержит userinfo.", "/host", ); } if let Some(base_path) = root.get("basePath") && (!base_path.is_string() || base_path.as_str().is_some_and(|path| { (!path.is_empty() && !path.starts_with('/')) || path.chars().any(char::is_control) })) { push( &mut findings, "invalid_server", "Swagger basePath имеет неверный формат.", "/basePath", ); } if let Some(schemes) = root.get("schemes").and_then(Value::as_array) { for (index, scheme) in schemes.iter().enumerate() { if !scheme .as_str() .is_some_and(|scheme| matches!(scheme, "http" | "https")) { push( &mut findings, "invalid_server", "Swagger scheme должен быть http или https.", &format!("/schemes/{index}"), ); } } } findings } pub(super) fn operation( root: &Value, operation: &Value, operation_pointer: &str, ) -> Vec<(ImportFinding, SourceLocation)> { let mut findings = Vec::new(); for (media, location) in media_with_locations(root, operation, operation_pointer, "consumes") { if !supported_request_media(&media) { findings.push(( document_blocker( "unsupported_consumes", format!("Content type запроса {media} не поддерживается."), ), location, )); } } for (media, location) in media_with_locations(root, operation, operation_pointer, "produces") { if !supported_response_media(&media) { findings.push(( document_blocker( "unsupported_produces", format!("Content type ответа {media} не поддерживается."), ), location, )); } } if let Some(responses) = operation.get("responses").and_then(Value::as_object) { for (status, response) in responses { if let Some(schema) = response.get("schema") { super::openapi3_findings::schema_findings( schema, &format!("{operation_pointer}/responses/{}/schema", escape(status)), &mut findings, ); } } } findings } pub(super) fn form_data_parameter( parameter: &Value, schema: Option<&Value>, pointer: &str, ) -> Vec<(ImportFinding, SourceLocation)> { let Some(schema) = schema else { return Vec::new(); }; let mut findings = Vec::new(); super::openapi3_findings::schema_findings(schema, pointer, &mut findings); super::openapi3_findings::form_field_schema_findings(schema, pointer, &mut findings); if schema.get("type").and_then(Value::as_str) == Some("array") && parameter.get("collectionFormat").and_then(Value::as_str) != Some("multi") { let finding_pointer = parameter .get("collectionFormat") .map(|_| format!("{pointer}/collectionFormat")) .unwrap_or_else(|| pointer.to_owned()); push( &mut findings, "unsupported_parameter_style", "Swagger formData array требует collectionFormat: multi; csv/default не соответствует repeat-key wire contract.", &finding_pointer, ); } findings } fn media_with_locations( root: &Value, operation: &Value, operation_pointer: &str, field: &str, ) -> Vec<(String, SourceLocation)> { let (value, pointer) = if let Some(value) = operation.get(field) { (value, format!("{operation_pointer}/{field}")) } else if let Some(value) = root.get(field) { (value, format!("/{field}")) } else { return Vec::new(); }; value .as_array() .into_iter() .flatten() .enumerate() .filter_map(|(index, value)| { value.as_str().map(|value| { ( value.to_owned(), SourceLocation { pointer: format!("{pointer}/{index}"), }, ) }) }) .collect() } pub(super) fn legacy_operation(path: &str, operation: &Value) -> Vec { if operation .get("consumes") .and_then(Value::as_array) .is_some_and(|items| { !items .iter() .any(|item| item.as_str().is_some_and(|value| value.contains("json"))) }) { vec![operation_finding( path, "unsupported_consumes", "Метод использует content type без JSON. Проверьте настройки тела запроса после импорта.", )] } else { Vec::new() } } pub(super) fn path_parameters( path: &str, parameters: &[RestImportParameter], operation_pointer: &str, ) -> Vec<(ImportFinding, SourceLocation)> { super::openapi3_findings::path_parameters(path, parameters, operation_pointer) } pub(super) fn unsupported_methods(path_item: &Value) -> Vec<&str> { const NON_METHOD: &[&str] = &["$ref", "parameters"]; const SUPPORTED: &[&str] = &["get", "post", "put", "patch", "delete", "head", "options"]; path_item .as_object() .into_iter() .flat_map(|item| item.keys()) .map(String::as_str) .filter(|name| { !name.starts_with("x-") && !NON_METHOD.contains(name) && !SUPPORTED.contains(name) }) .collect() } fn supported_request_media(media: &str) -> bool { matches!( media, "application/json" | "application/x-www-form-urlencoded" | "multipart/form-data" ) || media.ends_with("+json") } fn supported_response_media(media: &str) -> bool { media == "application/json" || media.ends_with("+json") } fn push( findings: &mut Vec<(ImportFinding, SourceLocation)>, code: &str, message: &str, pointer: &str, ) { findings.push(( document_blocker(code, message), SourceLocation { pointer: pointer.to_owned(), }, )); } fn escape(value: &str) -> String { value.replace('~', "~0").replace('/', "~1") }