From 1c00d9100d474c86c6a5a4d47b75174008c6717b Mon Sep 17 00:00:00 2001 From: Michael Welch Date: Mon, 25 Jul 2022 17:29:36 -0500 Subject: [PATCH] fix: default missing type to any type Per the spec if no type is given then the type default to the Any Type: https://swagger.io/docs/specification/data-models/data-types/#any This commit removes a check that explicitly sets any undefined type to object, instead leaving it blank. Then where type checking is done it adds support for missing types. One existing test was expecting SOME_OBJECT_TYPE because of a missing type, this test was corrected to look for SOME_ANY_TYPE. An additional test was added to explicitly state that missing types are to be considered Any Type. In addition a fix was made that if the value of a type field is not a string, then the parmater value SOME_ERROR_TYPE is generated. This allows the snippet generator to run without error, but presumably in some workflow the openapi validator will catch the spec error. Fixes #89 --- openapi-to-har.js | 18 ++++++++++++----- test/parameter_variations_swagger.json | 10 +++++++++ test/test.js | 28 +++++++++++++++++++++++++- 3 files changed, 50 insertions(+), 6 deletions(-) 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(); +});