| 1 | use crate::SchemaGenerator; |
| 2 | use crate::_alloc_prelude::*; |
| 3 | use crate::{json_schema, JsonSchema, Schema}; |
| 4 | use alloc::borrow::Cow; |
| 5 | use either1::Either; |
| 6 | |
| 7 | impl<L: JsonSchema, R: JsonSchema> JsonSchema for Either<L, R> { |
| 8 | inline_schema!(); |
| 9 | |
| 10 | fn schema_name() -> Cow<'static, str> { |
| 11 | format!("Either_{}_or_{}", L::schema_name(), R::schema_name()).into() |
| 12 | } |
| 13 | |
| 14 | fn schema_id() -> Cow<'static, str> { |
| 15 | format!("either::Either<{}, {}>", L::schema_id(), R::schema_id()).into() |
| 16 | } |
| 17 | |
| 18 | fn json_schema(generator: &mut SchemaGenerator) -> Schema { |
| 19 | json_schema!({ |
| 20 | "oneOf": [ |
| 21 | { |
| 22 | "type": "object", |
| 23 | "properties": { |
| 24 | "Left": generator.subschema_for::<L>() |
| 25 | }, |
| 26 | "additionalProperties": false, |
| 27 | "required": [ |
| 28 | "Left" |
| 29 | ] |
| 30 | }, |
| 31 | { |
| 32 | "type": "object", |
| 33 | "properties": { |
| 34 | "Right": generator.subschema_for::<R>() |
| 35 | }, |
| 36 | "additionalProperties": false, |
| 37 | "required": [ |
| 38 | "Right" |
| 39 | ] |
| 40 | } |
| 41 | ] |
| 42 | }) |
| 43 | } |
| 44 | } |