Describe the bug
validateXmlBodyEquals in @typespec/spec-api@0.1.0-alpha.15 rejects semantically equivalent XML when the expected body has an XML declaration and the actual request body does not.
The validator always prepends <?xml version='1.0' encoding='UTF-8'?> to a plain expected string. Since the migration from xml2js to fast-xml-parser in #10239, XMLParser preserves that declaration as a ?xml node by default. The parsed expected value therefore differs from an otherwise identical actual request without a declaration.
This causes 25 XML request-body Spector tests to fail in Azure/azure-sdk-for-net#61046. @typespec/spec-api@0.1.0-alpha.14 does not have this regression because xml2js ignored the declaration.
Reproduction
import { validateXmlBodyEquals } from "@typespec/spec-api";
validateXmlBodyEquals(
{ rawBody: "<Root><a>1</a></Root>" },
"<Root><a>1</a></Root>",
);
With @typespec/spec-api@0.1.0-alpha.15, this throws:
ValidationError: Body provided doesn't match expected body
The parsed values differ as follows:
const parser = new XMLParser({ parseTagValue: false });
parser.parse("<Root><a>1</a></Root>");
// { Root: { a: "1" } }
parser.parse('<?xml version="1.0" encoding="UTF-8"?><Root><a>1</a></Root>');
// { "?xml": "", Root: { a: "1" } }
Expected behavior
XML declarations should not affect semantic request-body equality. The reproduction should not throw.
Using the following parser option restores the prior behavior:
new XMLParser({ parseTagValue: false, ignoreDeclaration: true });
Suggested regression coverage
The current test covers a declaration in the actual request and no declaration in the expected string. Please also cover the inverse path that occurs in Spector: the actual request omits the declaration while validateXmlBodyEquals adds one to the expected value.
Related
Describe the bug
validateXmlBodyEqualsin@typespec/spec-api@0.1.0-alpha.15rejects semantically equivalent XML when the expected body has an XML declaration and the actual request body does not.The validator always prepends
<?xml version='1.0' encoding='UTF-8'?>to a plain expected string. Since the migration fromxml2jstofast-xml-parserin #10239,XMLParserpreserves that declaration as a?xmlnode by default. The parsed expected value therefore differs from an otherwise identical actual request without a declaration.This causes 25 XML request-body Spector tests to fail in Azure/azure-sdk-for-net#61046.
@typespec/spec-api@0.1.0-alpha.14does not have this regression becausexml2jsignored the declaration.Reproduction
With
@typespec/spec-api@0.1.0-alpha.15, this throws:The parsed values differ as follows:
Expected behavior
XML declarations should not affect semantic request-body equality. The reproduction should not throw.
Using the following parser option restores the prior behavior:
Suggested regression coverage
The current test covers a declaration in the actual request and no declaration in the expected string. Please also cover the inverse path that occurs in Spector: the actual request omits the declaration while
validateXmlBodyEqualsadds one to the expected value.Related

by default #10350