fix(openapi): wrap root XML arrays

This commit is contained in:
Gregory Schier
2026-08-19 13:32:09 -07:00
parent e2da700aaa
commit 509fb3c63f
2 changed files with 25 additions and 5 deletions
+5 -5
View File
@@ -1139,7 +1139,7 @@ function formatMediaTypeBody(
) {
return typeof example === "string"
? example
: valueToXml(example, schema, importState, "root");
: valueToXml(example, schema, importState, "root", true);
}
return formatBodyText(example);
}
@@ -1149,18 +1149,18 @@ function valueToXml(
schema: unknown,
importState: ImportState,
elementName: string,
isDocumentRoot = false,
): string {
const resolvedSchema = toRecord(importState.resolve(schema));
const schemaXml = toRecord(resolvedSchema.xml);
if (Array.isArray(value)) {
const itemSchema = importState.resolve(resolvedSchema.items);
const shouldWrap = schemaXml.wrapped === true || isDocumentRoot;
const itemName =
stringAt(toRecord(itemSchema).xml, "name") ??
(schemaXml.wrapped === true ? stringAt(schemaXml, "name") ?? elementName : elementName);
(shouldWrap ? stringAt(schemaXml, "name") ?? elementName : elementName);
const items = value.map((item) => valueToXml(item, itemSchema, importState, itemName)).join("");
return schemaXml.wrapped === true
? xmlElement(elementName, schemaXml, items)
: items;
return shouldWrap ? xmlElement(elementName, schemaXml, items) : items;
}
if (isRecord(value)) {
const properties = toRecord(resolvedSchema.properties);
@@ -1071,6 +1071,23 @@ describe("importer-openapi", () => {
responses: {},
},
},
"/values": {
post: {
requestBody: {
content: {
"application/xml": {
schema: {
type: "array",
example: ["one", "two"],
xml: { name: "values", wrapped: false },
items: { type: "string", xml: { name: "value" } },
},
},
},
},
responses: {},
},
},
},
}),
);
@@ -1083,6 +1100,9 @@ describe("importer-openapi", () => {
'<a:alias xmlns:a="urn:aliases">A</a:alias>' +
"</c:catalog>",
});
expect(imported?.resources.httpRequests[1]?.body).toEqual({
text: "<values><value>one</value><value>two</value></values>",
});
});
test("Omits read-only properties from generated request bodies", async () => {