diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index 72b0611..c9e6377 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -406,7 +406,7 @@ module.exports = { let example = _.get(parameter, 'example'), examples = _.values(_.get(parameter, 'examples')); - if (example) { + if (example !== undefined) { _.set(parameter, 'schema.example', example); } else if (examples) { diff --git a/test/data/valid_openapi/issue#10229.json b/test/data/valid_openapi/issue#10229.json new file mode 100644 index 0000000..5dbc306 --- /dev/null +++ b/test/data/valid_openapi/issue#10229.json @@ -0,0 +1,194 @@ +{ + "openapi": "3.1.0", + "info": { + "version": "1.0.0", + "title": "#10229", + "license": { + "name": "MIT" + } + }, + "servers": [ + { + "url": "http://petstore.swagger.io/v1" + } + ], + "paths": { + "/pets": { + "servers": [ + { + "url": "http://petstore.swagger.io:{port}/{basePath}", + "variables": { + "port": { + "enum": [ + "8443", + "443" + ], + "default": "8443" + }, + "basePath": { + "default": "v2" + } + } + } + ], + "get": { + "summary": "List all pets", + "operationId": "listPets", + "tags": [ + "pets" + ], + "parameters": [ + { + "name": "limit", + "in": "query", + "description": "How many items to return at one time (max 100)", + "required": false, + "schema": { + "type": "integer", + "format": "int32" + }, + "example": 0 + }, + { + "name": "otherString", + "in": "query", + "description": "other parameter", + "required": false, + "schema": { + "type": "string" + }, + "example": "" + }, + { + "name": "otherBoolean", + "in": "query", + "description": "other parameter", + "required": false, + "schema": { + "type": "boolean" + }, + "example": false + } + ], + "responses": { + "200": { + "description": "A paged array of pets", + "headers": { + "x-next": { + "description": "A link to the next page of responses", + "schema": { + "type": "string" + } + } + }, + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Pets" + } + } + } + }, + "default": { + "description": "unexpected error", + "content": { + "application/json": { + "schema": { + "$ref": "#/components/schemas/Error" + } + } + } + } + } + }, + "post": { + "summary": "Create a pet", + "operationId": "createPets", + "tags": [ + "pets" + ], + "requestBody": { + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "a": { + "type": "null", + "example": null + } + } + } + } + } + }, + "responses": { + "201": { + "description": "Null response" + }, + "default": { + "description": "unexpected error", + "content": { + "application/json": { + "schema": { + "type": "object", + "properties": { + "a": { + "type": "null", + "example": null + } + } + } + } + } + } + } + } + } + }, + "components": { + "schemas": { + "Pet": { + "type": "object", + "required": [ + "id", + "name" + ], + "properties": { + "id": { + "type": "integer", + "format": "int64" + }, + "name": { + "type": "string" + }, + "tag": { + "type": "string" + } + } + }, + "Pets": { + "type": "array", + "items": { + "$ref": "#/components/schemas/Pet" + } + }, + "Error": { + "type": "object", + "required": [ + "code", + "message" + ], + "properties": { + "code": { + "type": "integer", + "format": "int32" + }, + "message": { + "type": "string" + } + } + } + } + } +} diff --git a/test/unit/base.test.js b/test/unit/base.test.js index e59817c..7317cd3 100644 --- a/test/unit/base.test.js +++ b/test/unit/base.test.js @@ -43,7 +43,8 @@ describe('CONVERT FUNCTION TESTS ', function() { securityTestCases = path.join(__dirname, VALID_OPENAPI_PATH + '/security-test-cases.yaml'), emptySecurityTestCase = path.join(__dirname, VALID_OPENAPI_PATH + '/empty-security-test-case.yaml'), rootUrlServerWithVariables = path.join(__dirname, VALID_OPENAPI_PATH + '/root_url_server_with_variables.json'), - parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml'); + parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml'), + issue10229 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#10229.json'); it('Should add collection level auth with type as `bearer`' + @@ -393,6 +394,25 @@ describe('CONVERT FUNCTION TESTS ', function() { done(); }); }); + it('#GITHUB-10229 should generate correct example is out of the schema and is falsy' + + issue10229, function(done) { + var openapi = fs.readFileSync(issue10229, 'utf8'); + Converter.convert({ type: 'string', data: openapi }, { requestParametersResolution: 'Example' }, + (err, conversionResult) => { + expect(err).to.be.null; + expect(conversionResult.result).to.equal(true); + expect(conversionResult.output.length).to.equal(1); + expect(conversionResult.output[0].type).to.equal('collection'); + expect(conversionResult.output[0].data).to.have.property('info'); + expect(conversionResult.output[0].data).to.have.property('item'); + expect(conversionResult.output[0].data.item[0].item[0].request.url.query[0].value).to.equal('0'); + expect(conversionResult.output[0].data.item[0].item[0].request.url.query[1].value).to.equal(''); + expect(conversionResult.output[0].data.item[0].item[0].request.url.query[2].value).to.equal('false'); + expect(conversionResult.output[0].data.item[0].item[1].request.body.raw).to.equal('{\n "a": null\n}'); + expect(conversionResult.output[0].data.item[0].item[1].response[1].body).to.equal('{\n "a": null\n}'); + done(); + }); + }); describe('[Github #108]- Parameters resolution option', function() { it('Should respect schema faking for root request and example for example request' + examplesInSchemaSpec, function(done) {