Skip to content

fix: raise TrestleError when get_singular_alias path ends with numeric index#2144

Open
Acuspeedster wants to merge 2 commits intooscal-compass:developfrom
Acuspeedster:fix/get-singular-alias-numeric-index-2140
Open

fix: raise TrestleError when get_singular_alias path ends with numeric index#2144
Acuspeedster wants to merge 2 commits intooscal-compass:developfrom
Acuspeedster:fix/get-singular-alias-numeric-index-2140

Conversation

@Acuspeedster
Copy link
Contributor

PR Title

fix: raise TrestleError when get_singular_alias path ends with numeric index (#2140)
Closes #2140

Types of changes

  • Hot fix (emergency fix and release)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Documentation (change which affects the documentation site)
  • Breaking change (fix or feature that would cause existing functionality to change)
  • Release (develop -> main)

Quality assurance (all should be covered).

  • My code follows the code style of this project.
  • Documentation for my change is up to date?
  • My PR meets testing requirements.
  • All new and existing tests passed.
  • All commits are signed-off.

Summary

ModelUtils.get_singular_alias() previously returned the numeric index string (e.g. '0') when called with a path that terminates on an array index such as component-definition.components.0. This is meaningless as an alias and was explicitly flagged in the test suite with a # FIXME ideally this should report error comment.

Root cause

In get_singular_alias(), after the traversal loop, the branch that handles non-collection terminal model types (if not utils.is_collection_field_type(model_type)) returned last_alias without checking whether it was a numeric index. When an index like 0 was the last path segment, the loop unwrapped the collection's inner type (leaving a non-collection model type) and the function fell straight through to return last_alias, returning '0'.

Fix

Added a guard after extracting last_alias to check whether it is a pure digit string. If so, a TrestleError is raised with a clear message before any further processing.

elif last_alias.isdigit():
    raise err.TrestleError(
        f'Invalid alias path {alias_path}: path cannot terminate with a numeric index.'
    )

Numeric indices appearing in the middle of a path (e.g. component-definition.components.0.control-implementations) continue to work correctly and are unaffected.

Changes

File Change
trestle/common/model_utils.py Added elif last_alias.isdigit() guard to raise TrestleError
tests/trestle/utils/fs_test.py Updated test: replaced assert '0' == ... + # FIXME with pytest.raises(TrestleError)

Closes #2140

Key links:

Before you merge

  • Ensure it is a 'squash commit' if not a release.
  • Ensure CI is currently passing
  • Check sonar. If you are working for a fork a maintainer will reach out, if required.

…c index (oscal-compass#2140)

Signed-off-by: Acuspeedster <arnavrajsingh@gmail.com>
@Acuspeedster Acuspeedster requested a review from a team as a code owner March 11, 2026 17:36
…c index (oscal-compass#2140)

Signed-off-by: Acuspeedster <arnavrajsingh@gmail.com>
@Jay2006sawant
Copy link

Hi @Acuspeedster, thanks for the PR and the effort!

Since I opened this issue and am already working on it, could you please close this PR for now to avoid duplicate work?
If needed, we can also collaborate on the solution, happy to work together 🙂
Thanks again for contributing!

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.

ModelUtils.get_singular_alias returns numeric index for indexed element path

2 participants