elif-openapi 0.2.1

OpenAPI 3.0 specification generation for elif.rs framework
Documentation
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
use crate::{
    error::{OpenApiError, OpenApiResult},
    specification::Schema,
};
use serde_json::Value;
use std::collections::HashMap;

/// Schema generator for converting Rust types to OpenAPI schemas
pub struct SchemaGenerator {
    /// Generated schemas cache
    schemas: HashMap<String, Schema>,
    /// Configuration options
    config: SchemaConfig,
}

/// Configuration for schema generation
#[derive(Debug, Clone)]
pub struct SchemaConfig {
    /// Generate nullable schemas for Option<T>
    pub nullable_optional: bool,
    /// Include example values
    pub include_examples: bool,
    /// Custom type mappings
    pub custom_mappings: HashMap<String, Schema>,
}

/// Type information for schema generation
#[derive(Debug, Clone)]
pub struct TypeSchema {
    /// Type name
    pub name: String,
    /// Generated schema
    pub schema: Schema,
    /// Dependencies (other types this type references)
    pub dependencies: Vec<String>,
}

impl SchemaGenerator {
    /// Create a new schema generator
    pub fn new(config: SchemaConfig) -> Self {
        Self {
            schemas: HashMap::new(),
            config,
        }
    }

    /// Generate schema for a Rust type
    pub fn generate_schema(&mut self, type_name: &str) -> OpenApiResult<Schema> {
        // Check cache first
        if let Some(schema) = self.schemas.get(type_name) {
            return Ok(schema.clone());
        }

        // Check custom mappings
        if let Some(schema) = self.config.custom_mappings.get(type_name) {
            self.schemas.insert(type_name.to_string(), schema.clone());
            return Ok(schema.clone());
        }

        // Generate schema based on type
        let schema = self.generate_schema_for_type(type_name)?;
        self.schemas.insert(type_name.to_string(), schema.clone());
        Ok(schema)
    }

    /// Generate schema for primitive types, collections, and custom types
    fn generate_schema_for_type(&self, type_name: &str) -> OpenApiResult<Schema> {
        match type_name {
            // String types
            "String" | "str" | "&str" => Ok(Schema {
                schema_type: Some("string".to_string()),
                ..Default::default()
            }),

            // Numeric types
            "i8" | "i16" | "i32" => Ok(Schema {
                schema_type: Some("integer".to_string()),
                format: Some("int32".to_string()),
                ..Default::default()
            }),
            "i64" => Ok(Schema {
                schema_type: Some("integer".to_string()),
                format: Some("int64".to_string()),
                ..Default::default()
            }),
            "u8" | "u16" | "u32" => Ok(Schema {
                schema_type: Some("integer".to_string()),
                format: Some("int32".to_string()),
                minimum: Some(0.0),
                ..Default::default()
            }),
            "u64" => Ok(Schema {
                schema_type: Some("integer".to_string()),
                format: Some("int64".to_string()),
                minimum: Some(0.0),
                ..Default::default()
            }),
            "f32" => Ok(Schema {
                schema_type: Some("number".to_string()),
                format: Some("float".to_string()),
                ..Default::default()
            }),
            "f64" => Ok(Schema {
                schema_type: Some("number".to_string()),
                format: Some("double".to_string()),
                ..Default::default()
            }),

            // Boolean type
            "bool" => Ok(Schema {
                schema_type: Some("boolean".to_string()),
                ..Default::default()
            }),

            // UUID type
            "Uuid" => Ok(Schema {
                schema_type: Some("string".to_string()),
                format: Some("uuid".to_string()),
                ..Default::default()
            }),

            // DateTime types
            "DateTime" | "DateTime<Utc>" => Ok(Schema {
                schema_type: Some("string".to_string()),
                format: Some("date-time".to_string()),
                ..Default::default()
            }),
            "NaiveDate" => Ok(Schema {
                schema_type: Some("string".to_string()),
                format: Some("date".to_string()),
                ..Default::default()
            }),

            // Handle generic types
            type_name if type_name.starts_with("Option<") => {
                let inner_type = self.extract_generic_type(type_name, "Option")?;
                let mut schema = self.generate_schema_for_type(&inner_type)?;
                if self.config.nullable_optional {
                    schema.nullable = Some(true);
                }
                Ok(schema)
            }
            type_name if type_name.starts_with("Vec<") => {
                let inner_type = self.extract_generic_type(type_name, "Vec")?;
                let items_schema = self.generate_schema_for_type(&inner_type)?;
                Ok(Schema {
                    schema_type: Some("array".to_string()),
                    items: Some(Box::new(items_schema)),
                    ..Default::default()
                })
            }
            type_name if type_name.starts_with("HashMap<") => {
                // For simplicity, assume HashMap<String, V>
                let value_type = self.extract_hashmap_value_type(type_name)?;
                let value_schema = self.generate_schema_for_type(&value_type)?;
                Ok(Schema {
                    schema_type: Some("object".to_string()),
                    additional_properties: Some(Box::new(value_schema)),
                    ..Default::default()
                })
            }

            // Custom types - create reference
            _ => Ok(Schema {
                reference: Some(format!("#/components/schemas/{}", type_name)),
                ..Default::default()
            }),
        }
    }

    /// Extract generic type parameter (e.g., "T" from "Option<T>")
    fn extract_generic_type(&self, type_name: &str, wrapper: &str) -> OpenApiResult<String> {
        let start = wrapper.len() + 1; // +1 for '<'
        let end = type_name.len() - 1; // -1 for '>'

        if start >= end {
            return Err(OpenApiError::schema_error(format!(
                "Invalid generic type: {}",
                type_name
            )));
        }

        Ok(type_name[start..end].to_string())
    }

    /// Extract value type from HashMap<K, V>
    fn extract_hashmap_value_type(&self, type_name: &str) -> OpenApiResult<String> {
        // Simple implementation - assumes HashMap<String, ValueType>
        let inner = type_name
            .strip_prefix("HashMap<")
            .and_then(|s| s.strip_suffix(">"))
            .ok_or_else(|| {
                OpenApiError::schema_error(format!("Invalid HashMap type: {}", type_name))
            })?;

        let parts: Vec<&str> = inner.split(',').collect();
        if parts.len() != 2 {
            return Err(OpenApiError::schema_error(format!(
                "Invalid HashMap type: {}",
                type_name
            )));
        }

        Ok(parts[1].trim().to_string())
    }

    /// Generate schema for a struct with fields
    pub fn generate_struct_schema(
        &mut self,
        struct_name: &str,
        fields: &[(String, String, Option<String>)], // (name, type, description)
    ) -> OpenApiResult<Schema> {
        let mut properties = HashMap::new();
        let mut required = Vec::new();
        let mut dependencies = Vec::new();

        for (field_name, field_type, description) in fields {
            let mut field_schema = self.generate_schema(field_type)?;

            if let Some(desc) = description {
                field_schema.description = Some(desc.clone());
            }

            // Check if field is optional
            if !field_type.starts_with("Option<") {
                required.push(field_name.clone());
            }

            properties.insert(field_name.clone(), field_schema);

            // Track dependencies
            if !self.is_primitive_type(field_type) {
                dependencies.push(field_type.clone());
            }
        }

        let schema = Schema {
            schema_type: Some("object".to_string()),
            properties,
            required,
            ..Default::default()
        };

        self.schemas.insert(struct_name.to_string(), schema.clone());
        Ok(schema)
    }

    /// Generate schema for an enum
    pub fn generate_enum_schema(
        &mut self,
        enum_name: &str,
        variants: &[String],
    ) -> OpenApiResult<Schema> {
        let enum_values: Vec<Value> = variants.iter().map(|v| Value::String(v.clone())).collect();

        let schema = Schema {
            schema_type: Some("string".to_string()),
            enum_values,
            ..Default::default()
        };

        self.schemas.insert(enum_name.to_string(), schema.clone());
        Ok(schema)
    }

    /// Check if a type is primitive
    fn is_primitive_type(&self, type_name: &str) -> bool {
        matches!(
            type_name,
            "String"
                | "str"
                | "&str"
                | "i8"
                | "i16"
                | "i32"
                | "i64"
                | "u8"
                | "u16"
                | "u32"
                | "u64"
                | "f32"
                | "f64"
                | "bool"
                | "Uuid"
                | "DateTime"
                | "DateTime<Utc>"
                | "NaiveDate"
        ) || type_name.starts_with("Option<")
            || type_name.starts_with("Vec<")
            || type_name.starts_with("HashMap<")
    }

    /// Get all generated schemas
    pub fn get_schemas(&self) -> &HashMap<String, Schema> {
        &self.schemas
    }

    /// Clear schema cache
    pub fn clear_cache(&mut self) {
        self.schemas.clear();
    }
}

impl Default for SchemaConfig {
    fn default() -> Self {
        Self {
            nullable_optional: true,
            include_examples: true,
            custom_mappings: HashMap::new(),
        }
    }
}

impl SchemaConfig {
    /// Create new configuration
    pub fn new() -> Self {
        Self::default()
    }

    /// Set nullable option handling
    pub fn with_nullable_optional(mut self, nullable: bool) -> Self {
        self.nullable_optional = nullable;
        self
    }

    /// Set example inclusion
    pub fn with_examples(mut self, include: bool) -> Self {
        self.include_examples = include;
        self
    }

    /// Add custom type mapping
    pub fn with_custom_mapping(mut self, type_name: &str, schema: Schema) -> Self {
        self.custom_mappings.insert(type_name.to_string(), schema);
        self
    }
}

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

    #[test]
    fn test_primitive_schema_generation() {
        let mut generator = SchemaGenerator::new(SchemaConfig::default());

        let string_schema = generator.generate_schema("String").unwrap();
        assert_eq!(string_schema.schema_type, Some("string".to_string()));

        let int_schema = generator.generate_schema("i32").unwrap();
        assert_eq!(int_schema.schema_type, Some("integer".to_string()));
        assert_eq!(int_schema.format, Some("int32".to_string()));

        let bool_schema = generator.generate_schema("bool").unwrap();
        assert_eq!(bool_schema.schema_type, Some("boolean".to_string()));
    }

    #[test]
    fn test_optional_schema_generation() {
        let mut generator = SchemaGenerator::new(SchemaConfig::default());

        let optional_string_schema = generator.generate_schema("Option<String>").unwrap();
        assert_eq!(
            optional_string_schema.schema_type,
            Some("string".to_string())
        );
        assert_eq!(optional_string_schema.nullable, Some(true));
    }

    #[test]
    fn test_array_schema_generation() {
        let mut generator = SchemaGenerator::new(SchemaConfig::default());

        let array_schema = generator.generate_schema("Vec<String>").unwrap();
        assert_eq!(array_schema.schema_type, Some("array".to_string()));
        assert!(array_schema.items.is_some());

        let items = array_schema.items.unwrap();
        assert_eq!(items.schema_type, Some("string".to_string()));
    }

    #[test]
    fn test_struct_schema_generation() {
        let mut generator = SchemaGenerator::new(SchemaConfig::default());

        let fields = vec![
            ("id".to_string(), "i32".to_string(), None),
            (
                "name".to_string(),
                "String".to_string(),
                Some("User name".to_string()),
            ),
            ("email".to_string(), "Option<String>".to_string(), None),
        ];

        let schema = generator.generate_struct_schema("User", &fields).unwrap();
        assert_eq!(schema.schema_type, Some("object".to_string()));
        assert_eq!(schema.properties.len(), 3);
        assert_eq!(schema.required.len(), 2); // id and name are required
        assert!(schema.properties.contains_key("id"));
        assert!(schema.properties.contains_key("name"));
        assert!(schema.properties.contains_key("email"));
    }

    #[test]
    fn test_tuple_schema_representation() {
        // Test that tuples are represented correctly for OpenAPI 3.0
        // This test ensures we don't use oneOf incorrectly for tuples

        // Create a mock tuple schema similar to what the derive macro should generate
        let tuple_schema = crate::specification::Schema {
            schema_type: Some("array".to_string()),
            title: Some("TestTuple".to_string()),
            description: Some("A tuple with 2 fields in fixed order: (String, i32). Note: OpenAPI 3.0 cannot precisely represent tuple types - this is a generic array representation.".to_string()),
            items: Some(Box::new(crate::specification::Schema {
                description: Some("Tuple element (type varies by position)".to_string()),
                ..Default::default()
            })),
            ..Default::default()
        };

        // Verify the schema is structured correctly
        assert_eq!(tuple_schema.schema_type, Some("array".to_string()));
        assert!(tuple_schema.description.is_some());
        assert!(tuple_schema
            .description
            .as_ref()
            .unwrap()
            .contains("fixed order"));
        assert!(tuple_schema
            .description
            .as_ref()
            .unwrap()
            .contains("OpenAPI 3.0 cannot precisely represent"));

        // Verify items doesn't use oneOf (which would be incorrect)
        assert!(tuple_schema.items.is_some());
        let items = tuple_schema.items.as_ref().unwrap();
        assert!(items.one_of.is_empty()); // Should NOT use oneOf for tuples
    }
}