agenterra 0.2.2

Generate production-ready MCP (Model Context Protocol) servers and clients from OpenAPI specs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
//! Comprehensive OpenAPI specification parser
//!
//! This module contains the complete OpenAPI parsing implementation ported from src/core/openapi.rs
//! It handles the full OpenAPI specification including:
//! - Reference resolution ($ref)
//! - Parameters with schema resolution  
//! - Request bodies and responses
//! - Components and schemas
//! - Security definitions
//! - Callbacks and vendor extensions

use serde_json::Value as JsonValue;

use crate::generation::{
    ApiInfo, Components, GenerationError, OpenApiContext, Operation, Parameter, ParameterLocation,
    RequestBody, Response, Schema, Server,
};

/// HTTP methods supported by OpenAPI (copied from core)
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum HttpMethod {
    Get,
    Post,
    Put,
    Delete,
    Patch,
    Head,
    Options,
}

impl HttpMethod {
    /// Get all HTTP methods as an array
    pub fn all() -> &'static [HttpMethod] {
        &[
            HttpMethod::Get,
            HttpMethod::Post,
            HttpMethod::Put,
            HttpMethod::Delete,
            HttpMethod::Patch,
            HttpMethod::Head,
            HttpMethod::Options,
        ]
    }
}

impl std::fmt::Display for HttpMethod {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            HttpMethod::Get => write!(f, "get"),
            HttpMethod::Post => write!(f, "post"),
            HttpMethod::Put => write!(f, "put"),
            HttpMethod::Delete => write!(f, "delete"),
            HttpMethod::Patch => write!(f, "patch"),
            HttpMethod::Head => write!(f, "head"),
            HttpMethod::Options => write!(f, "options"),
        }
    }
}

/// OpenAPI specification parser with comprehensive parsing capabilities
/// This is a complete port of the core::openapi::OpenApiContext implementation
pub struct OpenApiParser {
    /// The raw JSON value of the OpenAPI spec
    pub json: JsonValue,
}

impl OpenApiParser {
    /// Create a new parser from JSON content
    pub fn new(json: JsonValue) -> Self {
        Self { json }
    }

    /// Parse the complete OpenAPI specification to our domain model
    pub async fn parse(&self) -> Result<OpenApiContext, GenerationError> {
        // Extract version
        let version = self
            .json
            .get("openapi")
            .or_else(|| self.json.get("swagger"))
            .and_then(|v| v.as_str())
            .ok_or_else(|| GenerationError::ValidationError("Missing OpenAPI version".to_string()))?
            .to_string();

        // Extract info
        let info = ApiInfo {
            title: self
                .title()
                .ok_or_else(|| GenerationError::ValidationError("Missing info.title".to_string()))?
                .to_string(),
            version: self
                .version()
                .ok_or_else(|| {
                    GenerationError::ValidationError("Missing info.version".to_string())
                })?
                .to_string(),
            description: self
                .json
                .get("info")
                .and_then(|info| info.get("description"))
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
        };

        // Extract servers
        let servers = self
            .json
            .get("servers")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|s| {
                        Some(Server {
                            url: s.get("url").and_then(|v| v.as_str())?.to_string(),
                            description: s
                                .get("description")
                                .and_then(|v| v.as_str())
                                .map(|s| s.to_string()),
                        })
                    })
                    .collect()
            })
            .unwrap_or_default();

        // Parse operations using the comprehensive implementation
        let operations = self.parse_operations().await?;
        tracing::debug!("OpenAPI parser found {} operations", operations.len());

        // Extract components if present
        let components = self
            .json
            .get("components")
            .and_then(|comp| comp.as_object())
            .and_then(|comp_obj| comp_obj.get("schemas"))
            .map(|schemas| Components {
                schemas: schemas.clone(),
            });

        Ok(OpenApiContext {
            version,
            info,
            servers,
            operations,
            components,
        })
    }

    /// Get the title of the API
    pub fn title(&self) -> Option<&str> {
        self.json.get("info")?.get("title")?.as_str()
    }

    /// Get the version of the API
    pub fn version(&self) -> Option<&str> {
        self.json.get("info")?.get("version")?.as_str()
    }

    /// Parse all endpoints into structured contexts for template rendering
    /// This is a complete port from core::openapi::OpenApiContext::parse_operations
    pub async fn parse_operations(&self) -> Result<Vec<Operation>, GenerationError> {
        // Get paths object
        let paths = self
            .json
            .get("paths")
            .and_then(JsonValue::as_object)
            .ok_or_else(|| {
                GenerationError::ValidationError("Missing 'paths' object".to_string())
            })?;

        // Use iterator combinators to flatten and map operations
        let operations = paths
            .iter()
            .flat_map(|(path, path_item)| {
                HttpMethod::all()
                    .iter()
                    .filter_map(|method| {
                        path_item
                            .get(method.to_string())
                            .and_then(JsonValue::as_object)
                            .map(|method_item| (path, method, path_item, method_item))
                    })
                    .collect::<Vec<_>>()
            })
            .map(|(path, method, path_item, method_item)| {
                self.build_operation(path, method, path_item, method_item)
            })
            .collect::<Result<Vec<_>, _>>()?;

        Ok(operations)
    }

    /// Build an Operation from path, method, and method item
    fn build_operation(
        &self,
        path: &str,
        method: &HttpMethod,
        path_item: &JsonValue,
        method_item: &serde_json::Map<String, JsonValue>,
    ) -> Result<Operation, GenerationError> {
        let operation_id = method_item
            .get("operationId")
            .and_then(JsonValue::as_str)
            .map(String::from)
            .unwrap_or_else(|| {
                format!(
                    "{}_{}",
                    method,
                    path.trim_start_matches('/').replace('/', "_")
                )
            });

        let summary = method_item
            .get("summary")
            .and_then(JsonValue::as_str)
            .map(String::from);
        let description = method_item
            .get("description")
            .and_then(JsonValue::as_str)
            .map(String::from);
        let external_docs = method_item.get("externalDocs").cloned();

        // Extract typed parameters - merge path-level and method-level parameters
        let mut parameters = self.extract_parameters(path_item).unwrap_or_default();
        let method_params = self
            .extract_parameters(&JsonValue::Object(method_item.clone()))
            .unwrap_or_default();
        parameters.extend(method_params);

        // Extract typed request body
        let request_body = method_item
            .get("requestBody")
            .map(|rb| self.parse_request_body(rb))
            .transpose()?;

        // Extract typed responses
        let responses = self.extract_responses(method_item)?;

        let callbacks = method_item.get("callbacks").cloned();
        let deprecated = method_item.get("deprecated").and_then(JsonValue::as_bool);
        let security = method_item
            .get("security")
            .and_then(JsonValue::as_array)
            .cloned();
        let servers = method_item
            .get("servers")
            .and_then(JsonValue::as_array)
            .cloned();
        let tags = method_item
            .get("tags")
            .and_then(JsonValue::as_array)
            .map(|arr| {
                arr.iter()
                    .filter_map(JsonValue::as_str)
                    .map(String::from)
                    .collect()
            });
        let vendor_extensions = self.extract_vendor_extensions(method_item);

        Ok(Operation {
            id: operation_id,
            path: path.to_string(),
            method: method.to_string(),
            summary,
            description,
            external_docs,
            tags,
            parameters,
            request_body,
            responses,
            callbacks,
            deprecated,
            security,
            servers,
            vendor_extensions,
        })
    }

    /// Extracts parameters from an OpenAPI path item, resolving any $ref references
    /// This is a complete port from core::openapi::OpenApiContext::extract_parameters
    fn extract_parameters(&self, path_item: &JsonValue) -> Option<Vec<Parameter>> {
        path_item
            .get("parameters")
            .and_then(JsonValue::as_array)
            .map(|arr| {
                arr.iter()
                    .filter_map(|param| {
                        if let Some(ref_str) = param.get("$ref").and_then(JsonValue::as_str) {
                            self.json
                                .pointer(&ref_str[1..])
                                .and_then(|p| self.parse_parameter(p).ok())
                        } else {
                            self.parse_parameter(param).ok()
                        }
                    })
                    .collect::<Vec<Parameter>>()
            })
    }

    /// Parse a single parameter
    fn parse_parameter(&self, param: &JsonValue) -> Result<Parameter, GenerationError> {
        let name = param["name"]
            .as_str()
            .ok_or_else(|| GenerationError::ValidationError("Parameter missing name".to_string()))?
            .to_string();

        let location = match param["in"].as_str() {
            Some("path") => ParameterLocation::Path,
            Some("query") => ParameterLocation::Query,
            Some("header") => ParameterLocation::Header,
            Some("cookie") => ParameterLocation::Cookie,
            _ => {
                return Err(GenerationError::ValidationError(
                    "Invalid parameter location".to_string(),
                ));
            }
        };

        let required = param
            .get("required")
            .and_then(|v| v.as_bool())
            .unwrap_or(false);
        let schema = self.parse_schema(param.get("schema").unwrap_or(&serde_json::json!({})))?;
        let description = param
            .get("description")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        Ok(Parameter {
            name,
            location,
            required,
            schema,
            description,
        })
    }

    /// Extracts response definitions from an OpenAPI operation
    /// This is a complete port from core::openapi::OpenApiContext::extract_responses
    fn extract_responses(
        &self,
        method_item: &serde_json::Map<String, JsonValue>,
    ) -> Result<Vec<Response>, GenerationError> {
        let responses = method_item
            .get("responses")
            .and_then(JsonValue::as_object)
            .map(|map| {
                map.iter()
                    .map(|(k, v)| self.parse_response(k, v))
                    .collect::<Result<Vec<_>, _>>()
            })
            .unwrap_or_else(|| Ok(Vec::new()))?;

        Ok(responses)
    }

    /// Parse a single response
    fn parse_response(
        &self,
        status_code: &str,
        response: &JsonValue,
    ) -> Result<Response, GenerationError> {
        // Check if this is a $ref
        let resolved_response = if let Some(ref_str) = response.get("$ref").and_then(|v| v.as_str())
        {
            self.resolve_ref(ref_str)?
        } else {
            response.clone()
        };

        // Process content to resolve any $ref in schemas
        let content = if let Some(content_value) = resolved_response.get("content") {
            if let Some(content_obj) = content_value.as_object() {
                let mut resolved_content = serde_json::Map::new();
                for (media_type, media_value) in content_obj {
                    if let Some(media_obj) = media_value.as_object() {
                        let mut resolved_media = media_obj.clone();
                        // Check if there's a schema to resolve
                        if let Some(schema) = media_obj.get("schema") {
                            let resolved_schema = self.resolve_schema_refs(schema)?;
                            resolved_media.insert("schema".to_string(), resolved_schema);
                        }
                        resolved_content
                            .insert(media_type.clone(), JsonValue::Object(resolved_media));
                    } else {
                        resolved_content.insert(media_type.clone(), media_value.clone());
                    }
                }
                Some(JsonValue::Object(resolved_content))
            } else {
                Some(content_value.clone())
            }
        } else {
            None
        };

        Ok(Response {
            status_code: status_code.to_string(),
            description: resolved_response
                .get("description")
                .and_then(|v| v.as_str())
                .unwrap_or("No description")
                .to_string(),
            content,
        })
    }

    /// Parse a request body
    fn parse_request_body(&self, body: &JsonValue) -> Result<RequestBody, GenerationError> {
        // Check if this is a $ref
        let resolved_body = if let Some(ref_str) = body.get("$ref").and_then(|v| v.as_str()) {
            self.resolve_ref(ref_str)?
        } else {
            body.clone()
        };

        // Process content to resolve any $ref in schemas
        let content = if let Some(content_value) = resolved_body.get("content") {
            self.resolve_schema_refs(content_value)?
        } else {
            JsonValue::Null
        };

        Ok(RequestBody {
            required: resolved_body
                .get("required")
                .and_then(|v| v.as_bool())
                .unwrap_or(false),
            content,
            description: resolved_body
                .get("description")
                .and_then(|v| v.as_str())
                .map(|s| s.to_string()),
        })
    }

    /// Parse a schema object
    #[allow(clippy::only_used_in_recursion)]
    fn parse_schema(&self, schema: &JsonValue) -> Result<Schema, GenerationError> {
        // First check if this is a $ref
        if let Some(ref_str) = schema.get("$ref").and_then(|v| v.as_str()) {
            // Resolve the reference
            let resolved_schema = self.resolve_ref(ref_str)?;
            // Parse the resolved schema
            return self.parse_schema(&resolved_schema);
        }

        let schema_type = schema
            .get("type")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());
        let format = schema
            .get("format")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());

        let items = if let Some(items_value) = schema.get("items") {
            Some(Box::new(self.parse_schema(items_value)?))
        } else {
            None
        };

        // Parse properties recursively to resolve any nested schemas
        let properties = if let Some(props) = schema.get("properties") {
            if let Some(props_obj) = props.as_object() {
                let mut parsed_props = indexmap::IndexMap::new();
                for (key, value) in props_obj {
                    let parsed_schema = self.parse_schema(value)?;
                    parsed_props.insert(key.clone(), parsed_schema);
                }
                Some(parsed_props)
            } else {
                None
            }
        } else {
            None
        };
        let required = schema
            .get("required")
            .and_then(|v| v.as_array())
            .map(|arr| {
                arr.iter()
                    .filter_map(|v| v.as_str())
                    .map(|s| s.to_string())
                    .collect()
            });

        // Extract all additional schema fields
        let description = schema
            .get("description")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());
        let title = schema
            .get("title")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());
        let default = schema.get("default").cloned();
        let example = schema.get("example").cloned();
        let enum_values = schema.get("enum").and_then(|v| v.as_array()).cloned();
        let minimum = schema.get("minimum").and_then(|v| v.as_f64());
        let maximum = schema.get("maximum").and_then(|v| v.as_f64());
        let min_length = schema
            .get("minLength")
            .and_then(|v| v.as_u64())
            .map(|v| v as usize);
        let max_length = schema
            .get("maxLength")
            .and_then(|v| v.as_u64())
            .map(|v| v as usize);
        let pattern = schema
            .get("pattern")
            .and_then(|v| v.as_str())
            .map(|s| s.to_string());
        let min_items = schema
            .get("minItems")
            .and_then(|v| v.as_u64())
            .map(|v| v as usize);
        let max_items = schema
            .get("maxItems")
            .and_then(|v| v.as_u64())
            .map(|v| v as usize);
        let unique_items = schema.get("uniqueItems").and_then(|v| v.as_bool());
        let read_only = schema.get("readOnly").and_then(|v| v.as_bool());
        let write_only = schema.get("writeOnly").and_then(|v| v.as_bool());
        let nullable = schema.get("nullable").and_then(|v| v.as_bool());
        let deprecated = schema.get("deprecated").and_then(|v| v.as_bool());
        let xml = schema.get("xml").cloned();

        // Parse additionalProperties
        let additional_properties = if let Some(add_props) = schema.get("additionalProperties") {
            if let Some(bool_val) = add_props.as_bool() {
                Some(Box::new(
                    crate::infrastructure::openapi::AdditionalProperties::Boolean(bool_val),
                ))
            } else {
                let schema = self.parse_schema(add_props)?;
                Some(Box::new(
                    crate::infrastructure::openapi::AdditionalProperties::Schema(Box::new(schema)),
                ))
            }
        } else {
            None
        };

        // Parse composition schemas
        let all_of = if let Some(all_of_arr) = schema.get("allOf").and_then(|v| v.as_array()) {
            let mut schemas = Vec::new();
            for schema_val in all_of_arr {
                schemas.push(self.parse_schema(schema_val)?);
            }
            Some(schemas)
        } else {
            None
        };

        let one_of = if let Some(one_of_arr) = schema.get("oneOf").and_then(|v| v.as_array()) {
            let mut schemas = Vec::new();
            for schema_val in one_of_arr {
                schemas.push(self.parse_schema(schema_val)?);
            }
            Some(schemas)
        } else {
            None
        };

        let any_of = if let Some(any_of_arr) = schema.get("anyOf").and_then(|v| v.as_array()) {
            let mut schemas = Vec::new();
            for schema_val in any_of_arr {
                schemas.push(self.parse_schema(schema_val)?);
            }
            Some(schemas)
        } else {
            None
        };

        let not = if let Some(not_schema) = schema.get("not") {
            Some(Box::new(self.parse_schema(not_schema)?))
        } else {
            None
        };

        // Parse discriminator
        let discriminator =
            if let Some(disc) = schema.get("discriminator").and_then(|v| v.as_object()) {
                let property_name = disc
                    .get("propertyName")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string())
                    .ok_or_else(|| {
                        GenerationError::ValidationError(
                            "Discriminator missing propertyName".to_string(),
                        )
                    })?;
                let mapping = disc.get("mapping").and_then(|v| v.as_object()).map(|m| {
                    m.iter()
                        .map(|(k, v)| (k.clone(), v.as_str().unwrap_or("").to_string()))
                        .collect()
                });
                Some(crate::infrastructure::openapi::Discriminator {
                    property_name,
                    mapping,
                })
            } else {
                None
            };

        // Parse external docs
        let external_docs =
            if let Some(ext_docs) = schema.get("externalDocs").and_then(|v| v.as_object()) {
                let url = ext_docs
                    .get("url")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string())
                    .ok_or_else(|| {
                        GenerationError::ValidationError("ExternalDocs missing url".to_string())
                    })?;
                let description = ext_docs
                    .get("description")
                    .and_then(|v| v.as_str())
                    .map(|s| s.to_string());
                Some(crate::infrastructure::openapi::ExternalDocs { url, description })
            } else {
                None
            };

        Ok(Schema {
            schema_type,
            format,
            items,
            properties,
            required,
            description,
            title,
            default,
            example,
            enum_values,
            minimum,
            maximum,
            min_length,
            max_length,
            pattern,
            min_items,
            max_items,
            unique_items,
            additional_properties,
            all_of,
            one_of,
            any_of,
            not,
            discriminator,
            read_only,
            write_only,
            xml,
            external_docs,
            deprecated,
            nullable,
        })
    }

    /// Recursively resolve all $ref in a JSON value
    fn resolve_schema_refs(&self, value: &JsonValue) -> Result<JsonValue, GenerationError> {
        match value {
            JsonValue::Object(obj) => {
                // Check if this object has a $ref
                if let Some(ref_str) = obj.get("$ref").and_then(|v| v.as_str()) {
                    // Resolve the reference and recursively resolve any nested refs
                    let resolved = self.resolve_ref(ref_str)?;
                    return self.resolve_schema_refs(&resolved);
                }

                // Otherwise, recursively process all fields
                let mut resolved_obj = serde_json::Map::new();
                for (key, val) in obj {
                    resolved_obj.insert(key.clone(), self.resolve_schema_refs(val)?);
                }
                Ok(JsonValue::Object(resolved_obj))
            }
            JsonValue::Array(arr) => {
                // Recursively process array elements
                let resolved_arr: Result<Vec<_>, _> = arr
                    .iter()
                    .map(|elem| self.resolve_schema_refs(elem))
                    .collect();
                Ok(JsonValue::Array(resolved_arr?))
            }
            // Primitive values are returned as-is
            _ => Ok(value.clone()),
        }
    }

    /// Resolve a $ref reference
    fn resolve_ref(&self, ref_str: &str) -> Result<JsonValue, GenerationError> {
        // Handle JSON pointer references (e.g., "#/components/schemas/Pet")
        if let Some(pointer) = ref_str.strip_prefix('#') {
            self.json.pointer(pointer).cloned().ok_or_else(|| {
                GenerationError::ValidationError(format!("Unable to resolve reference: {ref_str}"))
            })
        } else {
            // External references not supported yet
            Err(GenerationError::ValidationError(format!(
                "External references not supported: {ref_str}"
            )))
        }
    }

    /// Extracts vendor extensions (x-* prefixed properties) from an OpenAPI operation
    fn extract_vendor_extensions(
        &self,
        method_item: &serde_json::Map<String, JsonValue>,
    ) -> std::collections::HashMap<String, JsonValue> {
        method_item
            .iter()
            .filter(|(k, _)| k.starts_with("x-"))
            .map(|(k, v)| (k.clone(), v.clone()))
            .collect()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    #[tokio::test]
    async fn test_ref_resolution() {
        // Create a simple spec with $ref
        let spec_json = json!({
            "openapi": "3.0.0",
            "info": {
                "title": "Test API",
                "version": "1.0.0"
            },
            "paths": {
                "/pets/{petId}": {
                    "get": {
                        "operationId": "getPet",
                        "parameters": [{
                            "name": "petId",
                            "in": "path",
                            "required": true,
                            "schema": { "$ref": "#/components/schemas/PetId" }
                        }],
                        "requestBody": {
                            "$ref": "#/components/requestBodies/PetRequest"
                        },
                        "responses": {
                            "200": {
                                "$ref": "#/components/responses/PetResponse"
                            }
                        }
                    }
                }
            },
            "components": {
                "schemas": {
                    "PetId": {
                        "type": "integer",
                        "format": "int64"
                    },
                    "Pet": {
                        "type": "object",
                        "properties": {
                            "id": { "$ref": "#/components/schemas/PetId" },
                            "name": { "type": "string" }
                        }
                    }
                },
                "requestBodies": {
                    "PetRequest": {
                        "required": true,
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/Pet" }
                            }
                        }
                    }
                },
                "responses": {
                    "PetResponse": {
                        "description": "A pet",
                        "content": {
                            "application/json": {
                                "schema": { "$ref": "#/components/schemas/Pet" }
                            }
                        }
                    }
                }
            }
        });

        let parser = OpenApiParser::new(spec_json);
        let spec = parser.parse().await.unwrap();

        // Check that we have one operation
        assert_eq!(spec.operations.len(), 1);
        let operation = &spec.operations[0];

        // Check parameter schema was resolved
        assert_eq!(operation.parameters.len(), 1);
        let param = &operation.parameters[0];
        assert_eq!(param.schema.schema_type, Some("integer".to_string()));
        assert_eq!(param.schema.format, Some("int64".to_string()));

        // Check request body was resolved
        assert!(operation.request_body.is_some());
        let request_body = operation.request_body.as_ref().unwrap();
        assert!(request_body.required);

        // Check response was resolved
        assert_eq!(operation.responses.len(), 1);
        let response = &operation.responses[0];
        assert_eq!(response.description, "A pet");
        assert!(response.content.is_some());

        // Check that nested $ref in Pet schema was resolved
        let response_content = response.content.as_ref().unwrap();
        let json_content = response_content.get("application/json").unwrap();
        let schema_value = json_content.get("schema").unwrap();

        // Debug print to see what we have
        println!(
            "Response schema: {}",
            serde_json::to_string_pretty(schema_value).unwrap()
        );

        // The schema should be fully resolved with no $ref
        assert!(
            schema_value.get("$ref").is_none(),
            "Expected $ref to be resolved"
        );

        // Check that we have the Pet object properties
        assert_eq!(schema_value.get("type"), Some(&json!("object")));

        let props = schema_value.get("properties").expect("Expected properties");
        assert!(props.is_object(), "Properties should be an object");

        // Check the id property (which was a $ref to PetId)
        let id_prop = props.get("id").expect("Expected id property");
        assert_eq!(id_prop.get("type"), Some(&json!("integer")));
        assert_eq!(id_prop.get("format"), Some(&json!("int64")));

        // Check the name property
        let name_prop = props.get("name").expect("Expected name property");
        assert_eq!(name_prop.get("type"), Some(&json!("string")));
    }

    #[tokio::test]
    async fn test_petstore_parsing_parity() {
        // Load the petstore spec
        let petstore_json =
            include_str!("../../../tests/fixtures/openapi/petstore.openapi.v3.json");
        let spec_json: JsonValue = serde_json::from_str(petstore_json).unwrap();

        let parser = OpenApiParser::new(spec_json);
        let spec = parser.parse().await.unwrap();

        // Check basic metadata
        assert_eq!(spec.info.title, "Swagger Petstore - OpenAPI 3.0");
        assert_eq!(spec.version, "3.0.4");

        // Find an operation with comprehensive fields (getPetById has security)
        let get_pet_by_id = spec
            .operations
            .iter()
            .find(|op| op.id == "getPetById")
            .expect("getPetById operation not found");

        // Verify all fields are populated
        assert_eq!(get_pet_by_id.path, "/pet/{petId}");
        assert_eq!(get_pet_by_id.method, "get");
        assert_eq!(get_pet_by_id.summary, Some("Find pet by ID.".to_string()));
        assert_eq!(
            get_pet_by_id.description,
            Some("Returns a single pet.".to_string())
        );
        assert_eq!(get_pet_by_id.tags, Some(vec!["pet".to_string()]));

        // Check parameters
        assert_eq!(get_pet_by_id.parameters.len(), 1);
        assert_eq!(get_pet_by_id.parameters[0].name, "petId");
        assert_eq!(
            get_pet_by_id.parameters[0].location,
            ParameterLocation::Path
        );
        assert!(get_pet_by_id.parameters[0].required);

        // Check responses
        assert!(!get_pet_by_id.responses.is_empty());
        let success_response = get_pet_by_id
            .responses
            .iter()
            .find(|r| r.status_code == "200")
            .expect("200 response not found");
        assert_eq!(success_response.description, "successful operation");

        // Check security - this operation has both api_key and petstore_auth
        assert!(get_pet_by_id.security.is_some());
        let security = get_pet_by_id.security.as_ref().unwrap();
        assert_eq!(security.len(), 2);

        // Check an operation with request body (updatePet)
        let update_pet = spec
            .operations
            .iter()
            .find(|op| op.id == "updatePet")
            .expect("updatePet operation not found");

        assert!(update_pet.request_body.is_some());
        let request_body = update_pet.request_body.as_ref().unwrap();
        assert!(request_body.required);
        assert!(request_body.description.is_some());

        // Check an operation with multiple parameters (updatePetWithForm)
        let update_form = spec
            .operations
            .iter()
            .find(|op| op.id == "updatePetWithForm")
            .expect("updatePetWithForm operation not found");

        assert_eq!(update_form.parameters.len(), 3); // petId (path), name (query), status (query)
        let path_param = update_form
            .parameters
            .iter()
            .find(|p| p.location == ParameterLocation::Path)
            .expect("Path parameter not found");
        assert_eq!(path_param.name, "petId");
        assert!(path_param.required);

        let query_params: Vec<_> = update_form
            .parameters
            .iter()
            .filter(|p| p.location == ParameterLocation::Query)
            .collect();
        assert_eq!(query_params.len(), 2);
        assert!(query_params.iter().any(|p| p.name == "name"));
        assert!(query_params.iter().any(|p| p.name == "status"));

        // Check an operation with no parameters (getInventory)
        let get_inventory = spec
            .operations
            .iter()
            .find(|op| op.id == "getInventory")
            .expect("getInventory operation not found");
        assert_eq!(get_inventory.parameters.len(), 0);

        // Check operation with array parameter (findPetsByTags)
        let find_by_tags = spec
            .operations
            .iter()
            .find(|op| op.id == "findPetsByTags")
            .expect("findPetsByTags operation not found");
        assert_eq!(find_by_tags.parameters.len(), 1);
        let tags_param = &find_by_tags.parameters[0];
        assert_eq!(tags_param.name, "tags");
        assert_eq!(tags_param.location, ParameterLocation::Query);
        assert!(!tags_param.required); // This one is optional

        // Check operation with enum parameter (findPetsByStatus)
        let find_by_status = spec
            .operations
            .iter()
            .find(|op| op.id == "findPetsByStatus")
            .expect("findPetsByStatus operation not found");
        assert_eq!(find_by_status.parameters.len(), 1);
        let status_param = &find_by_status.parameters[0];
        assert_eq!(status_param.name, "status");
        // Check that schema contains enum values
        if let Some(schema_type) = &status_param.schema.schema_type {
            assert_eq!(schema_type, "string");
        }

        // Check operation with header parameter (deletePet)
        let delete_pet = spec
            .operations
            .iter()
            .find(|op| op.id == "deletePet")
            .expect("deletePet operation not found");
        assert_eq!(delete_pet.parameters.len(), 2);
        let header_param = delete_pet
            .parameters
            .iter()
            .find(|p| p.location == ParameterLocation::Header)
            .expect("Header parameter not found");
        assert_eq!(header_param.name, "api_key");
        assert!(!header_param.required);

        // Check operation without security (placeOrder)
        let place_order = spec
            .operations
            .iter()
            .find(|op| op.id == "placeOrder")
            .expect("placeOrder operation not found");
        assert!(place_order.security.is_none());

        // Check operation with empty parameters array (logoutUser)
        let logout_user = spec
            .operations
            .iter()
            .find(|op| op.id == "logoutUser")
            .expect("logoutUser operation not found");
        assert_eq!(logout_user.parameters.len(), 0);

        // Verify that all methods are parsed correctly
        let put_ops: Vec<_> = spec
            .operations
            .iter()
            .filter(|op| op.method == "put")
            .collect();
        assert!(put_ops.len() >= 2); // updatePet, updateUser

        let post_ops: Vec<_> = spec
            .operations
            .iter()
            .filter(|op| op.method == "post")
            .collect();
        assert!(post_ops.len() >= 5); // Multiple POST operations

        let delete_ops: Vec<_> = spec
            .operations
            .iter()
            .filter(|op| op.method == "delete")
            .collect();
        assert!(delete_ops.len() >= 3); // deletePet, deleteOrder, deleteUser

        // Verify no vendor extensions on standard operations
        assert_eq!(get_pet_by_id.vendor_extensions.len(), 0);

        // Verify deprecated flag is not set on non-deprecated operations
        assert_eq!(get_pet_by_id.deprecated, None);

        // Count total operations to ensure we're parsing all of them
        assert_eq!(spec.operations.len(), 19); // Petstore has exactly 19 operations
    }
}