Skip to content

[spec-api] XML validation treats the declaration as body content #11271

Description

@live1206

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingspectorIssues related to spector and the spec sets

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions