Skip to content

[Fix] retain namespaces and root attributes on round-trip; fixes #44#79

Merged
samatstariongroup merged 3 commits into
developmentfrom
retain-namespaces-roundtrip
Jun 6, 2026
Merged

[Fix] retain namespaces and root attributes on round-trip; fixes #44#79
samatstariongroup merged 3 commits into
developmentfrom
retain-namespaces-roundtrip

Conversation

@samatstariongroup

Copy link
Copy Markdown
Member

Summary

Fixes #44 — when a .reqif with custom namespaces is deserialized and serialized to a new destination, the declared namespaces (and other root attributes) should be retained.

The capture/re-emit mechanism on the REQ-IF root already round-tripped the default and xmlns:* declarations, but it had real defects for prefixed, non-xmlns attributes (e.g. xsi:schemaLocation) and lacked any test coverage. While adding that coverage, a latent file-output truncation bug was also surfaced and fixed.

Changes

  • Capture the namespace URI on readXmlAttribute gains a NamespaceUri; ReqIF.ReadXml / ReadXmlAsync store reader.NamespaceURI for each captured root attribute.
  • Correct write of prefixed attributesWriteNameSpaceAttributes / WriteNameSpaceAttributesAsync now emit ordinary prefixed attributes (e.g. xsi:schemaLocation) using the captured namespace URI instead of passing the prefix where a namespace URI is expected. The sync and async paths are aligned (they previously diverged).
  • No mutation of captured state — the synthesized XHTML namespace declaration is built into a local list rather than appended to the captured attributes, so the same ReqIF can be serialized repeatedly without accumulating duplicate declarations.
  • FileMode.Create for file outputReqIFSerializer.Serialize/SerializeAsync (file overloads) used FileMode.OpenOrCreate, which does not truncate; serializing a shorter document over a longer existing file left trailing bytes and produced invalid XML. Switched to FileMode.Create.

Tests

New NamespaceRetentionTestFixture (round-trips through deserialize → serialize → re-parse the root):

  • xmlns default + xmlns:configuration / xmlns:id / xmlns:xhtml retained (sync and async).
  • xmlns:xsi + xsi:schemaLocation prefixed attribute retained (fails before the fix).
  • repeated serialization does not duplicate declarations.

Verification

  • dotnet build ReqIFSharp.sln -c Release — 0 warnings / 0 errors.
  • dotnet test ReqIFSharp.sln380 tests pass (296 core incl. 4 new, 84 extensions). The two DefaultValueDemo round-trip-to-file tests, which exercised the OpenOrCreate path, now pass.

No .csproj / version changes.

ReqIF.ReadXml now captures each root attribute's namespace URI, and WriteNameSpaceAttributes (sync + async) emits prefixed non-namespace attributes such as xsi:schemaLocation correctly instead of passing the prefix where a namespace URI is expected; the sync and async write paths are aligned. The XHTML namespace declaration is no longer appended to the captured attribute list, so a ReqIF can be serialized more than once without accumulating duplicates.

Also fixes ReqIFSerializer file output to use FileMode.Create instead of FileMode.OpenOrCreate: writing a shorter document over a longer existing file previously left trailing bytes, producing invalid XML when serializing to an existing destination.

Adds NamespaceRetentionTestFixture covering xmlns declarations, prefixed attributes, async, and repeated serialization.
Adds data-driven round-trip tests over Datatype-Demo, ProR_Traceability-Template, DefaultValueDemo and reqifsharpgenerated (.reqif), and Spielwiese, requirements-and-objects and test-multiple-reqif (.reqifz, incl. async and the multi-document archive). Each asserts every root namespace declaration (and xml:lang / xsi:schemaLocation) present in the source is retained in the serialized output.
@sonarqubecloud

sonarqubecloud Bot commented Jun 6, 2026

Copy link
Copy Markdown

@samatstariongroup samatstariongroup merged commit a568593 into development Jun 6, 2026
8 checks passed
@samatstariongroup samatstariongroup deleted the retain-namespaces-roundtrip branch June 6, 2026 16:06
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.

When a reqif file is deserialized to a ReqIF object and then serialized to a new destination file, the namespaces should be retained

1 participant