Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[core-xml] Update fxp to v4 #19898

Merged
merged 3 commits into from
Jan 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions common/config/rush/pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 3 additions & 7 deletions sdk/core/core-xml/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
# Release History

## 1.1.1 (Unreleased)

### Features Added

### Breaking Changes

### Bugs Fixed
## 1.2.0 (Unreleased)

### Other Changes

- Upgrade to `fast-xml-parser` to v4 [PR# 19898](https://github.com/Azure/azure-sdk-for-js/pull/19898)

## 1.1.0 (2022-01-06)

### Other Changes
Expand Down
4 changes: 2 additions & 2 deletions sdk/core/core-xml/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@azure/core-xml",
"version": "1.1.1",
"version": "1.2.0",
"description": "Core library for interacting with XML payloads",
"sdk-type": "client",
"main": "dist/index.js",
Expand Down Expand Up @@ -66,7 +66,7 @@
"prettier": "@azure/eslint-plugin-azure-sdk/prettier.json",
"dependencies": {
"tslib": "^2.2.0",
"fast-xml-parser": "^3.20.0"
"fast-xml-parser": "^4.0.1"
Copy link
Member

Choose a reason for hiding this comment

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

should we also bump the minor version of core-xml?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah, good point. I'll bump it

},
"devDependencies": {
"@azure/dev-tool": "^1.0.0",
Expand Down
18 changes: 10 additions & 8 deletions sdk/core/core-xml/src/xml.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { j2xParser, parse, validate } from "fast-xml-parser";
import { XMLBuilder, XMLValidator, XMLParser } from "fast-xml-parser";
import { XML_ATTRKEY, XML_CHARKEY, XmlOptions } from "./xml.common";

function getCommonOptions(options: XmlOptions) {
return {
attrNodeName: XML_ATTRKEY,
attributesGroupName: XML_ATTRKEY,
textNodeName: options.xmlCharKey ?? XML_CHARKEY,
ignoreAttributes: false,
suppressBooleanAttributes: false,
};
}

Expand All @@ -17,7 +18,7 @@ function getSerializerOptions(options: XmlOptions = {}) {
...getCommonOptions(options),
attributeNamePrefix: "@_",
format: true,
supressEmptyNode: true,
suppressEmptyNode: true,
indentBy: "",
rootNodeName: options.rootName ?? "root",
};
Expand All @@ -27,7 +28,7 @@ function getParserOptions(options: XmlOptions = {}) {
return {
...getCommonOptions(options),
parseAttributeValue: false,
parseNodeValue: false,
parseTagValue: false,
attributeNamePrefix: "",
};
}
Expand All @@ -39,11 +40,11 @@ function getParserOptions(options: XmlOptions = {}) {
*/
export function stringifyXML(obj: unknown, opts: XmlOptions = {}): string {
const parserOptions = getSerializerOptions(opts);
const j2x = new j2xParser(parserOptions);
const j2x = new XMLBuilder(parserOptions);

const node = { [parserOptions.rootNodeName]: obj };

const xmlData: string = j2x.parse(node);
const xmlData: string = j2x.build(node);
return `<?xml version="1.0" encoding="UTF-8" standalone="yes"?>${xmlData}`.replace(/\n/g, "");
}

Expand All @@ -58,13 +59,14 @@ export async function parseXML(str: string, opts: XmlOptions = {}): Promise<any>
throw new Error("Document is empty");
}

const validation = validate(str);
const validation = XMLValidator.validate(str);

if (validation !== true) {
throw validation;
}

const parsedXml = parse(unescapeHTML(str), getParserOptions(opts));
const parser = new XMLParser(getParserOptions(opts));
const parsedXml = parser.parse(unescapeHTML(str));

if (!opts.includeRoot) {
for (const key of Object.keys(parsedXml)) {
Expand Down