support for value prop in example

Add support for value prop in example
This commit is contained in:
Luis Tejeda
2022-04-12 16:51:58 -05:00
committed by Erik Mendoza
parent fa623d32c0
commit ac9df06e84
3 changed files with 117 additions and 3 deletions

View File

@@ -3,7 +3,8 @@
* utils.js contains other util functions * 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'), { getConcreteSchemaUtils } = require('./common/versionUtils.js'),
async = require('async'), async = require('async'),
sdk = require('postman-collection'), sdk = require('postman-collection'),
@@ -1360,6 +1361,28 @@ module.exports = {
return example; 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 * converts one of the eamples or schema in Media Type object to postman data
* @param {*} bodyObj is MediaTypeObject * @param {*} bodyObj is MediaTypeObject
@@ -1410,7 +1433,8 @@ module.exports = {
} }
bodyData = bodyObj.example; bodyData = bodyObj.example;
// return example value if present else example is returned // 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; bodyData = bodyData.value;
} }
} }

View File

@@ -7,7 +7,6 @@ paths:
get: get:
operationId: someRequest operationId: someRequest
parameters: parameters:
- $ref: '#/components/parameters/DeepObjectNestedList'
- $ref: '#/components/parameters/DeepObjectLengthParameter' - $ref: '#/components/parameters/DeepObjectLengthParameter'
responses: responses:
'200': '200':

View File

@@ -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: []