Build a minimal example value for a schema. Only required fields are filled in; optional fields are skipped to keep the example small. Visited type- reference names prevent infinite recursion on self-referential schemas.
(root: &RootSchema, obj: &SchemaObject, visited: &mut Vec<String>)
| 405 | /// in; optional fields are skipped to keep the example small. Visited type- |
| 406 | /// reference names prevent infinite recursion on self-referential schemas. |
| 407 | fn example_for(root: &RootSchema, obj: &SchemaObject, visited: &mut Vec<String>) -> Value { |
| 408 | let s = resolve(root, obj); |
| 409 | |
| 410 | // Tagged union: render the first variant. |
| 411 | if let Some(sub) = s.subschemas.as_ref() { |
| 412 | if let Some(variants) = sub.one_of.as_ref() { |
| 413 | if let Some(Schema::Object(first)) = variants.first() { |
| 414 | return example_for(root, first, visited); |
| 415 | } |
| 416 | } |
| 417 | } |
| 418 | |
| 419 | // Enum (e.g. ReplyStatus / Status) — pick the first allowed value. |
| 420 | if let Some(enum_values) = s.enum_values.as_ref() { |
| 421 | if let Some(first) = enum_values.first() { |
| 422 | return first.clone(); |
| 423 | } |
| 424 | } |
| 425 | |
| 426 | // Object: emit required fields only, plus the discriminator if it's an enum-variant object. |
| 427 | if let Some(obj_v) = s.object.as_ref() { |
| 428 | let mut out = Map::new(); |
| 429 | // If this object has a tag const (it's an enum variant), include the tag. |
| 430 | if let Some(tag_val) = tagged_variant_value(root, s) { |
| 431 | if let Some(tag_field) = obj_v.properties.keys().find(|k| { |
| 432 | if let Some(Schema::Object(po)) = obj_v.properties.get(*k) { |
| 433 | po.enum_values.as_ref().map(|v| v.len() == 1).unwrap_or(false) |
| 434 | } else { |
| 435 | false |
| 436 | } |
| 437 | }) { |
| 438 | out.insert(tag_field.clone(), Value::String(tag_val)); |
| 439 | } |
| 440 | } |
| 441 | for name in &obj_v.required { |
| 442 | if let Some(Schema::Object(po)) = obj_v.properties.get(name) { |
| 443 | out.insert(name.clone(), example_for(root, po, visited)); |
| 444 | } |
| 445 | } |
| 446 | return Value::Object(out); |
| 447 | } |
| 448 | |
| 449 | // Primitive: type-driven default. |
| 450 | if let Some(inst) = s.instance_type.as_ref() { |
| 451 | let primary = match inst { |
| 452 | SingleOrVec::Single(b) => **b, |
| 453 | SingleOrVec::Vec(v) => *v.first().unwrap_or(&InstanceType::Null), |
| 454 | }; |
| 455 | return match primary { |
| 456 | InstanceType::String => json!("..."), |
| 457 | InstanceType::Integer => json!(0), |
| 458 | InstanceType::Number => json!(0.0), |
| 459 | InstanceType::Boolean => json!(false), |
| 460 | InstanceType::Array => json!([]), |
| 461 | InstanceType::Object => json!({}), |
| 462 | InstanceType::Null => Value::Null, |
| 463 | }; |
| 464 | } |
no test coverage detected
searching dependent graphs…