Skip to content

Commit

Permalink
fix: examples object structure for value property, tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tabrindle committed Feb 11, 2021
1 parent 02ecd77 commit 453e6f6
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 22 deletions.
4 changes: 2 additions & 2 deletions lib/response-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ class ResponseGenerator {

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

if(schemaResponse.enum && schemaResponse.enum.length)
Expand Down
20 changes: 12 additions & 8 deletions tests/paths/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -1816,10 +1816,10 @@ describe('Paths', () => {
'application/json': {
examples: {
hello: {
message: 'world'
value: { hello: 'world' }
},
goodbye: {
message: 'yellow brick road'
value: { goodbye: 'yellow brick road' }
}
}
}
Expand All @@ -1834,7 +1834,7 @@ describe('Paths', () => {
statusCode: 200,
headers: undefined,
body: {
message: 'yellow brick road'
goodbye: 'yellow brick road'
}
});
});
Expand All @@ -1852,10 +1852,10 @@ describe('Paths', () => {
'application/json': {
examples: {
hello: {
message: 'world'
value: { hello: 'world' }
},
goodbye: {
message: 'yellow brick road'
value: { goodbye: 'yellow brick road' }
}
}
}
Expand All @@ -1870,7 +1870,7 @@ describe('Paths', () => {
statusCode: 200,
headers: undefined,
body: {
message: 'world'
hello: 'world'
}
});
});
Expand Down Expand Up @@ -1939,10 +1939,14 @@ describe('Paths', () => {
'application/json': {
examples: {
invalid: {
message: 'Unauthorized - token invalid'
value: {
message: 'Unauthorized - token invalid'
}
},
expired: {
message: 'Unauthorized - token expired'
value: {
message: 'Unauthorized - token expired'
}
}
}
}
Expand Down
25 changes: 13 additions & 12 deletions tests/response-generator/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,13 @@ describe('Response Generator', () => {
it('Should return the first example if examples is defined', () => {

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

const response = ResponseGenerator.generate(responseSchema);
Expand Down Expand Up @@ -65,17 +69,14 @@ describe('Response Generator', () => {
}
};

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

assert.deepStrictEqual(response, {
summary: 'An example of a cat',
value: {
name: 'Fluffy',
petType: 'Cat',
color: 'White',
gender: 'male',
breed: 'Persian'
}
name: 'Puma',
petType: 'Dog',
color: 'Black',
gender: 'Female',
breed: 'Mixed'
});
});

Expand Down

0 comments on commit 453e6f6

Please sign in to comment.