From 646e2ddf0e5ffd1e59bfbc631ef91bc7814be185 Mon Sep 17 00:00:00 2001 From: Jim Crossley Date: Wed, 15 Jan 2025 10:44:30 -0500 Subject: [PATCH] Changed purl relationships struct from vec to hashmap Relates #1131 Signed-off-by: Jim Crossley --- entity/src/relationship.rs | 1 + .../fundamental/src/purl/endpoints/test.rs | 9 ++-- .../src/purl/model/details/purl.rs | 25 +++++----- openapi.yaml | 46 +++++++++---------- 4 files changed, 43 insertions(+), 38 deletions(-) diff --git a/entity/src/relationship.rs b/entity/src/relationship.rs index af5851763..669221cb7 100644 --- a/entity/src/relationship.rs +++ b/entity/src/relationship.rs @@ -5,6 +5,7 @@ use std::fmt; Debug, Copy, Clone, + Hash, PartialEq, Eq, EnumIter, diff --git a/modules/fundamental/src/purl/endpoints/test.rs b/modules/fundamental/src/purl/endpoints/test.rs index 18eb13a55..5e5c41d8e 100644 --- a/modules/fundamental/src/purl/endpoints/test.rs +++ b/modules/fundamental/src/purl/endpoints/test.rs @@ -355,13 +355,14 @@ async fn purl_relationships(ctx: &TrustifyContext) -> Result<(), anyhow::Error> .await?; let src = "pkg:rpm/redhat/openssl@3.0.7-18.el9_2?arch=src"; - let x86 = "pkg:rpm/redhat/openssl@3.0.7-18.el9_2?arch=x86_64"; - let uri = format!("/api/v1/purl/{}", urlencoding::encode(x86)); + let bin = "pkg:rpm/redhat/openssl@3.0.7-18.el9_2?arch=x86_64"; + + let uri = format!("/api/v2/purl/{}", urlencoding::encode(bin)); let request = TestRequest::get().uri(&uri).to_request(); let response: Value = app.call_and_read_body_json(request).await; log::debug!("{response:#?}"); - assert_eq!("generated_from", response["relationships"][0][0]); - assert_eq!(src, response["relationships"][0][1]); + + assert_eq!(src, response["relationships"]["generated_from"][0]); Ok(()) } diff --git a/modules/fundamental/src/purl/model/details/purl.rs b/modules/fundamental/src/purl/model/details/purl.rs index 85f803e86..33186c66b 100644 --- a/modules/fundamental/src/purl/model/details/purl.rs +++ b/modules/fundamental/src/purl/model/details/purl.rs @@ -38,7 +38,7 @@ pub struct PurlDetails { pub base: BasePurlHead, pub advisories: Vec, pub licenses: Vec, - pub relationships: Vec<(Relationship, String)>, + pub relationships: HashMap>, } impl PurlDetails { @@ -115,16 +115,19 @@ impl PurlDetails { .all(tx) .await?; - let relationships = package_relates_to_package::Entity::find() - .filter( - package_relates_to_package::Column::LeftNodeId - .eq(qualified_package.purl.to_string()), - ) - .all(tx) - .await? - .into_iter() - .map(|model| (model.relationship, model.right_node_id)) - .collect(); + let relationships: HashMap> = + package_relates_to_package::Entity::find() + .filter( + package_relates_to_package::Column::LeftNodeId + .eq(qualified_package.purl.to_string()), + ) + .all(tx) + .await? + .into_iter() + .fold(HashMap::new(), |mut h, m| { + h.entry(m.relationship).or_default().push(m.right_node_id); + h + }); Ok(PurlDetails { head: PurlHead::from_entity(&package, &package_version, qualified_package, tx).await?, diff --git a/openapi.yaml b/openapi.yaml index 1638641ee..36670df9d 100644 --- a/openapi.yaml +++ b/openapi.yaml @@ -3279,30 +3279,30 @@ components: items: $ref: '#/components/schemas/PurlLicenseSummary' relationships: - type: array - items: + type: object + additionalProperties: type: array - items: false - prefixItems: - - type: string - enum: - - contained_by - - dependency_of - - dev_dependency_of - - optional_dependency_of - - provided_dependency_of - - test_dependency_of - - runtime_dependency_of - - example_of - - generated_from - - ancestor_of - - variant_of - - build_tool_of - - dev_tool_of - - described_by - - package_of - - undefined - - type: string + items: + type: string + propertyNames: + type: string + enum: + - contained_by + - dependency_of + - dev_dependency_of + - optional_dependency_of + - provided_dependency_of + - test_dependency_of + - runtime_dependency_of + - example_of + - generated_from + - ancestor_of + - variant_of + - build_tool_of + - dev_tool_of + - described_by + - package_of + - undefined version: $ref: '#/components/schemas/VersionedPurlHead' PurlHead: