Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 48 additions & 2 deletions compiler-rs/clients_schema_to_openapi/src/schemas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,10 +404,56 @@ impl<'a> TypesAndComponents<'a> {
// TODO: typed-keys: add an extension to identify it?
}
Some(TypeAliasVariants::InternalTag(tag)) => {
// TODO: add tag.default_tag as an extension
// outputs a map of discriminator values to schema references
// e.g. { "type1": "#/components/schemas/Type1", "type2": "#/components/schemas/Type2" }
let mut disc_mapping = IndexMap::new();
let ValueOf::UnionOf(union) = &alias.typ else {
bail!("InternalTag type alias {} does not wrap a union", alias.base.name);
};
// Extract union members and build mapping
for item in &union.items {
let ValueOf::InstanceOf(instance) = item else {
bail!(
"InternalTag union member in type alias {} is not an instance_of",
alias.base.name
);
};

match self.model.get_type(&instance.typ) {
Ok(TypeDefinition::Interface(variant_itf)) => {
// Find the discriminator property in the variant
let Some(disc_prop) = variant_itf.properties.iter().find(|p| p.name == tag.tag) else {
bail!(
"InternalTag union member in type alias {} does not have discriminator property {}",
alias.base.name,
tag.tag
);
};

// Extract the literal value
let ValueOf::LiteralValue(literal) = &disc_prop.typ else {
bail!(
"InternalTag union member in type alias {} has non-literal discriminator property {}",
alias.base.name,
tag.tag
);
};
let discriminator_value = literal.value.to_string();
let schema_ref = format!("#/components/schemas/{}", instance.typ.schema_name());
disc_mapping.insert(discriminator_value, schema_ref);
}
_ => bail!(
"InternalTag union member in type alias {} is not an interface",
alias.base.name
),
}
}
disc_mapping.sort_unstable_keys();

schema.schema_data.discriminator = Some(Discriminator {
property_name: tag.tag.clone(),
mapping: Default::default(),
mapping: disc_mapping,
// TODO: add tag.default_tag as an extension
extensions: Default::default(),
});
}
Expand Down
Binary file modified compiler-rs/compiler-wasm-lib/pkg/compiler_wasm_lib_bg.wasm
Binary file not shown.
Loading