oag-core 0.20.6

OpenAPI 3.2 parser, IR, and transforms for oag
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
use indexmap::IndexMap;

use crate::error::TransformError;
use crate::ir::{
    IrAliasSchema, IrDiscriminator, IrEnumSchema, IrEnumVariant, IrField, IrObjectSchema, IrSchema,
    IrType, IrUnionSchema,
};
use crate::parse::schema::{AdditionalProperties, Schema, SchemaOrRef, SchemaType, TypeSet};

use super::name_normalizer::normalize_name;

/// Convert a parsed `SchemaOrRef` to an `IrType`.
pub fn schema_or_ref_to_ir_type(schema_or_ref: &SchemaOrRef) -> IrType {
    match schema_or_ref {
        SchemaOrRef::Ref { ref_path } => {
            let name = ref_path.rsplit('/').next().unwrap_or("Unknown");
            IrType::Ref(normalize_name(name).pascal_case)
        }
        SchemaOrRef::Schema(schema) => schema_to_ir_type(schema),
    }
}

/// Convert a parsed `Schema` to an `IrType`.
pub fn schema_to_ir_type(schema: &Schema) -> IrType {
    // Handle composition first
    if !schema.one_of.is_empty() {
        let variants: Vec<IrType> = schema.one_of.iter().map(schema_or_ref_to_ir_type).collect();
        return IrType::Union(variants);
    }
    if !schema.any_of.is_empty() {
        let variants: Vec<IrType> = schema.any_of.iter().map(schema_or_ref_to_ir_type).collect();
        return IrType::Union(variants);
    }
    if !schema.all_of.is_empty() {
        if schema.all_of.len() == 1 {
            return schema_or_ref_to_ir_type(&schema.all_of[0]);
        }
        let parts: Vec<IrType> = schema
            .all_of
            .iter()
            .map(|sub| match sub {
                SchemaOrRef::Ref { .. } => schema_or_ref_to_ir_type(sub),
                SchemaOrRef::Schema(s) => {
                    if s.properties.is_empty() {
                        schema_to_ir_type(s)
                    } else {
                        let fields: Vec<(String, IrType, bool)> = s
                            .properties
                            .iter()
                            .map(|(name, prop)| {
                                (
                                    name.clone(),
                                    schema_or_ref_to_ir_type(prop),
                                    s.required.contains(name),
                                )
                            })
                            .collect();
                        IrType::Object(fields)
                    }
                }
            })
            .collect();
        return IrType::Intersection(parts);
    }

    // Handle enum
    if !schema.enum_values.is_empty() {
        let variants: Vec<IrType> = schema
            .enum_values
            .iter()
            .filter_map(|v| {
                if let Some(s) = v.as_str() {
                    Some(IrType::StringLiteral(s.to_string()))
                } else {
                    v.as_i64().map(IrType::IntegerLiteral)
                }
            })
            .collect();
        if variants.len() == 1 {
            return variants.into_iter().next().unwrap();
        }
        if variants.len() > 1 {
            return IrType::Union(variants);
        }
        return IrType::String; // fallback for non-string/non-integer enums
    }

    // Handle const
    if let Some(ref val) = schema.const_value {
        if let Some(s) = val.as_str() {
            return IrType::StringLiteral(s.to_string());
        }
        if let Some(i) = val.as_i64() {
            return IrType::IntegerLiteral(i);
        }
        return IrType::String;
    }

    // Handle type
    match &schema.schema_type {
        Some(TypeSet::Single(t)) => match t {
            SchemaType::String => match schema.format.as_deref() {
                Some("date-time" | "date") => IrType::DateTime,
                Some("binary" | "byte") => IrType::Binary,
                _ => IrType::String,
            },
            SchemaType::Number => IrType::Number,
            SchemaType::Integer => IrType::Integer,
            SchemaType::Boolean => IrType::Boolean,
            SchemaType::Null => IrType::Null,
            SchemaType::Array => match &schema.items {
                Some(items) => IrType::Array(Box::new(schema_or_ref_to_ir_type(items))),
                None => IrType::Array(Box::new(IrType::Any)),
            },
            SchemaType::Object => resolve_object_type(schema),
        },
        Some(TypeSet::Multiple(types)) => {
            let non_null: Vec<_> = types.iter().filter(|t| **t != SchemaType::Null).collect();
            let has_null = types.contains(&SchemaType::Null);
            if non_null.len() == 1 {
                let single = Schema {
                    schema_type: Some(TypeSet::Single(non_null[0].clone())),
                    ..schema.clone()
                };
                let base = schema_to_ir_type(&single);
                if has_null {
                    IrType::Union(vec![base, IrType::Null])
                } else {
                    base
                }
            } else if non_null.is_empty() && has_null {
                IrType::Null
            } else {
                // Multiple non-null types — build union of all
                let mut variants: Vec<IrType> = non_null
                    .iter()
                    .map(|t| {
                        let s = Schema {
                            schema_type: Some(TypeSet::Single((*t).clone())),
                            ..schema.clone()
                        };
                        schema_to_ir_type(&s)
                    })
                    .collect();
                if has_null {
                    variants.push(IrType::Null);
                }
                IrType::Union(variants)
            }
        }
        None => {
            // No type specified — check if it has properties (implicit object)
            if !schema.properties.is_empty() {
                resolve_object_type(schema)
            } else if schema.items.is_some() {
                match &schema.items {
                    Some(items) => IrType::Array(Box::new(schema_or_ref_to_ir_type(items))),
                    None => IrType::Array(Box::new(IrType::Any)),
                }
            } else {
                IrType::Any
            }
        }
    }
}

fn resolve_object_type(schema: &Schema) -> IrType {
    if schema.properties.is_empty() {
        match &schema.additional_properties {
            Some(AdditionalProperties::Schema(s)) => {
                IrType::Map(Box::new(schema_or_ref_to_ir_type(s)))
            }
            Some(AdditionalProperties::Bool(true)) => IrType::Map(Box::new(IrType::Any)),
            Some(AdditionalProperties::Bool(false)) | None => IrType::Any,
        }
    } else {
        let fields: Vec<(String, IrType, bool)> = schema
            .properties
            .iter()
            .map(|(name, prop)| {
                let required = schema.required.contains(name);
                (name.clone(), schema_or_ref_to_ir_type(prop), required)
            })
            .collect();
        IrType::Object(fields)
    }
}

/// Convert a named component schema to an `IrSchema`.
pub fn schema_or_ref_to_ir_schema(
    name: &str,
    schema_or_ref: &SchemaOrRef,
) -> Result<IrSchema, TransformError> {
    match schema_or_ref {
        SchemaOrRef::Ref { ref_path } => {
            let target = ref_path.rsplit('/').next().unwrap_or("Unknown");
            Ok(IrSchema::Alias(IrAliasSchema {
                name: normalize_name(name),
                description: None,
                target: IrType::Ref(normalize_name(target).pascal_case),
            }))
        }
        SchemaOrRef::Schema(schema) => schema_to_ir_schema(name, schema),
    }
}

/// Convert a named `Schema` to an `IrSchema`.
pub fn schema_to_ir_schema(name: &str, schema: &Schema) -> Result<IrSchema, TransformError> {
    let normalized = normalize_name(name);

    // Check for enum
    if !schema.enum_values.is_empty() {
        let variants: Vec<IrEnumVariant> = schema
            .enum_values
            .iter()
            .filter_map(|v| {
                if let Some(s) = v.as_str() {
                    Some(IrEnumVariant::String(s.to_string()))
                } else {
                    v.as_i64().map(IrEnumVariant::Integer)
                }
            })
            .collect();
        return Ok(IrSchema::Enum(IrEnumSchema {
            name: normalized,
            description: schema.description.clone(),
            variants,
        }));
    }

    // Check for oneOf / anyOf (union)
    if !schema.one_of.is_empty() || !schema.any_of.is_empty() {
        let variants_src = if !schema.one_of.is_empty() {
            &schema.one_of
        } else {
            &schema.any_of
        };
        let variants: Vec<IrType> = variants_src.iter().map(schema_or_ref_to_ir_type).collect();
        let discriminator = schema.discriminator.as_ref().map(|d| IrDiscriminator {
            property_name: d.property_name.clone(),
            mapping: d
                .mapping
                .iter()
                .map(|(k, v)| {
                    let name = v.rsplit('/').next().unwrap_or(v);
                    (k.clone(), normalize_name(name).pascal_case)
                })
                .collect(),
        });
        return Ok(IrSchema::Union(IrUnionSchema {
            name: normalized,
            description: schema.description.clone(),
            variants,
            discriminator,
        }));
    }

    // Check for allOf
    if !schema.all_of.is_empty() {
        let has_refs = schema
            .all_of
            .iter()
            .any(|s| matches!(s, SchemaOrRef::Ref { .. }));
        if has_refs {
            // Build intersection: refs stay as Ref, inline schemas become Objects
            let mut parts: Vec<IrType> = schema
                .all_of
                .iter()
                .map(|sub| match sub {
                    SchemaOrRef::Ref { .. } => schema_or_ref_to_ir_type(sub),
                    SchemaOrRef::Schema(s) => {
                        let fields = build_fields(&s.properties, &s.required);
                        if fields.is_empty() {
                            schema_to_ir_type(s)
                        } else {
                            let inline_fields: Vec<(String, IrType, bool)> = fields
                                .into_iter()
                                .map(|f| (f.original_name, f.field_type, f.required))
                                .collect();
                            IrType::Object(inline_fields)
                        }
                    }
                })
                .collect();
            // Add extra properties from the parent schema if any
            if !schema.properties.is_empty() {
                let extra_fields = build_fields(&schema.properties, &schema.required);
                let inline_fields: Vec<(String, IrType, bool)> = extra_fields
                    .into_iter()
                    .map(|f| (f.original_name, f.field_type, f.required))
                    .collect();
                parts.push(IrType::Object(inline_fields));
            }
            return Ok(IrSchema::Alias(IrAliasSchema {
                name: normalized,
                description: schema.description.clone(),
                target: IrType::Intersection(parts),
            }));
        }
        // No refs — safe to flatten merge as before
        let merged = merge_all_of(&schema.all_of, &schema.properties, &schema.required);
        return Ok(IrSchema::Object(IrObjectSchema {
            name: normalized,
            description: schema.description.clone(),
            fields: merged,
            additional_properties: None,
        }));
    }

    // Check if it's a simple type alias
    match &schema.schema_type {
        Some(TypeSet::Single(SchemaType::Object)) | None if !schema.properties.is_empty() => {
            // Object with properties
            let fields = build_fields(&schema.properties, &schema.required);
            let additional = schema
                .additional_properties
                .as_ref()
                .and_then(|ap| match ap {
                    AdditionalProperties::Schema(s) => Some(schema_or_ref_to_ir_type(s)),
                    AdditionalProperties::Bool(true) => Some(IrType::Any),
                    _ => None,
                });
            Ok(IrSchema::Object(IrObjectSchema {
                name: normalized,
                description: schema.description.clone(),
                fields,
                additional_properties: additional,
            }))
        }
        _ => {
            // Simple alias (string, number, array, etc.)
            let target = schema_to_ir_type(schema);
            Ok(IrSchema::Alias(IrAliasSchema {
                name: normalized,
                description: schema.description.clone(),
                target,
            }))
        }
    }
}

fn build_fields(properties: &IndexMap<String, SchemaOrRef>, required: &[String]) -> Vec<IrField> {
    properties
        .iter()
        .map(|(name, prop)| {
            let (description, read_only, write_only, default_repr) = match prop {
                SchemaOrRef::Schema(s) => (
                    s.description.clone(),
                    s.read_only.unwrap_or(false),
                    s.write_only.unwrap_or(false),
                    schema_default_literal(s),
                ),
                _ => (None, false, false, None),
            };
            IrField {
                name: normalize_name(name),
                original_name: name.clone(),
                field_type: schema_or_ref_to_ir_type(prop),
                // Required-ness derives solely from the schema's `required` list;
                // a `default`/`const`/single-value enum never flips it.
                required: required.contains(name),
                description,
                read_only,
                write_only,
                default_repr,
            }
        })
        .collect()
}

/// Compute the literal default value of a property schema, if any.
///
/// Priority: `const` -> single-element `enum` -> scalar `default`. The value is
/// returned as a literal `IrType` (`StringLiteral`/`IntegerLiteral`) so packs
/// render it through the same type-map literal templates used for types. This is
/// purely a value; it does not affect required-ness.
fn schema_default_literal(schema: &Schema) -> Option<IrType> {
    if let Some(ref val) = schema.const_value {
        return json_to_literal(val);
    }
    if schema.enum_values.len() == 1 {
        return json_to_literal(&schema.enum_values[0]);
    }
    if let Some(ref val) = schema.default_value {
        return json_to_literal(val);
    }
    None
}

/// Convert a JSON scalar into a literal `IrType`, mirroring the enum/const
/// literal mapping in `schema_to_ir_type`. Non-string/non-integer values yield
/// `None`.
fn json_to_literal(val: &serde_json::Value) -> Option<IrType> {
    if let Some(s) = val.as_str() {
        return Some(IrType::StringLiteral(s.to_string()));
    }
    val.as_i64().map(IrType::IntegerLiteral)
}

fn merge_all_of(
    all_of: &[SchemaOrRef],
    extra_properties: &IndexMap<String, SchemaOrRef>,
    extra_required: &[String],
) -> Vec<IrField> {
    let mut fields = Vec::new();

    for item in all_of {
        if let SchemaOrRef::Schema(schema) = item {
            fields.extend(build_fields(&schema.properties, &schema.required));
            // Recursively merge nested allOf
            if !schema.all_of.is_empty() {
                fields.extend(merge_all_of(&schema.all_of, &IndexMap::new(), &[]));
            }
        }
    }

    // Add extra properties from the parent schema
    fields.extend(build_fields(extra_properties, extra_required));

    fields
}