Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed Hapi arrays to work as expected with swaggered-ui following swagger documentation #42

Merged
merged 1 commit into from
Aug 15, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 16 additions & 16 deletions lib/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ generator.newModel = function (schema, definitions) {
model.required.push(key)
}

var pick = _.has(value, '$ref') ? ['$ref'] : ['type', 'format', 'items', 'default', 'description', '$ref', 'enum', 'minimum', 'maximum']
var pick = _.has(value, '$ref') ? ['$ref'] : ['type', 'format', 'items', 'default', 'description', '$ref', 'enum', 'minimum', 'maximum', 'collectionFormat']
memo[key] = _.pick(value, pick)
return memo
}, model.properties)
Expand Down Expand Up @@ -69,28 +69,28 @@ generator.newArray = function (schema, definitions, arrayModel) {
if (firstInclusionTypeModel.$ref) {
arrayModel.items = _.pick(firstInclusionTypeModel, ['$ref'])
} else {
arrayModel.items = _.pick(firstInclusionTypeModel, ['type', 'format', 'items'])
arrayModel.items = _.pick(firstInclusionTypeModel, ['type', 'format', 'items', 'collectionFormat'])
}
} else {
// array item schema missing -> go for string as default
arrayModel.items = { type: 'string' }
arrayModel.items = {type: 'string'}
}

return arrayModel

/*
// May extract all arrays and use a reference? Sometimes inline required?
var required = arrayModel.required
delete arrayModel.required
var name = utils.generateNameWithFallback(schema, definitions, arrayModel)
definitions[name] = arrayModel

return {
required: required,
description: arrayModel.description,
$ref: '#/definitions/' + name
}
*/
/*
// May extract all arrays and use a reference? Sometimes inline required?
var required = arrayModel.required
delete arrayModel.required
var name = utils.generateNameWithFallback(schema, definitions, arrayModel)
definitions[name] = arrayModel

return {
required: required,
description: arrayModel.description,
$ref: '#/definitions/' + name
}
*/
}

generator.extractAsDefinition = function (schema, definitions, definition) {
Expand Down
4 changes: 3 additions & 1 deletion lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ schemas.Property = Joi.object().keys({
default: Joi.any().optional(),
enum: Joi.any().optional(),
maximum: Joi.number().optional(),
minimum: Joi.number().optional()
minimum: Joi.number().optional(),
collectionFormat: Joi.string().optional()
// schema: schemas.Reference.optional()
}).meta({
className: 'Property'
Expand Down Expand Up @@ -157,6 +158,7 @@ schemas.Definition = Joi.object({
default: Joi.any().optional(),
items: Joi.alternatives().try(schemas.SimpleReference, schemas.Item).optional(),
format: dataFormat.optional(),
collectionFormat: Joi.string().optional(),

// For schema integration tests:
allOf: Joi.array().items(schemas.Reference).optional()
Expand Down
3 changes: 2 additions & 1 deletion lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ utils.parseBaseModelAttributes = function (schema) {
var defaultValue = Hoek.reach(schema, '_flags.default')
var format = utils.getFormat(schema)
var enumValues = Hoek.reach(schema, '_flags.allowOnly') === true ? Hoek.reach(schema, '_valids._set') : undefined
enumValues = _.isArray(enumValues) && enumValues.length > 0 ? enumValues : undefined
var collectionFormat = utils.getMeta(schema, 'collectionFormat')

var baseModel = {
required: required
Expand All @@ -300,6 +300,7 @@ utils.parseBaseModelAttributes = function (schema) {
utils.setNotEmpty(baseModel, 'default', defaultValue)
utils.setNotEmpty(baseModel, 'format', format)
utils.setNotEmpty(baseModel, 'enum', enumValues)
utils.setNotEmpty(baseModel, 'collectionFormat', collectionFormat)
utils.setNotEmpty(baseModel, 'minimum', utils.findSchemaTest(schema, 'min'))
utils.setNotEmpty(baseModel, 'maximum', utils.findSchemaTest(schema, 'max'))

Expand Down
28 changes: 28 additions & 0 deletions test/generator-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,34 @@ describe('definitions', function () {
done()
})

it('collectionFormat', function (done) {
var schema = Joi.object({
test: Joi.array().items(Joi.object()).meta({collectionFormat: 'multi'})
}).meta({
className: 'Pet1'
})

var result = {
'EmptyModel': {
'properties': {}
},
'Pet1': {
'properties': {
'test': {
'type': 'array',
'items': {
'$ref': '#/definitions/EmptyModel'
},
'collectionFormat': 'multi'
}
}
}
}

helper.testDefinition(schema, result)
done()
})

it('multiple properties', function (done) {
var schema = Joi.object({
booleanValue: Joi.boolean(),
Expand Down