Skip to content

Commit

Permalink
fix an error if a include function does not return a transformer
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralph Huwiler committed Feb 10, 2018
1 parent f084233 commit cf4d3f8
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 4 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ All notable changes to this project will be documented in this file.
## [Unreleased]


## [0.0.7] - 2018-02-10

### Fixed
- If an include function does not return an instance of `ResourceAbstrace`, the returned value is used without transformation.


## [0.0.6] - 2018-02-10

This release adds optional includes that can be requested on a per-transform basis.
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "adonis-bumblebee",
"version": "0.0.6",
"version": "0.0.7",
"description": "Api Transformer Provider for AdonisJs Framework",
"main": "index.js",
"scripts": {
Expand Down
4 changes: 3 additions & 1 deletion src/Bumblebee/Resources/index.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const Collection = require('./Collection')
const Item = require('./Item')
const Null = require('./Null')
const ResourceAbstract = require('./ResourceAbstract')

module.exports = {
Collection,
Item,
Null
Null,
ResourceAbstract
}
6 changes: 5 additions & 1 deletion src/Bumblebee/TransformerAbstract.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ class TransformerAbstract {
for (let include of this.figureOutWhichIncludes(parentScope)) {
let resource = await this.callIncludeFunction(include, data)

includeData[include] = await this.createChildScopeFor(parentScope, resource, include).toArray()
if (resource instanceof Resources.ResourceAbstract) {
includeData[include] = await this.createChildScopeFor(parentScope, resource, include).toArray()
} else {
includeData[include] = resource
}
}

return includeData
Expand Down
19 changes: 18 additions & 1 deletion test/includes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Book2Transformer extends TransformerAbstract {
availableInclude () {
return [
'author',
'characters'
'characters',
'school'
]
}

Expand All @@ -35,6 +36,9 @@ class Book2Transformer extends TransformerAbstract {
includeCharacters (book) {
return this.collection(book.characters, Book2CharacterTransformer)
}
includeSchool (book) {
return 'Hogwarts'
}
}

class Book2CharacterTransformer extends TransformerAbstract {
Expand Down Expand Up @@ -142,4 +146,17 @@ test.group('Includes can be an array or a string', () => {

assert.deepEqual(transformed, {title: 'Harry Potter and the Deathly Hallows'})
})

test('an include function can return a object to be merged', async (assert) => {
let transformed = await Bumblebee.create()
.include(['school'])
.item(data)
.transformWith(Book2Transformer)
.toArray()

assert.deepEqual(transformed, {
title: 'Harry Potter and the Deathly Hallows',
school: 'Hogwarts'
})
})
})

0 comments on commit cf4d3f8

Please sign in to comment.