Skip to content

Fspes 117#36

Merged
MatteoDelOmbra merged 5 commits into
mainfrom
FSPES-117
May 12, 2026
Merged

Fspes 117#36
MatteoDelOmbra merged 5 commits into
mainfrom
FSPES-117

Conversation

@MichalFrends1
Copy link
Copy Markdown
Contributor

@MichalFrends1 MichalFrends1 commented May 7, 2026

Please review my changes :)

Task Update PR template

Review Checklist

  • Task version updated (x.x.0)
  • CHANGELOG.md updated
  • Solution builds
  • Warnings resolved (if possible)
  • Typos resolved
  • Tests cover new code
  • Description how to run tests locally added to README.md (if needed)
  • All tests pass locally

Summary by CodeRabbit

  • New Features
    • Introduced optional XSD schema input for XML validation and improved array/object mapping during JSON conversion.
    • Elements with multiple occurrence constraints now correctly serialize as JSON arrays instead of single values.

Version: 1.2.0

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 7, 2026

Review Change Stack

Walkthrough

This PR adds optional XSD schema support to the XML-to-JSON conversion task. The Input class receives a new XSD property. The ConvertXMLStringToJToken method now branches on whether an XSD is provided, validates XML against it, enriches XML with JSON array attributes based on schema occurrence constraints, and maintains backward compatibility when no schema is supplied.

Changes

XSD-Based Array Mapping Feature

Layer / File(s) Summary
Data Contract
Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken/Definitions/Input.cs
Input class adds optional XSD string property for schema validation and array-vs-singleton serialization control.
Core Logic and Helpers
Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken/ConvertXMLStringToJToken.cs
ConvertXMLStringToJToken branches on XSD presence; new helpers LoadXmlDocument, LoadXmlDocumentWithSchemaHints, CreateSchemaSet, and AddJsonArrayAttributesFromSchema validate XML, enrich array attributes from schema MaxOccurs constraints, and preserve JSON namespace declarations.
Unit Tests
Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs
New ShouldUseXsdToMapSingleElementAsArray test verifies that a single person element with schema maxOccurs='unbounded' serializes as a JSON array.
Version and Documentation
Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.csproj, Frends.JSON.ConvertXMLStringToJToken/CHANGELOG.md
Package version bumped to 1.2.0; changelog documents optional XSD input for consistent array/object mapping.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Suggested reviewers

  • jannejjj
  • jefim

Poem

🐰 A schema arrives, with maxOccurs in hand,
Single elements now form arrays so grand,
XSD validation keeps data on track,
JSON serialization gets a welcome new knack!

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (1 warning, 1 inconclusive)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 14.29% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
Title check ❓ Inconclusive The title 'Fspes 117' is a vague reference to a ticket/issue number and does not describe the actual changes made in the pull request. Replace the title with a clear, descriptive summary of the main change, such as 'Add XSD schema support for XML-to-JSON conversion with array mapping' to better reflect the actual implementation.
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch FSPES-117

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

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs (1)

33-75: ⚡ Quick win

Add tests for the XSD validation-failure path and the maxOccurs=1 (no-array) case.

The current test suite only exercises the happy path. The XmlSchemaValidationException-throwing branch in LoadXmlDocumentWithSchemaHints has no coverage, and there is no test confirming that an element with maxOccurs='1' (or default) is correctly serialized as a JObject rather than a JArray.

Suggested additions:

[TestMethod]
[ExpectedException(typeof(XmlSchemaValidationException))]
public void ShouldThrowWhenXmlFailsXsdValidation()
{
    var input = new Input()
    {
        XML = @"<?xml version='1.0' standalone='no'?>
         <root>
           <unknown>value</unknown>
         </root>",
        XSD = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
                  <xs:element name='root'>
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name='person' maxOccurs='unbounded' />
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                </xs:schema>"
    };

    JSON.ConvertXMLStringToJToken(input);
}

[TestMethod]
public void ShouldNotMapElementAsArrayWhenMaxOccursIsOne()
{
    var input = new Input()
    {
        XML = @"<?xml version='1.0' standalone='no'?>
         <root>
           <person id='1'>
             <name>Alan</name>
           </person>
         </root>",
        XSD = @"<xs:schema xmlns:xs='http://www.w3.org/2001/XMLSchema'>
                  <xs:element name='root'>
                    <xs:complexType>
                      <xs:sequence>
                        <xs:element name='person' maxOccurs='1'>
                          <xs:complexType>
                            <xs:sequence>
                              <xs:element name='name' type='xs:string' />
                            </xs:sequence>
                            <xs:attribute name='id' type='xs:string' />
                          </xs:complexType>
                        </xs:element>
                      </xs:sequence>
                    </xs:complexType>
                  </xs:element>
                </xs:schema>"
    };

    var result = JSON.ConvertXMLStringToJToken(input);
    Assert.IsTrue(result.Success);
    var root = ((JObject)result.Jtoken)["root"];
    Assert.IsNotInstanceOfType(root["person"], typeof(JArray));
}
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In
`@Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs`
around lines 33 - 75, Add two unit tests to cover the XSD validation-failure
path and the maxOccurs=1 non-array case: create a test
ShouldThrowWhenXmlFailsXsdValidation that calls JSON.ConvertXMLStringToJToken
with XML that doesn't match the provided XSD and asserts an
XmlSchemaValidationException is thrown (exercising the
XmlSchemaValidationException branch in LoadXmlDocumentWithSchemaHints), and
create ShouldNotMapElementAsArrayWhenMaxOccursIsOne that supplies an XSD where
person has maxOccurs='1', calls JSON.ConvertXMLStringToJToken and asserts
root["person"] is not a JArray (should be a JObject); name the tests exactly as
above so they integrate with the existing UnitTests.cs suite and reference
JSON.ConvertXMLStringToJToken and LoadXmlDocumentWithSchemaHints in comments if
needed for maintainability.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In
`@Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs`:
- Around line 62-65: Assert result.Success before accessing result.Jtoken: in
the test that calls JSON.ConvertXMLStringToJToken, move the
Assert.IsTrue(result.Success) to immediately after obtaining result and before
the cast that accesses ((JObject)result.Jtoken)["root"] (or perform the cast
only after asserting success). This ensures the test fails with a clear
assertion instead of throwing a NullReferenceException when result.Jtoken is
null.

---

Nitpick comments:
In
`@Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs`:
- Around line 33-75: Add two unit tests to cover the XSD validation-failure path
and the maxOccurs=1 non-array case: create a test
ShouldThrowWhenXmlFailsXsdValidation that calls JSON.ConvertXMLStringToJToken
with XML that doesn't match the provided XSD and asserts an
XmlSchemaValidationException is thrown (exercising the
XmlSchemaValidationException branch in LoadXmlDocumentWithSchemaHints), and
create ShouldNotMapElementAsArrayWhenMaxOccursIsOne that supplies an XSD where
person has maxOccurs='1', calls JSON.ConvertXMLStringToJToken and asserts
root["person"] is not a JArray (should be a JObject); name the tests exactly as
above so they integrate with the existing UnitTests.cs suite and reference
JSON.ConvertXMLStringToJToken and LoadXmlDocumentWithSchemaHints in comments if
needed for maintainability.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: e815f948-0905-457b-ac90-360181a78487

📥 Commits

Reviewing files that changed from the base of the PR and between 39a4669 and 73f0f8d.

📒 Files selected for processing (5)
  • Frends.JSON.ConvertXMLStringToJToken/CHANGELOG.md
  • Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.Tests/UnitTests.cs
  • Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken/ConvertXMLStringToJToken.cs
  • Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken/Definitions/Input.cs
  • Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken/Frends.JSON.ConvertXMLStringToJToken.csproj

@MatteoDelOmbra MatteoDelOmbra merged commit 117af9c into main May 12, 2026
4 checks passed
@MatteoDelOmbra MatteoDelOmbra deleted the FSPES-117 branch May 12, 2026 08:55
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.

3 participants