Skip to content

Commit

Permalink
Changed purl relationships struct from vec to hashmap
Browse files Browse the repository at this point in the history
Relates #1131

Signed-off-by: Jim Crossley <[email protected]>
  • Loading branch information
jcrossley3 committed Jan 15, 2025
1 parent efc9394 commit 646e2dd
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 38 deletions.
1 change: 1 addition & 0 deletions entity/src/relationship.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fmt;
Debug,
Copy,
Clone,
Hash,
PartialEq,
Eq,
EnumIter,
Expand Down
9 changes: 5 additions & 4 deletions modules/fundamental/src/purl/endpoints/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,14 @@ async fn purl_relationships(ctx: &TrustifyContext) -> Result<(), anyhow::Error>
.await?;

let src = "pkg:rpm/redhat/[email protected]_2?arch=src";
let x86 = "pkg:rpm/redhat/[email protected]_2?arch=x86_64";
let uri = format!("/api/v1/purl/{}", urlencoding::encode(x86));
let bin = "pkg:rpm/redhat/[email protected]_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(())
}
25 changes: 14 additions & 11 deletions modules/fundamental/src/purl/model/details/purl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct PurlDetails {
pub base: BasePurlHead,
pub advisories: Vec<PurlAdvisory>,
pub licenses: Vec<PurlLicenseSummary>,
pub relationships: Vec<(Relationship, String)>,
pub relationships: HashMap<Relationship, Vec<String>>,
}

impl PurlDetails {
Expand Down Expand Up @@ -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<Relationship, Vec<_>> =
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?,
Expand Down
46 changes: 23 additions & 23 deletions openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 646e2dd

Please sign in to comment.