Skip to content

Make AasBuilder build atomic: clone working state, validate references, and add tests#22

Merged
Freezor merged 2 commits into12-enable-incremental-submodel-composition-via-addsubmodel-at-any-stage-of-the-build-processfrom
codex/fix-atomicity-in-build-fragment-application
Mar 27, 2026
Merged

Make AasBuilder build atomic: clone working state, validate references, and add tests#22
Freezor merged 2 commits into12-enable-incremental-submodel-composition-via-addsubmodel-at-any-stage-of-the-build-processfrom
codex/fix-atomicity-in-build-fragment-application

Conversation

@Freezor
Copy link
Copy Markdown
Owner

@Freezor Freezor commented Mar 27, 2026

Motivation

  • Ensure build-time operations are defensive so partial fragment application cannot corrupt builder state and shell->submodel references are validated before committing changes.
  • Provide clearer diagnostics for duplicate submodel ids, missing fragment targets, and invalid shell submodel references.
  • Preserve backwards compatibility while documenting the behavior difference for AddExistingSubmodel.

Description

  • Implemented transactional/defensive build behavior by cloning internal shell and submodel lists at the start of AasBuilder.Build, applying validations and fragments against the working copies, and committing only on success; this prevents partial application on failure and enables retrying after failure.
  • Added pre-validation for duplicate submodel ids and for staged fragment targets to surface errors before applying fragments, and added explicit checks that each shell submodel reference resolves to a known submodel id.
  • Introduced cloning helpers: CloneSubmodels, CloneSubmodel, CloneShells, CloneShell, and CloneReference to produce environment copies returned by Build and to isolate working state during build.
  • Added unit tests covering shell references and fragment rollback semantics (Build_WithShellReferencingKnownSubmodelId_ShouldSucceed, Build_WithShellReferencingUnknownSubmodelId_ShouldThrowInvalidOperationException, and Build_WhenFragmentBatchFails_ShouldRollbackAppliedFragmentsBeforeRetry) and updated API/method XML/remarks and README note about AddExistingSubmodel behavior.

Testing

  • Ran the unit test suite with dotnet test, including the three new tests, and all tests passed.
  • Verified that Build_WithShellReferencingKnownSubmodelId_ShouldSucceed succeeds when the submodel exists.
  • Verified that Build_WithShellReferencingUnknownSubmodelId_ShouldThrowInvalidOperationException and Build_WhenFragmentBatchFails_ShouldRollbackAppliedFragmentsBeforeRetry both surface the expected InvalidOperationException conditions.

Codex Task

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 27, 2026

Important

Review skipped

Auto reviews are disabled on base/target branches other than the default branch.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 01ad3bdf-6151-4902-9360-3a458238ca93

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch codex/fix-atomicity-in-build-fragment-application

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 694a5cfd63

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +258 to +262
return new Submodel(id: source.Id)
{
IdShort = source.IdShort,
SubmodelElements = source.SubmodelElements?.ToList()
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve full submodel state when cloning

CloneSubmodel recreates a Submodel with only Id, IdShort, and SubmodelElements, so any other configured fields (for example SemanticId set via SubmodelBuilder.WithSemanticId, descriptions, qualifiers, or kind) are silently dropped during Build(). Because Build() then writes these clones back into _submodels, this is destructive: metadata is lost after the first successful build and cannot be recovered in later builds.

Useful? React with 👍 / 👎.

Comment on lines +275 to +279
return new AssetAdministrationShell(id: source.Id, assetInformation: source.AssetInformation)
{
IdShort = source.IdShort,
Submodels = source.Submodels?.Select(CloneReference).ToList()
};
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Copy all shell properties in CloneShell

CloneShell only copies Id, AssetInformation, IdShort, and Submodels, which strips other valid AssetAdministrationShell data (e.g. DerivedFrom, administration/description fields, extensions, embedded data specs) on every Build(). Since these reduced clones are committed back into _shells, a successful build permanently erases shell metadata and subsequent environments become incomplete.

Useful? React with 👍 / 👎.

…del-at-any-stage-of-the-build-process' into codex/fix-atomicity-in-build-fragment-application

# Conflicts:
#	FluentAAS/FluentAAS.Builder/AasBuilder.cs
#	FluentAAS/FluentAAS.Builder/IAasBuilder.cs
#	FluentAAS/FluentAAS.Builder/SubModel/SubmodelFragmentBuilder.cs
@Freezor Freezor merged commit 65dcf8b into 12-enable-incremental-submodel-composition-via-addsubmodel-at-any-stage-of-the-build-process Mar 27, 2026
2 of 3 checks passed
@Freezor Freezor deleted the codex/fix-atomicity-in-build-fragment-application branch March 27, 2026 12:50
Freezor added a commit that referenced this pull request Mar 27, 2026
#20)

* Add staged submodel composition and validation (fragments) with tests (#19)

* Update README for staged submodel composition and remove extra spec doc

* Fix staged fragment idempotency and preserve internal submodel compatibility

* 📝 Add docstrings to `12-enable-incremental-submodel-composition-via-addsubmodel-at-any-stage-of-the-build-process` (#21)

Docstrings generation was requested by @Freezor.

* #20 (comment)

The following files were modified:

* `FluentAAS/FluentAAS.Builder/AasBuilder.cs`
* `FluentAAS/FluentAAS.Builder/IAasBuilder.cs`
* `FluentAAS/FluentAAS.Builder/SubModel/SubmodelFragmentBuilder.cs`

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>

* 📝 CodeRabbit Chat: Add unit tests

* Fix malformed XML docs in builder interfaces (#22)

* FEAT Fix docs issue

* Make submodel fragment application transactional in Build (#23)

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant