diff --git a/lib/schemaUtils.js b/lib/schemaUtils.js index b6bce77..4e3e0e8 100644 --- a/lib/schemaUtils.js +++ b/lib/schemaUtils.js @@ -3,7 +3,8 @@ * utils.js contains other util functions */ -const { formatDataPath, checkIsCorrectType, isKnownType } = require('./common/schemaUtilsCommon.js'), +const { formatDataPath, checkIsCorrectType, isKnownType, + formatSchemaPathFromAJVErrorToConvertToDataPath } = require('./common/schemaUtilsCommon.js'), { getConcreteSchemaUtils } = require('./common/versionUtils.js'), async = require('async'), sdk = require('postman-collection'), @@ -1360,6 +1361,28 @@ module.exports = { return example; }, + useExampleValueAsValue(schema, example, components) { + let isCandidate = true, + schemaProperties = [], + schemaObject, + schemaDataPath = '', + exampleProperties = []; + if (!schema) { + return isCandidate; + } + if (schema.$ref) { + schemaDataPath = formatDataPath(formatSchemaPathFromAJVErrorToConvertToDataPath(schema.$ref)); + schemaObject = _.get(components, schemaDataPath); + } + else { + schemaObject = schema; + } + + schemaProperties = Object.keys(schemaObject.properties).sort(); + exampleProperties = Object.keys(example.value).sort(); + return JSON.stringify(schemaProperties) === JSON.stringify(exampleProperties); + }, + /** * converts one of the eamples or schema in Media Type object to postman data * @param {*} bodyObj is MediaTypeObject @@ -1410,7 +1433,8 @@ module.exports = { } bodyData = bodyObj.example; // return example value if present else example is returned - if (bodyData.hasOwnProperty('value')) { + if (bodyData.hasOwnProperty('value') && + this.useExampleValueAsValue(bodyObj.schema, bodyObj.example, components)) { bodyData = bodyData.value; } } diff --git a/test/data/valid_openapi/deepObjectLengthProperty.yaml b/test/data/valid_openapi/deepObjectLengthProperty.yaml index dd33402..d3d682c 100644 --- a/test/data/valid_openapi/deepObjectLengthProperty.yaml +++ b/test/data/valid_openapi/deepObjectLengthProperty.yaml @@ -7,7 +7,6 @@ paths: get: operationId: someRequest parameters: - - $ref: '#/components/parameters/DeepObjectNestedList' - $ref: '#/components/parameters/DeepObjectLengthParameter' responses: '200': diff --git a/test/data/valid_openapi/valuePropInExample.yaml b/test/data/valid_openapi/valuePropInExample.yaml new file mode 100644 index 0000000..dc1d05a --- /dev/null +++ b/test/data/valid_openapi/valuePropInExample.yaml @@ -0,0 +1,91 @@ +openapi: 3.0.0 +info: + version: '1.1.1' + title: 'ExampleYaml' + license: + name: MIT +servers: + - url: 'localhost:3000' +paths: + /user: + get: + summary: 'Sample endpoint: Returns details about a particular user' + operationId: listUser + tags: + - user + parameters: + - name: id + in: query + description: ID of the user + required: true + schema: + type: integer + format: int32 + responses: + '200': + description: 'Sample response: Details about a user by ID' + headers: + x-next: + description: A link to the next page of responses + schema: + type: string + content: + application/json: + schema: + $ref: '#/components/schemas/User' + example: + id: "5789-6378-6372-6372" + name: Vani + tag: true + value: QA + lastmodifieddate: "2022-03-30T07:01:46" + lastmodifiedBy: VM + default: + description: Unexpected error + content: + application/json: + schema: + $ref: '#/components/schemas/Error' +components: + schemas: + User: + type: object + required: + - id + - name + - tag + - value + - lastmodifieddate + - lastmodifiedBy + properties: + id: + type: integer + format: int64 + name: + type: string + tag: + type: string + value: + type: string + lastmodifieddate: + type: integer + format: int64 + lastmodifiedBy: + type: string + Error: + type: object + required: + - code + - message + properties: + code: + type: integer + format: int32 + message: + type: string + securitySchemes: + BasicAuth: + type: http + scheme: basic +security: + - BasicAuth: []