From 04b44f297ae14f4bb6116685db94dadce0105c19 Mon Sep 17 00:00:00 2001 From: Erik Mendoza Date: Mon, 27 Jun 2022 16:07:32 -0500 Subject: [PATCH] Fix #518 integer query params with enum values get default value of NaN Fixes: #518 Resolve parameters correctly when requestParametersResolution is set as 'Example' and the schema contains an enum value --- assets/json-schema-faker.js | 2 +- ...ry_param_with_enum_resolve_as_example.json | 51 +++++++++++++++++++ test/unit/base.test.js | 22 +++++++- 3 files changed, 72 insertions(+), 3 deletions(-) create mode 100644 test/data/valid_openapi/query_param_with_enum_resolve_as_example.json diff --git a/assets/json-schema-faker.js b/assets/json-schema-faker.js index 0e8ee4b..ac37555 100644 --- a/assets/json-schema-faker.js +++ b/assets/json-schema-faker.js @@ -23782,7 +23782,7 @@ function extend() { var min = Math.max(params.minimum || 0, 0); var max = Math.min(params.maximum || Infinity, Infinity); min = handleExclusiveMinimum(schema, min); - max = handleExclusiveMaximum(schema, min); + max = handleExclusiveMaximum(schema, max); // discard out-of-bounds enumerations schema.enum = schema.enum.filter(function (x) { if (x >= min && x <= max) { diff --git a/test/data/valid_openapi/query_param_with_enum_resolve_as_example.json b/test/data/valid_openapi/query_param_with_enum_resolve_as_example.json new file mode 100644 index 0000000..a2d73ab --- /dev/null +++ b/test/data/valid_openapi/query_param_with_enum_resolve_as_example.json @@ -0,0 +1,51 @@ +{ + "openapi": "3.1.0", + "info": { + "version": "1.0.0", + "title": "Swagger Petstore", + "license": { + "name": "MIT" + } + }, + "servers": [ + { + "url": "http://petstore.swagger.io/v1" + } + ], + "paths": { + "/foo": { + "get": { + "parameters": [ + { + "name": "foo", + "in": "query", + "required": true, + "schema": { + "type": "integer", + "enum": [120] + } + } + ], + "responses": {} + } + } + }, + "components": { + "schemas": { + "Pet": { + "type": "object", + "properties": { + "id": { + "type": "integer", + "format": "int64", + "description": "Description of Pet ID" + }, + "name": { + "type": "string", + "description": "Description of Pet name" + } + } + } + } + } +} diff --git a/test/unit/base.test.js b/test/unit/base.test.js index 71e07f8..bab1d8d 100644 --- a/test/unit/base.test.js +++ b/test/unit/base.test.js @@ -50,8 +50,9 @@ describe('CONVERT FUNCTION TESTS ', function() { issue10229 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#10229.json'), deepObjectLengthProperty = path.join(__dirname, VALID_OPENAPI_PATH, '/deepObjectLengthProperty.yaml'), valuePropInExample = path.join(__dirname, VALID_OPENAPI_PATH, '/valuePropInExample.yaml'), - petstoreParamExample = path.join(__dirname, VALID_OPENAPI_PATH, '/petstoreParamExample.yaml'); - + petstoreParamExample = path.join(__dirname, VALID_OPENAPI_PATH, '/petstoreParamExample.yaml'), + queryParamWithEnumResolveAsExample = + path.join(__dirname, VALID_OPENAPI_PATH, '/query_param_with_enum_resolve_as_example.json'); it('Should add collection level auth with type as `bearer`' + securityTestCases, function(done) { @@ -1141,6 +1142,23 @@ describe('CONVERT FUNCTION TESTS ', function() { done(); }); }); + + it('[Github #518]- integer query params with enum values get default value of NaN' + + descriptionInBodyParams, function(done) { + var openapi = fs.readFileSync(queryParamWithEnumResolveAsExample, 'utf8'); + Converter.convert({ + type: 'string', + data: openapi + }, { + schemaFaker: true, + requestParametersResolution: 'Example' + }, (err, conversionResult) => { + let fakedParam = conversionResult.output[0].data.item[0].request.url.query[0].value; + expect(err).to.be.null; + expect(fakedParam).to.be.equal('120'); + done(); + }); + }); }); describe('Converting swagger 2.0 files', function() { it('should convert path paramters to postman-compatible paramters', function (done) {