question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

"Cannot set property 'x-alternatives' of undefined" for .when without a then: clause

See original GitHub issue

Context

  • Hapi-swagger version: 7.2.0
  • Joi version: 9.0.4

What are you trying to achieve or the steps to reproduce ?

I have the following validation with Joi 9.0.4. Joi allows when clauses with either ‘then’ or ‘otherwise’ but it seems to raise errors in the latest version of hapi-swagger

tagName: joi.string().when('parentType', {
            is: 'tag',
            otherwise: joi.allow(null)
        }).required().description('Name of the tag where the document was found.  Only applicable if parentType == tag'),

What result did you get?

I get the following error when I try to load the swagger JSON

TypeError: Uncaught error: Cannot set property 'x-alternatives' of undefined
    at internals.properties.parseProperty.internals.properties.parseString.internals.properties.parseObject.internals.properties.parseAlternatives.joiObj._inner.matches.reduce [as parseAlternatives] (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/properties.js:496:40)
    at internals.properties.parseProperty.isAlt [as parseProperty] (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/properties.js:180:25)
    at /Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/properties.js:361:45
    at Array.forEach (native)
    at internals.properties.parseProperty.internals.properties.parseString.internals.properties.parseObject.isAlt [as parseObject] (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/properties.js:351:12)
    at internals.properties.parseProperty.isAlt [as parseProperty] (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/properties.js:139:29)
    at internals.paths.build.internals.paths.buildRoutes.internals.paths.getSwaggerStructures (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/paths.js:451:41)
    at /Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/paths.js:209:38
    at Array.forEach (native)
    at internals.paths.build.internals.paths.buildRoutes.routes [as buildRoutes] (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/paths.js:179:12)
    at internals.paths.build.routes.forEach [as build] (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/paths.js:151:17)
    at Object.builder.getSwaggerJSON (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/builder.js:109:26)
    at settings.log.plugin.route.config.handler.plugin.methods.getSwaggerJSON.Builder.getSwaggerJSON (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi-swagger/lib/index.js:115:29)
    at Object.exports.execute.internals.prerequisites.internals.handler.callback [as handler] (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi/lib/handler.js:96:36)
    at /Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi/lib/handler.js:30:23
    at internals.Protect.run.finish [as run] (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi/lib/protect.js:62:12)
    at exports.execute.finalize (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi/lib/handler.js:24:22)
    at each (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi/lib/request.js:380:16)
    at iterate (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi/node_modules/items/lib/index.js:36:13)
    at done (/Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi/node_modules/items/lib/index.js:28:25)
    at /Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi/lib/route.js:384:20
    at /Users/timjones/gyda/haakon-env/repos/node-tracking/node_modules/hapi/node_modules/statehood/lib/index.js:244:16

What did you expect ?

No error. It looks like properties.parseAlternatives assumes there must be a .then around line 478 of properties.js.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

4reactions
Y-LyN-10commented, Jul 24, 2018

Hello! The fix proposed by @hostiphur works, but looks-like that it was commented out at some point, so this issue is reproducible now (with Joi.forbidden() )

hapi: v17.4.0 hapi-swagger: v9.1.1 joi: v13.1.2

2reactions
zsiglincommented, Jul 31, 2018

@glennjones Can we get this reimplemented? Any reason this was commented out?

Read more comments on GitHub >

github_iconTop Results From Across the Web

"Cannot set property 'x-alternatives' of undefined" for .when ...
No error. It looks like properties.parseAlternatives assumes there must be a .then around line 478 of properties.js. The text was updated ...
Read more >
JavaScript - cannot set property of undefined - Stack Overflow
The object stored at d[a] has not been set to anything. Thus, d[a] evaluates to undefined . You can't assign a property to...
Read more >
Fix Cannot Set Property of Null Error in JavaScript - YouTube
Check us out at https://www.skillforge.com The " cannot set property of null " error is a very common JavaScript issue that you may...
Read more >
How To Fix 'Uncaught TypeError: Cannot set properties of null'
Join Discord https://selftaught-dev.com/join-discord//r/SelfTaughtDev - https://www.reddit.com/r/selftaughtdevGet My Resume Template Free ...
Read more >
Error "Uncaught TypeError: Cannot set property 'value' of ...
The code has not changed at all. I have not touched the project in the past 5 days. It was working when I...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found