Skip to content

Commit

Permalink
H-3677: Include entity type in closed entity type's allOf (#5739)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann authored Nov 27, 2024
1 parent 7dfddba commit a5c5414
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ pub struct ClosedEntityTypeMetadata {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title_plural: Option<String>,
pub description: String,
#[serde(default, skip_serializing_if = "InverseEntityTypeMetadata::is_empty")]
pub inverse: InverseEntityTypeMetadata,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[cfg_attr(
target_arch = "wasm32",
tsify(type = "[EntityTypeDisplayMetadata, ...EntityTypeDisplayMetadata[]]")
)]
pub all_of: Vec<EntityTypeDisplayMetadata>,
#[serde(default, skip_serializing_if = "InverseEntityTypeMetadata::is_empty")]
pub inverse: InverseEntityTypeMetadata,
}

#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
Expand All @@ -46,16 +45,15 @@ pub struct ClosedEntityType {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title_plural: Option<String>,
pub description: String,
#[serde(flatten)]
pub constraints: EntityConstraints,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
#[cfg_attr(
target_arch = "wasm32",
tsify(type = "[EntityTypeDisplayMetadata, ...EntityTypeDisplayMetadata[]]")
)]
pub all_of: Vec<EntityTypeDisplayMetadata>,
#[serde(default, skip_serializing_if = "InverseEntityTypeMetadata::is_empty")]
pub inverse: InverseEntityTypeMetadata,
#[serde(flatten)]
pub constraints: EntityConstraints,
}

#[derive(Debug, Error)]
Expand All @@ -69,6 +67,8 @@ pub enum ResolveClosedEntityTypeError {
IncompatibleProperty(BaseUrl),
#[error("The entity type has an empty schema")]
EmptySchema,
#[error("The entity type has an inheritance depth overflow")]
InheritanceDepthOverflow,
}

impl ClosedEntityType {
Expand All @@ -82,16 +82,21 @@ impl ClosedEntityType {
resolve_data: &EntityTypeResolveData,
) -> Result<Self, Report<ResolveClosedEntityTypeError>> {
let mut closed_schema = Self {
id: schema.id,
id: schema.id.clone(),
constraints: schema.constraints,
title: schema.title,
title_plural: schema.title_plural,
description: schema.description,
inverse: schema.inverse,
all_of: Vec::new(),
all_of: vec![EntityTypeDisplayMetadata {
id: schema.id,
depth: 0,
icon: schema.icon,
label_property: schema.label_property,
}],
};

for (_depth, entity_type) in resolve_data.ordered_schemas() {
for (depth, entity_type) in resolve_data.ordered_schemas() {
schema.all_of.remove((&entity_type.id).into());
closed_schema
.constraints
Expand All @@ -105,13 +110,16 @@ impl ClosedEntityType {
&mut closed_schema.constraints.links,
entity_type.constraints.links.clone(),
);
if entity_type.icon.is_some() || entity_type.label_property.is_some() {
closed_schema.all_of.push(EntityTypeDisplayMetadata {
id: entity_type.id.clone(),
label_property: entity_type.label_property.clone(),
icon: entity_type.icon.clone(),
});
}

closed_schema.all_of.push(EntityTypeDisplayMetadata {
id: entity_type.id.clone(),
depth: depth
.inner()
.checked_add(1)
.ok_or(ResolveClosedEntityTypeError::InheritanceDepthOverflow)?,
label_property: entity_type.label_property.clone(),
icon: entity_type.icon.clone(),
});
}

ensure!(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub struct EntityTypeSchemaMetadata {
pub struct EntityTypeDisplayMetadata {
#[serde(rename = "$id")]
pub id: VersionedUrl,
pub depth: u16,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub label_property: Option<BaseUrl>,
#[serde(default, skip_serializing_if = "Option::is_none")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,16 @@ describe("Entity type CRU", () => {
...(userType.schema.links ?? {}),
...(userType.schema.links ?? {}),
},
allOf: [
{
depth: 0,
$id: systemEntityTypes.user.entityTypeId,
},
{
depth: 1,
$id: systemEntityTypes.actor.entityTypeId,
},
],
} satisfies ClosedEntityType,
},
]);
Expand Down Expand Up @@ -394,6 +404,7 @@ describe("Entity type CRU", () => {
$id: closedEntityType.schema.$id,
title: closedEntityType.schema.title,
description: closedEntityType.schema.description,
allOf: closedEntityType.schema.allOf,
}) as ClosedMultiEntityType["allOf"][0],
),
);
Expand Down

0 comments on commit a5c5414

Please sign in to comment.