Skip to content

Commit 8313f25

Browse files
authored
Merge pull request #227 from postmanlabs/feature/improve-example-option
Improved example value for `requestParameterResolution` and `exampleParameterResolution` options.
2 parents 663e420 + bc432fe commit 8313f25

File tree

4 files changed

+16
-10
lines changed

4 files changed

+16
-10
lines changed

lib/deref.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports = {
5656
* @param {object} schemaResolutionCache stores already resolved references
5757
* @param {*} resolveFor - resolve refs for validation/conversion (value to be one of VALIDATION/CONVERSION)
5858
* @param {string} resolveTo The desired JSON-generation mechanism (schema: prefer using the JSONschema to
59-
generate a fake object, example: use specified examples as-is). Default: schema
59+
generate a fake object, example: use specified examples as-is). Default: schema
6060
* @param {*} stack counter which keeps a tab on nested schemas
6161
* @param {*} seenRef References that are repeated. Used to identify circular references.
6262
* @returns {*} schema - schema that adheres to all individual schemas in schemaArr
@@ -234,8 +234,10 @@ module.exports = {
234234

235235
schema.type = 'string';
236236

237-
// Don't override default value to schema for validation
238-
resolveFor !== 'VALIDATION' && (schema.default = '<object>');
237+
// Override deefault value to appropriate type representation for parameter resolution to schema
238+
if (resolveFor === 'CONVERSION' && resolveTo === 'schema') {
239+
schema.default = '<object>';
240+
}
239241
}
240242
else if (schema.type === 'array' && schema.items) {
241243
/*

lib/schemaUtils.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -773,12 +773,13 @@ module.exports = {
773773
else {
774774
_.forEach(commonPathVars, (variable) => {
775775
let description = this.getParameterDescription(variable);
776+
776777
variables.push({
777778
key: variable.name,
778779
// we only fake the schema for param-level pathVars
779780
value: options.schemaFaker ?
780-
safeSchemaFaker(variable.schema || {}, 'schema', PROCESSING_TYPE.CONVERSION, PARAMETER_SOURCE.REQUEST,
781-
components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache) : '',
781+
safeSchemaFaker(variable.schema || {}, options.requestParametersResolution, PROCESSING_TYPE.CONVERSION,
782+
PARAMETER_SOURCE.REQUEST, components, SCHEMA_FORMATS.DEFAULT, options.indentCharacter, schemaCache) : '',
782783
description: description
783784
});
784785
});

test/unit/base.test.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,10 +318,13 @@ describe('CONVERT FUNCTION TESTS ', function() {
318318
{ schemaFaker: true, requestParametersResolution: 'example', exampleParametersResolution: 'example' },
319319
(err, conversionResult) => {
320320
let rootRequestBody = JSON.parse(conversionResult.output[0].data.item[0].request.body.raw),
321-
exampleRequestBody = JSON.parse(conversionResult.output[0].data.item[0].response[0]
322-
.originalRequest.body.raw);
321+
exampleRequestBody = JSON.parse(conversionResult.output[0].data.item[0]
322+
.response[0].originalRequest.body.raw);
323+
324+
expect(rootRequestBody).to.have.all.keys(['a', 'b']);
323325
expect(rootRequestBody.a).to.be.a('string');
324326
expect(rootRequestBody.b).to.be.a('string');
327+
expect(exampleRequestBody).to.have.all.keys(['a', 'b']);
325328
expect(exampleRequestBody.a).to.be.a('string');
326329
expect(exampleRequestBody.b).to.be.a('string');
327330
done();

test/unit/util.test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
163163
parameterSource = 'REQUEST',
164164
resolveTo = 'schema',
165165
resolveFor = 'CONVERSION',
166-
resolvedSchema = deref.resolveRefs(schema, parameterSource, { components }, {}, resolveFor),
166+
resolvedSchema = deref.resolveRefs(schema, parameterSource, { components }, {}, resolveFor, resolveTo),
167167
schemaCache = {
168168
schemaFakerCache: {},
169169
schemaResolutionCache: {}
@@ -205,7 +205,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
205205
schemaResolutionCache: {}
206206
},
207207
resolvedSchema = deref.resolveRefs(schema, parameterSource, { components }, schemaCache.schemaResolutionCache,
208-
resolveFor),
208+
resolveFor, resolveTo),
209209
key = hash('resolveToExample ' + JSON.stringify(resolvedSchema)),
210210
fakedSchema = SchemaUtils.safeSchemaFaker(schema, resolveTo, resolveFor, parameterSource,
211211
{ components }, 'default', ' ', schemaCache);
@@ -640,7 +640,7 @@ describe('SCHEMA UTILITY FUNCTION TESTS ', function () {
640640
expect(retVal).to.be.an('array');
641641
expect(retVal[0].key).to.equal('varName');
642642
expect(retVal[0].description).to.equal('varDesc');
643-
expect(retVal[0].value).to.equal('<integer>');
643+
expect(retVal[0].value).to.be.a('number');
644644
});
645645
});
646646

0 commit comments

Comments
 (0)