Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions openapi-to-har.js
Original file line number Diff line number Diff line change
Expand Up @@ -437,8 +437,20 @@ const getBaseUrl = function (openApi, path, method) {
* @return {HarParameterObject[]} Array of objects describing the parameters in a given OpenAPI method or path
*/
const getParameterValues = function (openApi, param, location, values) {
var type = (param.type || (param.schema && param.schema.type));

if (typeof type === 'undefined') {
type = "ANY"
} else if (typeof type !== 'string') {
// While this is an error and it'll get by the openapi-snippet
// generator, any openapi validator should flag this error where
// typeof type is not 'string'.
type = "ERROR"
} else {
type = type.toUpperCase()
}
let value =
'SOME_' + (param.type || param.schema.type).toUpperCase() + '_VALUE';
'SOME_' + type + '_VALUE';
if (location === 'path') {
// then default to the original place holder value (e.b. '{id}')
value = `{${param.name}}`;
Expand Down Expand Up @@ -500,10 +512,6 @@ const parseParametersToQuery = function (
/^#/.test(param.schema['$ref'])
) {
param.schema = resolveRef(openApi, param.schema['$ref']);
if (typeof param.schema.type === 'undefined') {
// many schemas don't have an explicit type
param.schema.type = 'object';
}
}
}
if (
Expand Down
10 changes: 10 additions & 0 deletions test/parameter_variations_swagger.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,16 @@
"type": "integer",
"format": "int32"
}
},
{
"name": "noType",
"description": "can be any type",
"in": "query"
},
{
"name": "typeNotAString",
"type": 35,
"in": "query"
}
],
"responses": {
Expand Down
28 changes: 27 additions & 1 deletion test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ test('Parameters that are Schema References Are Dereferenced', function (t) {
['node_request']
);
const snippet = result.snippets[0].content;
t.true(/pet: 'SOME_OBJECT_VALUE'/.test(snippet));
t.true(/pet: 'SOME_ANY_VALUE'/.test(snippet));
t.end();
});

Expand Down Expand Up @@ -1714,3 +1714,29 @@ test('A reference in an examples object is resolved', function (t) {
t.match(snippet, /tags=dog%2Ccat/);
t.end();
});

test('A parameter without an explicit type is assigned the Any Type', function (t) {
const result = OpenAPISnippets.getEndpointSnippets(
ParameterVariationsAPI,
'/pets',
'get',
['shell_curl']
);

const snippet = result.snippets[0].content;
t.match(snippet, /noType=SOME_ANY_VALUE/);
t.end();
});

test('A parameter with a type that is not a string value (like "boolean" or "object") is an error', function (t) {
const result = OpenAPISnippets.getEndpointSnippets(
ParameterVariationsAPI,
'/pets',
'get',
['shell_curl']
);

const snippet = result.snippets[0].content;
t.match(snippet, /typeNotAString=SOME_ERROR_VALUE/);
t.end();
});