Skip to content

Fix SPDX 3.0.1 relationship type gaps - #303

Merged
pellecchialuigi merged 2 commits into
mainfrom
issue-297
Jul 31, 2026
Merged

Fix SPDX 3.0.1 relationship type gaps#303
pellecchialuigi merged 2 commits into
mainfrom
issue-297

Conversation

@pellecchialuigi

Copy link
Copy Markdown
Collaborator
  • Replace SPDXRelationship.relationships list with SpdxRelationshipType enum (str, Enum) covering all valid SPDX 3.0.1 relationship types; all addRelationship call sites updated to use enum members
  • Fix gap 2a: replace invalid hasDocument with hasDocumentation for document-to-nested-document relationships
  • Fix gap 2b: add explicit describes relationship from SpdxDocument to its root element, complementing the existing rootElement property
  • Fix gap 2c: replace generic hasTest with hasTestCase for all relationships pointing to TestCase elements

Closes: #297

pellecchialuigi and others added 2 commits July 29, 2026 15:54
- Replace SPDXRelationship.relationships list with SpdxRelationshipType
  enum (str, Enum) covering all valid SPDX 3.0.1 relationship types;
  all addRelationship call sites updated to use enum members
- Fix gap 2a: replace invalid hasDocument with hasDocumentation for
  document-to-nested-document relationships
- Fix gap 2b: add explicit describes relationship from SpdxDocument to
  its root element, complementing the existing rootElement property
- Fix gap 2c: replace generic hasTest with hasTestCase for all
  relationships pointing to TestCase elements

Closes: #297
Signed-off-by: Luigi Pellecchia <pellecchia.luigi@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
SPDXDocument.to_dict() was building the spdxId as
f"spdx:document:{self.spdx_id}" while self.spdx_id already carries the
full "spdx:document:basil:export:..." prefix, resulting in a
double-prefixed identifier in the serialized JSON-LD.

Every other SPDX class in this file uses self.spdx_id directly; align
SPDXDocument with the same convention.

The bug was latent until the describes relationship (gap 2b) was added:
that relationship's from field references document.spdx_id
(single-prefix), which no longer matched the double-prefixed spdxId
emitted by to_dict(), causing spdx3-validate to reject the document.

Signed-off-by: Luigi Pellecchia <pellecchia.luigi@gmail.com>
Co-authored-by: Cursor <cursoragent@cursor.com>
@pellecchialuigi
pellecchialuigi merged commit 053901d into main Jul 31, 2026
6 of 7 checks passed
@pellecchialuigi
pellecchialuigi deleted the issue-297 branch July 31, 2026 07:17
pellecchialuigi added a commit that referenced this pull request Jul 31, 2026
Commit #303 ("Fix SPDX 3.0.1 relationship type gaps") changed the
relationship type emitted for SW requirement -> test case links from
"hasTest" to the more specific SPDX 3.0.1 type "hasTestCase", but the
assertion added by commit #302 in
test_spdx_sw_requirement_children_exported was never updated to match,
causing a spurious CI failure.

Update the assertion and its error message to check for "hasTestCase"
so the test reflects the relationship type actually emitted by
addSwRequirementTestCases.

Signed-off-by: Luigi Pellecchia <pellecchia.luigi@gmail.com>
pellecchialuigi added a commit that referenced this pull request Aug 7, 2026
* Fix SPDX 3.0.1 opaque metadata in Annotations (#299)

Address the 3.0.1-addressable portion of the opaque-metadata gap identified
in elisa-tech/Safety_Architecture_WG#18 (Gap 4).

All BASIL-specific metadata was previously serialised as raw JSON strings
inside Annotation.statement fields, making entity identifiers and titles
invisible to standard SPDX-aware tooling.

Changes in api/spdx_manager.py:

- Add BASIL_ANNOTATION_VERSION = "2.0" constant so consumers can
  distinguish the new annotation schema from the old one.

- Add SPDXExternalIdentifier class that serialises as a SPDX 3.0.1
  ExternalIdentifier inline object (externalIdentifierType "other").
  The identifier follows the convention basil:<db_tablename>:<db_id>,
  using the model's __tablename__ directly to stay in sync with the
  database schema without a separate mapping layer.

- Extend SPDXFile with an optional external_identifiers list; to_dict()
  emits "externalIdentifier": [...] when the list is non-empty.

- Populate ExternalIdentifier on every persisted BASIL entity:
  apis, sw_requirements, test_specifications, test_cases, documents,
  justifications, test_runs.

- Inject "basil:annotationVersion": "2.0" into every Annotation.statement
  automatically inside SPDXAnnotation.__init__, with no changes required
  at call sites.

- Add clean_entity_annotation_dict() helper that strips "id" and "title"
  from entity annotation payloads — both fields are now carried by the
  ExternalIdentifier on the element itself.

- Add clean_snippet_annotation_dict() helper that strips "offset",
  "section", and "coverage" from snippet annotation payloads — these are
  already represented in software_byteRange and Relationship.completeness
  respectively.

Changes in api/test/test_spdx_api_validation.py:

- Add helper functions _export_and_parse(), _external_identifiers_for(),
  and _assert_external_identifier() shared by the new tests.

- Add one dedicated test per entity type (api, sw_requirements,
  test_specifications, test_cases, justifications, documents, test_runs)
  that asserts the ExternalIdentifier carries the correct
  basil:<tablename>:<id> identifier and a comment containing the entity id.

- Add test_spdx_annotation_version_injected: every Annotation.statement
  must contain "basil:annotationVersion": "2.0".

- Add test_spdx_entity_annotations_have_no_id_or_title: entity annotation
  statements must not contain "id" or "title".

- Add test_spdx_snippet_annotations_stripped: snippet annotation statements
  must not contain "offset", "section", or "coverage".

Signed-off-by: Luigi Pellecchia <pellecchia.luigi@gmail.com>

* Fix stale hasTest assertion in SPDX regression test

Commit #303 ("Fix SPDX 3.0.1 relationship type gaps") changed the
relationship type emitted for SW requirement -> test case links from
"hasTest" to the more specific SPDX 3.0.1 type "hasTestCase", but the
assertion added by commit #302 in
test_spdx_sw_requirement_children_exported was never updated to match,
causing a spurious CI failure.

Update the assertion and its error message to check for "hasTestCase"
so the test reflects the relationship type actually emitted by
addSwRequirementTestCases.

Signed-off-by: Luigi Pellecchia <pellecchia.luigi@gmail.com>

* Fix flake8 error

Signed-off-by: Luigi Pellecchia <pellecchia.luigi@gmail.com>

---------

Signed-off-by: Luigi Pellecchia <pellecchia.luigi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SPDX 3.0.1 Gap - Invalid and Missing SPDX 3.0.1 Relationship Types

1 participant