From d226580b52e51e1048f4e13f88ee5cf14feb1c20 Mon Sep 17 00:00:00 2001 From: Luis Tejeda <46000487+LuisTejedaS@users.noreply.github.com> Date: Wed, 13 Apr 2022 12:03:50 -0500 Subject: [PATCH] Add test and code improvements Add test and code improvements --- lib/schemaUtils.js | 21 +++++++--- .../valid_openapi/valuePropInExample.yaml | 41 +++++++++++++++++++ test/unit/base.test.js | 18 +++++++- 3 files changed, 74 insertions(+), 6 deletions(-) diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index 4e3e0e8..cb5f756 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -1361,14 +1361,25 @@ module.exports = { return example; }, - useExampleValueAsValue(schema, example, components) { - let isCandidate = true, - schemaProperties = [], + /** + * Identifies if the example.value should be used as value of the body + * This is needed because there are cases that the example object has a property + * called value like: example = { id: 1, value: "someValue" } + * In this case the body value should be the whole example object and not the property called value + * The property value belongs to the example object not the example OAS spec property + * @param {*} schema bodyObject (media type) schema to use + * @param {*} example - Exampel took from the bodyObject + * @param {object} components - components defined in the OAS spec. + * @returns {boolean} Wheter to use the property value of the example as the value of the + * body data + */ + useExampleValuePropAsValue(schema, example, components) { + let schemaProperties = [], schemaObject, schemaDataPath = '', exampleProperties = []; if (!schema) { - return isCandidate; + return false; } if (schema.$ref) { schemaDataPath = formatDataPath(formatSchemaPathFromAJVErrorToConvertToDataPath(schema.$ref)); @@ -1434,7 +1445,7 @@ module.exports = { bodyData = bodyObj.example; // return example value if present else example is returned if (bodyData.hasOwnProperty('value') && - this.useExampleValueAsValue(bodyObj.schema, bodyObj.example, components)) { + this.useExampleValuePropAsValue(bodyObj.schema, bodyObj.example, components)) { bodyData = bodyData.value; } } diff --git a/test/data/valid_openapi/valuePropInExample.yaml b/test/data/valid_openapi/valuePropInExample.yaml index dc1d05a..8c61e1b 100644 --- a/test/data/valid_openapi/valuePropInExample.yaml +++ b/test/data/valid_openapi/valuePropInExample.yaml @@ -46,6 +46,47 @@ paths: application/json: schema: $ref: '#/components/schemas/Error' + /pet: + get: + summary: 'Sample endpoint: Returns details about a particular pet' + operationId: listPet + tags: + - pet + parameters: + - name: id + in: query + description: ID of the pet + required: true + schema: + type: integer + format: int32 + responses: + '200': + description: 'Sample response: Details about a pet by ID' + headers: + x-next: + description: A link to the next page of responses + schema: + type: string + content: + application/json: + schema: + type: object + properties: + id: + type: integer + format: int64 + value: + type: string + example: + id: "5789-6378-6372-6372" + value: QA + default: + description: Unexpected error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' components: schemas: User: diff --git a/test/unit/base.test.js b/test/unit/base.test.js index a6bc0ab..9814135 100644 --- a/test/unit/base.test.js +++ b/test/unit/base.test.js @@ -48,7 +48,8 @@ describe('CONVERT FUNCTION TESTS ', function() { rootUrlServerWithVariables = path.join(__dirname, VALID_OPENAPI_PATH + '/root_url_server_with_variables.json'), parameterExamples = path.join(__dirname, VALID_OPENAPI_PATH + '/parameteres_with_examples.yaml'), issue10229 = path.join(__dirname, VALID_OPENAPI_PATH, '/issue#10229.json'), - deepObjectLengthProperty = path.join(__dirname, VALID_OPENAPI_PATH, '/deepObjectLengthProperty.yaml'); + deepObjectLengthProperty = path.join(__dirname, VALID_OPENAPI_PATH, '/deepObjectLengthProperty.yaml'), + valuePropInExample = path.join(__dirname, VALID_OPENAPI_PATH, '/valuePropInExample.yaml'); it('Should add collection level auth with type as `bearer`' + @@ -1111,6 +1112,21 @@ describe('CONVERT FUNCTION TESTS ', function() { done(); }); }); + + it('[Github #10752]: Should convert value property in example' + + valuePropInExample, function(done) { + var openapi = fs.readFileSync(valuePropInExample, 'utf8'); + Converter.convert({ type: 'string', data: openapi }, + { schemaFaker: true, requestParametersResolution: 'Example' }, (err, conversionResult) => { + expect(err).to.be.null; + expect(conversionResult.result).to.equal(true); + expect(conversionResult.output[0].data.item[0].response[0] + .body).to.include('"value": "QA"'); + expect(conversionResult.output[0].data.item[1].response[0] + .body).to.include('"value": "QA"'); + done(); + }); + }); }); describe('Converting swagger 2.0 files', function() { it('should convert path paramters to postman-compatible paramters', function (done) {