Skip to content

Commit

Permalink
fix: test for invalid schema in examples
Browse files Browse the repository at this point in the history
  • Loading branch information
tabrindle committed Feb 11, 2021
1 parent 27ac251 commit 525672c
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
7 changes: 4 additions & 3 deletions lib/response-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ class ResponseGenerator {
return schemaResponse.example;

if(schemaResponse.examples && Object.values(schemaResponse.examples).length) {
if(preferredExampleName && schemaResponse.examples[preferredExampleName])
return schemaResponse.examples[preferredExampleName].value || Object.values(schemaResponse.examples)[0].value;
return Object.values(schemaResponse.examples)[0].value;
if(preferredExampleName && schemaResponse.examples[preferredExampleName] && schemaResponse.examples[preferredExampleName].value)
return schemaResponse.examples[preferredExampleName].value
if(Object.values(schemaResponse.examples)[0].value)
return Object.values(schemaResponse.examples)[0].value;
}

if(schemaResponse.enum && schemaResponse.enum.length)
Expand Down
36 changes: 36 additions & 0 deletions tests/response-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,42 @@ describe('Response Generator', () => {
});
});

it('Should throw if examples is defined but example has no value', () => {

const responseSchema = {
examples: {
first: {
foo: 'bar'
}
}
};

assert.throws(() => ResponseGenerator.generate(responseSchema));
});


it('Should return the first example if examples is defined & preferred example value undefined', () => {

const responseSchema = {
examples: {
first: {
value: {
yes: 'no'
}
},
second: {
hello: 'goodbye'
},
}
};

const response = ResponseGenerator.generate(responseSchema, 'second');

assert.deepStrictEqual(response, {
yes: 'no'
});
});

it('Should return the preferred example if prefer header is set', () => {

const responseSchema = {
Expand Down

0 comments on commit 525672c

Please sign in to comment.