Skip to content

Commit

Permalink
Fix siren bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Renaud8469 committed Sep 6, 2017
1 parent e2d5f30 commit 6411492
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
5 changes: 1 addition & 4 deletions test/interceptors/express/index.test.js
Expand Up @@ -232,10 +232,7 @@ describe('Intercepting the response', () => {
})

it('Should return an empty Siren object', () => {
expect(resultStorage.sentJson).to.deep.equal({
class: ["undefined"],
properties: {}
})
expect(resultStorage.sentJson).to.deep.equal({})
})
})

Expand Down
11 changes: 6 additions & 5 deletions translators/siren_translator.js
@@ -1,18 +1,18 @@
const transitions = require('../state_transitions')
const _ = require('lodash')

/*
* Generates a Siren "entity". Useful for both resources,
* sub-resources and collection members.
*/
function generateSirenEntity (resource, name, host, state, isAuth, subEntityRel) {
let entity = {
class: [name]
}
let entity = {}
if (name !== 'undefined') entity.class = [name]
if (subEntityRel) {
entity.rel = [subEntityRel]
}

entity.properties = resource
if (!_.isEmpty(resource)) entity.properties = resource

let possibleTransitions = transitions.getAvailableTransitions(state, isAuth)
let controls = getActionsAndLinks(possibleTransitions, resource, state, host, subEntityRel, isAuth)
Expand Down Expand Up @@ -50,7 +50,7 @@ function getActionsAndLinks (possibleTr, data, state, host, isSubEntity, isAuth)
let actions = []
let links = []
for (let tr of possibleTr) {
if (transitions.isForEachItem(tr, state) || !isSubEntity) {
if ((transitions.isForEachItem(tr, state) && isSubEntity) || (!transitions.isForEachItem(tr, state) && !isSubEntity)) {
let action = {}
action.href = host + transitions.getUrl(tr, state, data).href

Expand All @@ -76,6 +76,7 @@ function translate (data, state, host, isAuth) {
let sirenResponse = {}

if (data instanceof Array) {
sirenResponse = generateSirenEntity({}, resourceName, host, state, isAuth)
sirenResponse.entities = []
for (let elem of data) {
sirenResponse.entities.push(generateSirenEntity(elem, resourceName, host, state, isAuth, resourceName))
Expand Down

0 comments on commit 6411492

Please sign in to comment.