Skip to content

Commit

Permalink
fix: fixed example parsing for joi version > 14
Browse files Browse the repository at this point in the history
Examples are now objects, as per relase 14.0.0. References:

- hapijs/joi#1615
- hapi-swagger/hapi-swagger#540
- hapi-swagger/hapi-swagger#546
  • Loading branch information
lucasconstantino committed Feb 6, 2019
1 parent 9c9dbef commit 0c864a1
Show file tree
Hide file tree
Showing 3 changed files with 2,355 additions and 9 deletions.
8 changes: 6 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ module.exports = exports = function parse (schema, existingComponents) {

if (schema._examples.length) {
if (schema._examples.length === 1) {
swagger.example = schema._examples[0];
swagger.example = extractExampleValue(schema._examples[0]);
} else {
swagger.examples = schema._examples;
swagger.examples = schema._examples.map(extractExampleValue);
}
}

Expand Down Expand Up @@ -332,6 +332,10 @@ function refDef (type, name) {
return { $ref: '#/components/' + type + '/' + name };
}

function extractExampleValue (example) {
return joi.version < '14' ? example : example.value;
}

// var inspectU = require('util').inspect;
// function inspect (value) {
// console.error(inspectU(value, { colors: true, depth: 10 }));
Expand Down
18 changes: 11 additions & 7 deletions tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -317,13 +317,17 @@ suite('swagger converts', (s) => {

simpleTest(
joi.string().example('sel').example('wyn'),
{
examples: [
'sel',
'wyn',
],
type: 'string',
}
joi.version < '14'
? {
examples: [
'sel',
'wyn',
],
type: 'string',
} : {
example: 'wyn',
type: 'string',
}
);

simpleTest(
Expand Down
Loading

0 comments on commit 0c864a1

Please sign in to comment.