mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-08-25 12:54:09 +02:00
fix(openapi): wrap root XML arrays
This commit is contained in:
@@ -1139,7 +1139,7 @@ function formatMediaTypeBody(
|
|||||||
) {
|
) {
|
||||||
return typeof example === "string"
|
return typeof example === "string"
|
||||||
? example
|
? example
|
||||||
: valueToXml(example, schema, importState, "root");
|
: valueToXml(example, schema, importState, "root", true);
|
||||||
}
|
}
|
||||||
return formatBodyText(example);
|
return formatBodyText(example);
|
||||||
}
|
}
|
||||||
@@ -1149,18 +1149,18 @@ function valueToXml(
|
|||||||
schema: unknown,
|
schema: unknown,
|
||||||
importState: ImportState,
|
importState: ImportState,
|
||||||
elementName: string,
|
elementName: string,
|
||||||
|
isDocumentRoot = false,
|
||||||
): string {
|
): string {
|
||||||
const resolvedSchema = toRecord(importState.resolve(schema));
|
const resolvedSchema = toRecord(importState.resolve(schema));
|
||||||
const schemaXml = toRecord(resolvedSchema.xml);
|
const schemaXml = toRecord(resolvedSchema.xml);
|
||||||
if (Array.isArray(value)) {
|
if (Array.isArray(value)) {
|
||||||
const itemSchema = importState.resolve(resolvedSchema.items);
|
const itemSchema = importState.resolve(resolvedSchema.items);
|
||||||
|
const shouldWrap = schemaXml.wrapped === true || isDocumentRoot;
|
||||||
const itemName =
|
const itemName =
|
||||||
stringAt(toRecord(itemSchema).xml, "name") ??
|
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("");
|
const items = value.map((item) => valueToXml(item, itemSchema, importState, itemName)).join("");
|
||||||
return schemaXml.wrapped === true
|
return shouldWrap ? xmlElement(elementName, schemaXml, items) : items;
|
||||||
? xmlElement(elementName, schemaXml, items)
|
|
||||||
: items;
|
|
||||||
}
|
}
|
||||||
if (isRecord(value)) {
|
if (isRecord(value)) {
|
||||||
const properties = toRecord(resolvedSchema.properties);
|
const properties = toRecord(resolvedSchema.properties);
|
||||||
|
|||||||
@@ -1071,6 +1071,23 @@ describe("importer-openapi", () => {
|
|||||||
responses: {},
|
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>' +
|
'<a:alias xmlns:a="urn:aliases">A</a:alias>' +
|
||||||
"</c:catalog>",
|
"</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 () => {
|
test("Omits read-only properties from generated request bodies", async () => {
|
||||||
|
|||||||
Reference in New Issue
Block a user