diff --git a/openapi-to-har.js b/openapi-to-har.js index 2d9daf2..a2fe96e 100644 --- a/openapi-to-har.js +++ b/openapi-to-har.js @@ -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}}`; @@ -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 ( diff --git a/test/parameter_variations_swagger.json b/test/parameter_variations_swagger.json index ba1cc8a..b9cae1e 100644 --- a/test/parameter_variations_swagger.json +++ b/test/parameter_variations_swagger.json @@ -67,6 +67,16 @@ "type": "integer", "format": "int32" } + }, + { + "name": "noType", + "description": "can be any type", + "in": "query" + }, + { + "name": "typeNotAString", + "type": 35, + "in": "query" } ], "responses": { diff --git a/test/test.js b/test/test.js index cd59409..0a963fb 100644 --- a/test/test.js +++ b/test/test.js @@ -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(); }); @@ -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(); +});