Skip to content
This repository was archived by the owner on Nov 8, 2024. It is now read-only.

Commit 832efb5

Browse files
committed
feat(form): support dataStructure element in serialize
Allow passing the dataStructure element directly, instead of its content
1 parent 757da84 commit 832efb5

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

packages/form-serializer/lib/serializeForm.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ function collectElementByIDs(element) {
2828
}
2929

3030
function serializeForm({ api, mediaType }) {
31+
if (api.element === 'dataStructure') {
32+
return serializeForm({ api: api.content, mediaType });
33+
}
34+
3135
const dataStructures = collectElementByIDs(api);
3236
let values = api.valueOf(undefined, dataStructures);
3337

@@ -51,9 +55,11 @@ function serializeForm({ api, mediaType }) {
5155
content += `--${boundary}--\r\n`;
5256
return content;
5357
}
58+
5459
if (typeof values === 'string') {
5560
values = { undefined: values };
5661
}
62+
5763
return querystring.encode(values);
5864
}
5965

packages/form-serializer/test/serializeForm-test.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,14 @@ describe('#serializeForm', () => {
1818
expect(serializeForm({ api: element, mediaType: 'multipart/form-data' })).to.equal('--BOUNDARY\r\nContent-Disposition: form-data; name="undefined"\r\n\r\nHello world\r\n--BOUNDARY--\r\n');
1919
});
2020

21+
it('can serialize a dataStructure element', () => {
22+
const element = new namespace.elements.DataStructure(
23+
new namespace.elements.String('Hello world')
24+
);
25+
26+
expect(serializeForm({ api: element, mediaType: 'multipart/form-data' })).to.equal('--BOUNDARY\r\nContent-Disposition: form-data; name="undefined"\r\n\r\nHello world\r\n--BOUNDARY--\r\n');
27+
});
28+
2129
it('can serialize an element with references via parent tree', () => {
2230
const element = new namespace.elements.Element();
2331
element.element = 'message';

0 commit comments

Comments
 (0)