Skip to main content

mockforge_core/openapi/
route.rs

1//! OpenAPI route generation from specifications
2//!
3//! This module provides functionality for generating Axum routes
4//! from OpenAPI path definitions.
5
6use crate::intelligent_behavior::config::Persona;
7use crate::openapi::response_selection::{ResponseSelectionMode, ResponseSelector};
8use crate::{ai_response::AiResponseConfig, openapi::spec::OpenApiSpec, Result};
9use openapiv3::{Operation, PathItem, ReferenceOr};
10use std::collections::BTreeMap;
11use std::sync::Arc;
12
13/// Extract path parameters from an OpenAPI path template
14fn extract_path_parameters(path_template: &str) -> Vec<String> {
15    let mut params = Vec::new();
16    let mut in_param = false;
17    let mut current_param = String::new();
18
19    for ch in path_template.chars() {
20        match ch {
21            '{' => {
22                in_param = true;
23                current_param.clear();
24            }
25            '}' => {
26                if in_param {
27                    params.push(current_param.clone());
28                    in_param = false;
29                }
30            }
31            ch if in_param => {
32                current_param.push(ch);
33            }
34            _ => {}
35        }
36    }
37
38    params
39}
40
41/// OpenAPI route wrapper with additional metadata
42#[derive(Debug, Clone)]
43pub struct OpenApiRoute {
44    /// The HTTP method
45    pub method: String,
46    /// The path pattern
47    pub path: String,
48    /// The OpenAPI operation
49    pub operation: Operation,
50    /// Route-specific metadata
51    pub metadata: BTreeMap<String, String>,
52    /// Path parameters extracted from the path
53    pub parameters: Vec<String>,
54    /// Reference to the OpenAPI spec for response generation
55    pub spec: Arc<OpenApiSpec>,
56    /// AI response configuration (parsed from x-mockforge-ai extension)
57    pub ai_config: Option<AiResponseConfig>,
58    /// Response selection mode (parsed from x-mockforge-response-selection extension)
59    pub response_selection_mode: ResponseSelectionMode,
60    /// Response selector for sequential/random modes (shared across requests)
61    pub response_selector: Arc<ResponseSelector>,
62    /// Active persona for consistent data generation (optional)
63    pub persona: Option<Arc<Persona>>,
64}
65
66impl OpenApiRoute {
67    /// Create a new OpenApiRoute
68    pub fn new(method: String, path: String, operation: Operation, spec: Arc<OpenApiSpec>) -> Self {
69        Self::new_with_persona(method, path, operation, spec, None)
70    }
71
72    /// Create a new OpenApiRoute with persona
73    pub fn new_with_persona(
74        method: String,
75        path: String,
76        operation: Operation,
77        spec: Arc<OpenApiSpec>,
78        persona: Option<Arc<Persona>>,
79    ) -> Self {
80        let parameters = extract_path_parameters(&path);
81
82        // Parse AI configuration from x-mockforge-ai vendor extension
83        let ai_config = Self::parse_ai_config(&operation);
84
85        // Parse response selection mode from x-mockforge-response-selection extension
86        let response_selection_mode = Self::parse_response_selection_mode(&operation);
87        let response_selector = Arc::new(ResponseSelector::new(response_selection_mode));
88
89        Self {
90            method,
91            path,
92            operation,
93            metadata: BTreeMap::new(),
94            parameters,
95            spec,
96            ai_config,
97            response_selection_mode,
98            response_selector,
99            persona,
100        }
101    }
102
103    /// Parse AI configuration from OpenAPI operation's vendor extensions
104    fn parse_ai_config(operation: &Operation) -> Option<AiResponseConfig> {
105        // Check for x-mockforge-ai extension
106        if let Some(ai_config_value) = operation.extensions.get("x-mockforge-ai") {
107            // Try to deserialize the AI config from the extension value
108            match serde_json::from_value::<AiResponseConfig>(ai_config_value.clone()) {
109                Ok(config) => {
110                    if config.is_active() {
111                        tracing::debug!(
112                            "Parsed AI config for operation {}: mode={:?}, prompt={:?}",
113                            operation.operation_id.as_deref().unwrap_or("unknown"),
114                            config.mode,
115                            config.prompt
116                        );
117                        return Some(config);
118                    }
119                }
120                Err(e) => {
121                    tracing::warn!(
122                        "Failed to parse x-mockforge-ai extension for operation {}: {}",
123                        operation.operation_id.as_deref().unwrap_or("unknown"),
124                        e
125                    );
126                }
127            }
128        }
129        None
130    }
131
132    /// Parse response selection mode from OpenAPI operation's vendor extensions
133    fn parse_response_selection_mode(operation: &Operation) -> ResponseSelectionMode {
134        // Check for environment variable override (per-operation or global)
135        let op_id = operation.operation_id.as_deref().unwrap_or("unknown");
136
137        // Try operation-specific env var first: MOCKFORGE_RESPONSE_SELECTION_<OPERATION_ID>
138        if let Ok(op_env_var) = std::env::var(format!(
139            "MOCKFORGE_RESPONSE_SELECTION_{}",
140            op_id.to_uppercase().replace('-', "_")
141        )) {
142            if let Some(mode) = ResponseSelectionMode::from_str(&op_env_var) {
143                tracing::debug!(
144                    "Using response selection mode from env var for operation {}: {:?}",
145                    op_id,
146                    mode
147                );
148                return mode;
149            }
150        }
151
152        // Check global env var: MOCKFORGE_RESPONSE_SELECTION_MODE
153        if let Ok(global_mode_str) = std::env::var("MOCKFORGE_RESPONSE_SELECTION_MODE") {
154            if let Some(mode) = ResponseSelectionMode::from_str(&global_mode_str) {
155                tracing::debug!("Using global response selection mode from env var: {:?}", mode);
156                return mode;
157            }
158        }
159
160        // Check for x-mockforge-response-selection extension
161        if let Some(selection_value) = operation.extensions.get("x-mockforge-response-selection") {
162            // Try to parse as string first
163            if let Some(mode_str) = selection_value.as_str() {
164                if let Some(mode) = ResponseSelectionMode::from_str(mode_str) {
165                    tracing::debug!(
166                        "Parsed response selection mode for operation {}: {:?}",
167                        op_id,
168                        mode
169                    );
170                    return mode;
171                }
172            }
173            // Try to parse as object with mode field
174            if let Some(obj) = selection_value.as_object() {
175                if let Some(mode_str) = obj.get("mode").and_then(|v| v.as_str()) {
176                    if let Some(mode) = ResponseSelectionMode::from_str(mode_str) {
177                        tracing::debug!(
178                            "Parsed response selection mode for operation {}: {:?}",
179                            op_id,
180                            mode
181                        );
182                        return mode;
183                    }
184                }
185            }
186            tracing::warn!(
187                "Failed to parse x-mockforge-response-selection extension for operation {}",
188                op_id
189            );
190        }
191        // Default to First mode
192        ResponseSelectionMode::First
193    }
194
195    /// Create an OpenApiRoute from an operation
196    pub fn from_operation(
197        method: &str,
198        path: String,
199        operation: &Operation,
200        spec: Arc<OpenApiSpec>,
201    ) -> Self {
202        Self::from_operation_with_persona(method, path, operation, spec, None)
203    }
204
205    /// Create a new OpenApiRoute from an operation with optional persona
206    pub fn from_operation_with_persona(
207        method: &str,
208        path: String,
209        operation: &Operation,
210        spec: Arc<OpenApiSpec>,
211        persona: Option<Arc<Persona>>,
212    ) -> Self {
213        Self::new_with_persona(method.to_string(), path, operation.clone(), spec, persona)
214    }
215
216    /// Convert OpenAPI path to Axum-compatible path format
217    pub fn axum_path(&self) -> String {
218        // Strip query string if present (some non-standard OpenAPI specs embed query params in path)
219        // Axum v0.7+ uses {param} format, same as OpenAPI
220        let path = self.path.split('?').next().unwrap_or(&self.path);
221
222        // Handle OData function call syntax: functionName(key='{param}',key2={param2})
223        // Convert to: functionName/{param}/{param2}
224        // This prevents Axum from panicking on multiple params per segment or invalid chars
225        if path.contains("(") && path.contains("={") {
226            let mut result = String::with_capacity(path.len());
227            let mut chars = path.chars().peekable();
228
229            while let Some(ch) = chars.next() {
230                if ch == '(' {
231                    // Extract params from inside parentheses
232                    let mut paren_content = String::new();
233                    for c in chars.by_ref() {
234                        if c == ')' {
235                            break;
236                        }
237                        paren_content.push(c);
238                    }
239                    // Parse key='{value}' or key={value} pairs
240                    for part in paren_content.split(',') {
241                        if let Some((_key, value)) = part.split_once('=') {
242                            let param = value.trim_matches(|c| c == '\'' || c == '"');
243                            result.push('/');
244                            result.push_str(param);
245                        }
246                    }
247                } else {
248                    result.push(ch);
249                }
250            }
251            return result;
252        }
253
254        path.to_string()
255    }
256
257    /// Returns true if this route's path can be registered with Axum's router.
258    ///
259    /// Paths that contain characters Axum can't handle (e.g., unmatched braces,
260    /// multiple params per segment after conversion) are considered invalid.
261    pub fn is_valid_axum_path(&self) -> bool {
262        let path = self.axum_path();
263        // Each segment may contain at most one `{param}` capture
264        for segment in path.split('/') {
265            let brace_count = segment.matches('{').count();
266            if brace_count > 1 {
267                return false;
268            }
269            // A segment with a param must be ONLY the param (e.g. `{id}` not `prefix{id}suffix`)
270            // unless it's a wildcard. Axum allows `{*rest}` as a catch-all.
271            if brace_count == 1
272                && segment
273                    != format!(
274                        "{{{}}}",
275                        segment
276                            .trim_matches(|c: char| c != '{' && c != '}')
277                            .trim_matches(|c| c == '{' || c == '}')
278                    )
279            {
280                // Segment has a param mixed with literal text — check if it's truly invalid
281                // Axum 0.8 allows `{param}` as full segment only
282                if !segment.starts_with('{') || !segment.ends_with('}') {
283                    return false;
284                }
285            }
286        }
287        true
288    }
289
290    /// Add metadata to the route
291    pub fn with_metadata(mut self, key: String, value: String) -> Self {
292        self.metadata.insert(key, value);
293        self
294    }
295
296    /// Generate a mock response with status code for this route (async version with AI support)
297    ///
298    /// This method checks if AI response generation is configured and uses it if available,
299    /// otherwise falls back to standard OpenAPI response generation.
300    ///
301    /// # Arguments
302    /// * `context` - The request context for AI prompt expansion
303    /// * `ai_generator` - Optional AI generator implementation for actual LLM calls
304    pub async fn mock_response_with_status_async(
305        &self,
306        context: &crate::ai_response::RequestContext,
307        ai_generator: Option<&dyn crate::openapi::response::AiGenerator>,
308    ) -> (u16, serde_json::Value) {
309        use crate::openapi::response::ResponseGenerator;
310
311        // Find the first available status code from the OpenAPI spec
312        let status_code = self.find_first_available_status_code();
313
314        // Check if AI response generation is configured
315        if let Some(ai_config) = &self.ai_config {
316            if ai_config.is_active() {
317                tracing::info!(
318                    "Using AI-assisted response generation for {} {}",
319                    self.method,
320                    self.path
321                );
322
323                match ResponseGenerator::generate_ai_response(ai_config, context, ai_generator)
324                    .await
325                {
326                    Ok(response_body) => {
327                        tracing::debug!(
328                            "AI response generated successfully for {} {}: {:?}",
329                            self.method,
330                            self.path,
331                            response_body
332                        );
333                        return (status_code, response_body);
334                    }
335                    Err(e) => {
336                        tracing::warn!(
337                            "AI response generation failed for {} {}: {}, falling back to standard generation",
338                            self.method,
339                            self.path,
340                            e
341                        );
342                        // Continue to standard generation on error
343                    }
344                }
345            }
346        }
347
348        // Standard OpenAPI-based response generation
349        let expand_tokens = std::env::var("MOCKFORGE_RESPONSE_TEMPLATE_EXPAND")
350            .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
351            .unwrap_or(false);
352
353        // Use response selection mode for multiple examples
354        let mode = Some(self.response_selection_mode);
355        let selector = Some(self.response_selector.as_ref());
356
357        // Get persona reference for response generation
358        let persona_ref = self.persona.as_deref();
359
360        match ResponseGenerator::generate_response_with_expansion_and_mode_and_persona(
361            &self.spec,
362            &self.operation,
363            status_code,
364            Some("application/json"),
365            expand_tokens,
366            mode,
367            selector,
368            persona_ref,
369        ) {
370            Ok(response_body) => {
371                tracing::debug!(
372                    "ResponseGenerator succeeded for {} {} with status {}: {:?}",
373                    self.method,
374                    self.path,
375                    status_code,
376                    response_body
377                );
378                (status_code, response_body)
379            }
380            Err(e) => {
381                tracing::debug!(
382                    "ResponseGenerator failed for {} {}: {}, using fallback",
383                    self.method,
384                    self.path,
385                    e
386                );
387                // Fallback to simple mock response if schema-based generation fails
388                let response_body = serde_json::json!({
389                    "message": format!("Mock response for {} {}", self.method, self.path),
390                    "operation_id": self.operation.operation_id,
391                    "status": status_code
392                });
393                (status_code, response_body)
394            }
395        }
396    }
397
398    /// Generate a mock response with status code for this route (synchronous version)
399    ///
400    /// Note: This method does not support AI-assisted response generation.
401    /// Use `mock_response_with_status_async` for AI features.
402    pub fn mock_response_with_status(&self) -> (u16, serde_json::Value) {
403        self.mock_response_with_status_and_scenario(None)
404    }
405
406    /// Generate a mock response with status code and scenario selection
407    ///
408    /// # Arguments
409    /// * `scenario` - Optional scenario name to select from the OpenAPI examples
410    ///
411    /// # Example
412    ///
413    /// ```rust,ignore
414    /// // Select the "error" scenario from examples
415    /// let (status, body) = route.mock_response_with_status_and_scenario(Some("error"));
416    /// ```
417    pub fn mock_response_with_status_and_scenario(
418        &self,
419        scenario: Option<&str>,
420    ) -> (u16, serde_json::Value) {
421        self.mock_response_with_status_and_scenario_and_override(scenario, None)
422    }
423
424    /// Generate a mock response with status code, scenario, and optional status override
425    ///
426    /// # Arguments
427    /// * `scenario` - Optional scenario name to select from the OpenAPI examples
428    /// * `status_override` - Optional HTTP status code to use instead of the default
429    pub fn mock_response_with_status_and_scenario_and_override(
430        &self,
431        scenario: Option<&str>,
432        status_override: Option<u16>,
433    ) -> (u16, serde_json::Value) {
434        let (status, body, _) =
435            self.mock_response_with_status_and_scenario_and_trace(scenario, status_override);
436        (status, body)
437    }
438
439    /// Generate a mock response with status code, scenario selection, and trace collection
440    ///
441    /// Returns a tuple of (status_code, response_body, trace_data)
442    pub fn mock_response_with_status_and_scenario_and_trace(
443        &self,
444        scenario: Option<&str>,
445        status_override: Option<u16>,
446    ) -> (
447        u16,
448        serde_json::Value,
449        crate::reality_continuum::response_trace::ResponseGenerationTrace,
450    ) {
451        use crate::openapi::response_trace;
452        use crate::reality_continuum::response_trace::ResponseGenerationTrace;
453
454        // Use status override if the spec has a response for that code, otherwise default
455        let status_code = status_override
456            .filter(|code| self.has_response_for_status(*code))
457            .unwrap_or_else(|| self.find_first_available_status_code());
458
459        // Check if token expansion should be enabled
460        let expand_tokens = std::env::var("MOCKFORGE_RESPONSE_TEMPLATE_EXPAND")
461            .map(|v| v == "1" || v.eq_ignore_ascii_case("true"))
462            .unwrap_or(false);
463
464        // Use response selection mode for multiple examples
465        let mode = Some(self.response_selection_mode);
466        let selector = Some(self.response_selector.as_ref());
467
468        // Try to generate with trace collection
469        match response_trace::generate_response_with_trace(
470            &self.spec,
471            &self.operation,
472            status_code,
473            Some("application/json"),
474            expand_tokens,
475            scenario,
476            mode,
477            selector,
478            None, // No persona in basic route
479        ) {
480            Ok((response_body, trace)) => {
481                tracing::debug!(
482                    "ResponseGenerator succeeded for {} {} with status {} and scenario {:?}: {:?}",
483                    self.method,
484                    self.path,
485                    status_code,
486                    scenario,
487                    response_body
488                );
489                (status_code, response_body, trace)
490            }
491            Err(e) => {
492                tracing::debug!(
493                    "ResponseGenerator failed for {} {}: {}, using fallback",
494                    self.method,
495                    self.path,
496                    e
497                );
498                // Fallback to simple mock response if schema-based generation fails
499                let response_body = serde_json::json!({
500                    "message": format!("Mock response for {} {}", self.method, self.path),
501                    "operation_id": self.operation.operation_id,
502                    "status": status_code
503                });
504                // Create a minimal trace for fallback
505                let mut trace = ResponseGenerationTrace::new();
506                trace.set_final_payload(response_body.clone());
507                trace.add_metadata("fallback".to_string(), serde_json::json!(true));
508                trace.add_metadata("error".to_string(), serde_json::json!(e.to_string()));
509                (status_code, response_body, trace)
510            }
511        }
512    }
513
514    /// Check if the operation declares a response for the given HTTP status code
515    pub fn has_response_for_status(&self, code: u16) -> bool {
516        self.operation
517            .responses
518            .responses
519            .iter()
520            .any(|(status, _)| matches!(status, openapiv3::StatusCode::Code(c) if *c == code))
521    }
522
523    /// Find the first available status code from the OpenAPI operation responses
524    pub fn find_first_available_status_code(&self) -> u16 {
525        // Look for the first available status code in the responses
526        for (status, _) in &self.operation.responses.responses {
527            match status {
528                openapiv3::StatusCode::Code(code) => {
529                    return *code;
530                }
531                openapiv3::StatusCode::Range(range) => {
532                    // For ranges, use the appropriate status code
533                    match range {
534                        2 => return 200, // 2XX range
535                        3 => return 300, // 3XX range
536                        4 => return 400, // 4XX range
537                        5 => return 500, // 5XX range
538                        _ => continue,   // Skip unknown ranges
539                    }
540                }
541            }
542        }
543
544        // If no specific status codes found, check for default
545        if self.operation.responses.default.is_some() {
546            return 200; // Default to 200 for default responses
547        }
548
549        // Fallback to 200 if nothing else is available
550        200
551    }
552}
553
554/// OpenAPI operation wrapper with path context
555#[derive(Debug, Clone)]
556pub struct OpenApiOperation {
557    /// The HTTP method
558    pub method: String,
559    /// The path this operation belongs to
560    pub path: String,
561    /// The OpenAPI operation
562    pub operation: Operation,
563}
564
565impl OpenApiOperation {
566    /// Create a new OpenApiOperation
567    pub fn new(method: String, path: String, operation: Operation) -> Self {
568        Self {
569            method,
570            path,
571            operation,
572        }
573    }
574}
575
576/// Route generation utilities
577pub struct RouteGenerator;
578
579impl RouteGenerator {
580    /// Generate routes from an OpenAPI path item
581    pub fn generate_routes_from_path(
582        path: &str,
583        path_item: &ReferenceOr<PathItem>,
584        spec: &Arc<OpenApiSpec>,
585    ) -> Result<Vec<OpenApiRoute>> {
586        Self::generate_routes_from_path_with_persona(path, path_item, spec, None)
587    }
588
589    /// Generate routes from an OpenAPI path item with optional persona
590    pub fn generate_routes_from_path_with_persona(
591        path: &str,
592        path_item: &ReferenceOr<PathItem>,
593        spec: &Arc<OpenApiSpec>,
594        persona: Option<Arc<Persona>>,
595    ) -> Result<Vec<OpenApiRoute>> {
596        let mut routes = Vec::new();
597
598        if let Some(item) = path_item.as_item() {
599            // Generate route for each HTTP method
600            if let Some(op) = &item.get {
601                routes.push(OpenApiRoute::new_with_persona(
602                    "GET".to_string(),
603                    path.to_string(),
604                    op.clone(),
605                    spec.clone(),
606                    persona.clone(),
607                ));
608            }
609            if let Some(op) = &item.post {
610                routes.push(OpenApiRoute::new_with_persona(
611                    "POST".to_string(),
612                    path.to_string(),
613                    op.clone(),
614                    spec.clone(),
615                    persona.clone(),
616                ));
617            }
618            if let Some(op) = &item.put {
619                routes.push(OpenApiRoute::new_with_persona(
620                    "PUT".to_string(),
621                    path.to_string(),
622                    op.clone(),
623                    spec.clone(),
624                    persona.clone(),
625                ));
626            }
627            if let Some(op) = &item.delete {
628                routes.push(OpenApiRoute::new_with_persona(
629                    "DELETE".to_string(),
630                    path.to_string(),
631                    op.clone(),
632                    spec.clone(),
633                    persona.clone(),
634                ));
635            }
636            if let Some(op) = &item.patch {
637                routes.push(OpenApiRoute::new_with_persona(
638                    "PATCH".to_string(),
639                    path.to_string(),
640                    op.clone(),
641                    spec.clone(),
642                    persona.clone(),
643                ));
644            }
645            if let Some(op) = &item.head {
646                routes.push(OpenApiRoute::new_with_persona(
647                    "HEAD".to_string(),
648                    path.to_string(),
649                    op.clone(),
650                    spec.clone(),
651                    persona.clone(),
652                ));
653            }
654            if let Some(op) = &item.options {
655                routes.push(OpenApiRoute::new_with_persona(
656                    "OPTIONS".to_string(),
657                    path.to_string(),
658                    op.clone(),
659                    spec.clone(),
660                    persona.clone(),
661                ));
662            }
663            if let Some(op) = &item.trace {
664                routes.push(OpenApiRoute::new_with_persona(
665                    "TRACE".to_string(),
666                    path.to_string(),
667                    op.clone(),
668                    spec.clone(),
669                    persona.clone(),
670                ));
671            }
672        }
673
674        Ok(routes)
675    }
676}