Skip to content

Commit 96b3cd4

Browse files
author
Daniel Pezely
committed
Obtain name from SchemaObject, else subtype_{idx}
1 parent 150d05e commit 96b3cd4

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

typify-impl/src/structs.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,20 @@ impl TypeSpace {
288288
}
289289

290290
// TODO we need a reasonable name that could be derived
291-
// from the name of the type
292-
let name = format!("subtype_{}", idx);
291+
// from the name of the type. Use of SchemaObject here
292+
// accommodates "anyOf" and "allOf" but others, TBD. e.g.,
293+
// "foo":{"items":{"anyOf":[{"$ref": "#/$defs/bar-baz"}, ...]}}
294+
// provides "bar_baz".
295+
let name = match schema {
296+
Schema::Object(SchemaObject {
297+
reference: Some(s), ..
298+
}) => match s.rsplit_once('/') {
299+
Some((_, name)) => name,
300+
None => s,
301+
}
302+
.replace('-', "_"),
303+
_ => format!("subtype_{}", idx),
304+
};
293305

294306
Ok(StructProperty {
295307
name,

0 commit comments

Comments
 (0)