Skip to content

Commit

Permalink
Fix bug when generating multipart/form-data requests (#73)
Browse files Browse the repository at this point in the history
Form data must be of type string, so stringify objects and arrays
  • Loading branch information
Benjamin Ross authored Aug 31, 2021
1 parent 1feadda commit ee88b65
Show file tree
Hide file tree
Showing 3 changed files with 137 additions and 4 deletions.
12 changes: 8 additions & 4 deletions openapi-to-har.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,10 +147,14 @@ const getPayloads = function (openApi, path, method) {
});
} else if (type === 'multipart/form-data') {
if (sample !== undefined) {
const params = Object.keys(sample).reduce(
(acc, key) => acc.concat([{ name: key, value: sample[key] }]),
[]
);
const params = [];
Object.keys(sample).forEach((key) => {
let value = sample[key];
if (typeof sample[key] !== 'string') {
value = JSON.stringify(sample[key]);
}
params.push({ name: key, value: value });
});
payloads.push({
mimeType: type,
params: params,
Expand Down
99 changes: 99 additions & 0 deletions test/form_data_example.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,80 @@
}
}
}
},
"/pets/{id}/updatetags": {
"patch": {
"description": "Updates tags on a specific pet",
"operationId": "updatePetTags",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"$ref": "#/components/schemas/UpdateTags"
}
}
}
},
"responses": {
"200": {
"description": "pet response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
},
"/pets/{id}/feedingschedule": {
"patch": {
"description": "Updates feeding schedule for specific pet",
"operationId": "updatePetFeedingSchedule",
"requestBody": {
"content": {
"multipart/form-data": {
"schema": {
"$ref": "#/components/schemas/UpdateFeedingSchedule"
}
}
}
},
"responses": {
"200": {
"description": "pet response",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Pet"
}
}
}
},
"default": {
"description": "unexpected error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/Error"
}
}
}
}
}
}
}
},
"components": {
Expand All @@ -70,6 +144,31 @@
"type": "string"
}
}
},
"UpdateTags": {
"properties": {
"pet[tags]": {
"type": "array",
"items": {
"type": "string"
}
}
}
},
"UpdateFeedingSchedule": {
"properties": {
"pet[feedingSchedule]": {
"type": "object",
"properties": {
"time": {
"type": "string"
},
"food": {
"type": "string"
}
}
}
}
}
}
}
Expand Down
30 changes: 30 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,36 @@ test('Generate snippet with multipart/form-data', function (t) {
t.end();
});

test('Generate snippet with multipart/form-data with array', function (t) {
const result = OpenAPISnippets.getEndpointSnippets(
FormDataExampleReferenceAPI,
'/pets/{id}/updatetags',
'patch',
['node_request']
);
const snippet = result.snippets[0].content;
t.true(/boundary=---011000010111000001101001/.test(snippet));
t.true(/formData: {'pet\[tags\]': '\["string"\]'}/.test(snippet));
t.end();
});

test('Generate snippet with multipart/form-data with object', function (t) {
const result = OpenAPISnippets.getEndpointSnippets(
FormDataExampleReferenceAPI,
'/pets/{id}/feedingschedule',
'patch',
['node_request']
);
const snippet = result.snippets[0].content;
t.true(/boundary=---011000010111000001101001/.test(snippet));
t.true(
/formData: {'pet\[feedingSchedule\]': '{"time":"string","food":"string"}'}/.test(
snippet
)
);
t.end();
});

test('Generate snippets with multiple content types', function (t) {
const result = OpenAPISnippets.getEndpointSnippets(
MultipleRequestContentReferenceAPI,
Expand Down

0 comments on commit ee88b65

Please sign in to comment.