Skip to content

Comments

Add XML conversion to bodies, and fix XML data conversion #214

Open
ySnoopyDogy wants to merge 6 commits intopb33f:mainfrom
ySnoopyDogy:feat/xml_request_validation
Open

Add XML conversion to bodies, and fix XML data conversion #214
ySnoopyDogy wants to merge 6 commits intopb33f:mainfrom
ySnoopyDogy:feat/xml_request_validation

Conversation

@ySnoopyDogy
Copy link
Contributor

@ySnoopyDogy ySnoopyDogy commented Jan 8, 2026

The new XML validation functions just convert the data to JSON before validating the schema. With that, we can just convert the data on Request and Response validations just by looking at the content type.

Because in XML everything is a string, there can be problems to validating against a schema that includes other primitive types. Now, we convert the strings based on the schema types. If the property is a boolean, we convert to a boolean to pass the validation. If the value is not a valid boolean, we do nothing to let the schema validation to raise an error.

Also, now we are validating if the payload includes prefixes and namespaces defined in the schema (the XMLNS feature of XML).

Added a new option to ValidationOptions: AllowXMLBodyValidation to enable XML validations in request/response bodies (the default is false). Created a function WithXmlBodyValidation to enable the new validator option.

No breaking changes with the new code, as the updated function was internal, and the XML body validation setting is false by default.

@codecov
Copy link

codecov bot commented Jan 8, 2026

Codecov Report

❌ Patch coverage is 99.25651% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 97.73%. Comparing base (26d43b7) to head (25009e6).

Files with missing lines Patch % Lines
schema_validation/validate_xml.go 98.65% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #214      +/-   ##
==========================================
+ Coverage   97.63%   97.73%   +0.10%     
==========================================
  Files          56       57       +1     
  Lines        5323     5527     +204     
==========================================
+ Hits         5197     5402     +205     
+ Misses        126      125       -1     
Flag Coverage Δ
unittests 97.73% <99.25%> (+0.10%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@ySnoopyDogy
Copy link
Contributor Author

Going to implement a smarter conversion, like the one used in Query Parameter validation, looking to the type defined in the schema. This idea can be used to convert the body to JSON with correct defined types, so we can validate application/xml, x-www-form-urlencoded and also multipart/form-data

@ySnoopyDogy ySnoopyDogy marked this pull request as draft January 22, 2026 21:16
@ySnoopyDogy ySnoopyDogy marked this pull request as ready for review February 2, 2026 19:37
@ySnoopyDogy ySnoopyDogy force-pushed the feat/xml_request_validation branch from a457bde to 09fe157 Compare February 2, 2026 19:53
@ySnoopyDogy ySnoopyDogy changed the title Add XML conversion to bodies Add XML conversion to bodies, and fix XML data conversion Feb 2, 2026
@daveshanley
Copy link
Member

A few missing lines of coverage, any chance of a bump?

@ySnoopyDogy ySnoopyDogy force-pushed the feat/xml_request_validation branch from 09fe157 to fb2fe21 Compare February 15, 2026 21:45
@ySnoopyDogy
Copy link
Contributor Author

The coverage lines from validate_xml I think are impossible lines to hit. I tried forcing errors in the xml2json package, but they seem just to not generate errors at all. We create a reader from a string, so we need to force an invalid string that would cause errors in the LIB.

Looking the code from the xj.Convert, the Decode and Encode functions seem to not error. The lib just ignores every error. The most likely error that I found would be an error in the xmlDec.Token from the Decode function, but if it errors, the xml2json lib just breaks out the loop and stop converting the data. I don't think we can force errors for that.

The other line is the validation for the marshal error, but the xml lib only returns valid json. If the lib receives wrong XML data, they just return an empty buffer, that the line 53 ensures to return early. For that reason, the line 59 is practically unreachable, as the xml2json will only generate valid JSON with string values, so we can't even force NaN conversions.

For that, I would say there is no way of covering that lines, the IFs are just safeguards for impossible errors. What could we do in this situation about that two uncovered lines?

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.

2 participants