• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

pmcelhaney / counterfact / 8697996524

16 Apr 2024 12:29AM CUT coverage: 86.607% (+0.09%) from 86.517%
8697996524

Pull #852

github

web-flow
Merge branch 'main' into refactor-path-coder
Pull Request #852: Support OpenAPI reusable responses in global components object (handle $ref properly everywhere)

891 of 984 branches covered (90.55%)

Branch coverage included in aggregate %.

101 of 103 new or added lines in 8 files covered. (98.06%)

2 existing lines in 1 file now uncovered.

2879 of 3369 relevant lines covered (85.46%)

43.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

70.43
/src/server/json-to-xml.ts
1
interface XmlHints {
2✔
2
  attribute?: boolean;
2✔
3
  name?: string;
2✔
4
  namespace?: string;
2✔
5
  prefix?: string;
2✔
6
  wrapped?: boolean;
2✔
7
}
2✔
8

2✔
9
interface Schema {
2✔
10
  [key: string]: unknown;
2✔
11
  items?: Schema;
2✔
12
  properties?: {
2✔
13
    [key: string]: Schema;
2✔
14
  };
2✔
15
  xml?: XmlHints;
2✔
16
}
2✔
17

2✔
18
function xmlEscape(xmlString: string): string {
2✔
19
  // eslint-disable-next-line unicorn/prefer-string-replace-all
2✔
20
  return xmlString.replace(/["&'<>]/gu, (character: string) => {
2✔
21
    switch (character) {
×
22
      case "<": {
×
23
        return "&lt;";
×
24
      }
×
25
      case ">": {
×
26
        return "&gt;";
×
27
      }
×
28
      case "&": {
×
29
        return "&amp;";
×
30
      }
×
31
      case "'": {
×
32
        return "&apos;";
×
33
      }
×
34
      case '"': {
×
35
        return "&quot;";
×
36
      }
×
37
      default: {
×
38
        return character;
×
39
      }
×
40
    }
×
41
  });
×
42
}
2✔
43

2✔
44
function objectToXml(
8✔
45
  json: object,
8✔
46
  schema: Schema | undefined,
8✔
47
  name: string,
8✔
48
): string {
8✔
49
  const xml: string[] = [];
8✔
50

8✔
51
  const attributes: string[] = [];
8✔
52

8✔
53
  Object.entries(json).forEach(([key, value]) => {
8✔
54
    const properties = schema?.properties?.[key];
16✔
55
    // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
16✔
56
    if (properties?.attribute) {
16✔
57
      attributes.push(` ${key}="${xmlEscape(String(value))}"`);
2✔
58
    } else {
16✔
59
      // eslint-disable-next-line @typescript-eslint/no-use-before-define
14✔
60
      xml.push(jsonToXml(value, properties, key));
14✔
61
    }
14✔
62
  });
16✔
63

8✔
64
  return `<${name}${attributes.join("")}>${String(xml.join(""))}</${name}>`;
8✔
65
}
8✔
66

2✔
67
export function jsonToXml(
2✔
68
  json: unknown,
22✔
69
  schema: Schema | undefined,
22✔
70
  keyName = "root",
22✔
71
): string {
22✔
72
  const name = schema?.xml?.name ?? keyName;
22✔
73

22✔
74
  if (Array.isArray(json)) {
22!
75
    const items = json
×
76
      .map((item) => jsonToXml(item, schema?.items, name))
×
77
      .join("");
×
78

×
79
    // eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
×
80
    if (schema?.xml?.wrapped) {
×
81
      return `<${name}>${items}</${name}>`;
×
82
    }
×
83

×
84
    return items;
×
85
  }
×
86

22✔
87
  if (typeof json === "object" && json !== null) {
22✔
88
    return objectToXml(json, schema, name);
8✔
89
  }
8✔
90

14✔
91
  return `<${name}>${String(json)}</${name}>`;
14✔
92
}
14✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc