Skip to content

feat(xml): validate XML names and namespace prefixes - #12673

Draft
Debojyoti De Majumder (debojyoti10CC) wants to merge 2 commits into
microsoft:mainfrom
debojyoti10CC:feat/xml-validate-names
Draft

Debojyoti De Majumder (debojyoti10CC) wants to merge 2 commits into
microsoft:mainfrom
debojyoti10CC:feat/xml-validate-names

Conversation

@debojyoti10CC

Copy link
Copy Markdown

Fixes #12663

@Xml.name and the prefix given to @Xml.ns accepted any string, so values that can never be valid XML ("1abc", "a b", "a<b", ...) reached emitters.

This adds two diagnostics to @typespec/xml:

  • invalid-xml-name: @Xml.name value must match the XML 1.0 Name production (Unicode ranges per the spec). The encoded name is not set when invalid.
  • invalid-xml-prefix: the @Xml.ns prefix must be a valid NCName (no colon).

Not covered: a raw @encodedName("application/xml", ...) goes through the compiler decorator, so it is not validated here. Happy to discuss whether that belongs in the compiler.

Testing: added cases in packages/xml/test/decorators.test.ts (valid names incl. Unicode, invalid start chars / punctuation / whitespace, invalid and valid prefixes). pnpm vitest run in packages/xml: 42 passed; oxlint and prettier clean. Added a chronus change file.

AI drafted the implementation and tests; I reviewed both and ran the package tests.

Report invalid-xml-name for @Xml.name and invalid-xml-prefix for @Xml.ns prefixes that are not valid XML Name/NCName.

Signed-off-by: debojyoti10CC <[email protected]>
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

The enum-derived prefix validation branch lacks regression coverage for the diagnostic and unset namespace state, and the changelog incorrectly labels the bug fix as a feature.

2 open issues
What changed in this PR

This PR addresses #12663 by validating XML names and namespace prefixes in @typespec/xml, preventing invalid decorator values from reaching emitters. Direct @encodedName usage remains outside its scope.

Changes:

  • Adds Unicode-aware XML Name and NCName validation.
  • Reports diagnostics and skips storing invalid names or prefixes.
  • Adds decorator validation tests and a changelog entry.
File Description
packages/​xml/​test/​decorators.test.ts Adds valid and invalid name and prefix tests.
packages/​xml/​src/​xml-name.ts Implements XML Name and NCName validators.
packages/​xml/​src/​lib.ts Defines the two validation diagnostics.
packages/​xml/​src/​decorators.ts Validates values before storing XML metadata.
.chronus/​changes/​xml-validate-names-2026-11-29-0-0-0.md Documents the new validation behavior.

Review effort: Balanced

Comment thread packages/xml/test/decorators.test.ts Resolved
Copilot AI balanced review requested due to automatic review settings November 30, 2026 02:27

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟢 Approval recommended

No active review issues were identified for the XML name and namespace-prefix validation changes.

No open issues 🎉

2 resolved since last review

Review effort: Balanced

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Fixing it this way I don't think is the correct approach. @name is just a wrapper aronnd @encodedName where this can still reproduce. This also is possible to do as the original name so I think this needs a bit more design on what should happen.

@debojyoti10CC

Copy link
Copy Markdown
Author

Thanks, that makes sense. Checking only in @Xml.name misses @encodedName("application/xml", ...) and types whose own name isn't a valid XML name. Would you rather validate the resolved XML name in one place, for example a @typespec/xml linter rule that checks every model and property that ends up in XML, or in the emitter? Happy to rework it in whichever direction you prefer, and I'll move this to draft until then.

This branch has not been deployed

No deployments
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.

[@typespec/xml] Validate XML element, attribute, and prefix names against W3C XML Name grammar

3 participants