Skip to content

Commit a70c45a

Browse files
authored
Support type nested in oneOf (#160)
1 parent c70a8ea commit a70c45a

1 file changed

Lines changed: 33 additions & 3 deletions

File tree

src/api/types.rs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,38 @@ pub struct Type {
9292

9393
impl Type {
9494
pub(crate) fn from_schema(name: String, s: SchemaObject) -> anyhow::Result<Self> {
95-
let data = match s.instance_type {
96-
Some(SingleOrVec::Single(it)) => match *it {
95+
let instance_type = match &s.instance_type {
96+
Some(ty) => Some(ty),
97+
None => {
98+
let mut result = None;
99+
100+
for variant in s
101+
.subschemas
102+
.iter()
103+
.filter_map(|s| s.one_of.as_ref())
104+
.flatten()
105+
{
106+
let Schema::Object(schema) = variant else {
107+
bail!("unsupported: boolean oneOf schema");
108+
};
109+
110+
if let Some(ty) = &schema.instance_type {
111+
if let Some(res_ty) = result {
112+
if res_ty != ty {
113+
bail!("unsupported: oneOf schemas with different types");
114+
}
115+
} else {
116+
result = Some(ty);
117+
}
118+
}
119+
}
120+
121+
result
122+
}
123+
};
124+
125+
let data = match instance_type {
126+
Some(SingleOrVec::Single(it)) => match **it {
97127
InstanceType::Object => {
98128
let obj = s.object.unwrap_or_default();
99129
TypeData::from_object_schema(*obj, s.extensions, s.subschemas)?
@@ -126,7 +156,7 @@ impl Type {
126156
_ => bail!("unsupported type {it:?}"),
127157
},
128158
Some(SingleOrVec::Vec(_)) => bail!("unsupported: multiple types"),
129-
None => bail!("unsupported: no type"),
159+
None => bail!("unsupported: schema without a type"),
130160
};
131161

132162
let metadata = s.metadata.unwrap_or_default();

0 commit comments

Comments
 (0)