petehug / wjelement-cpp

WJElement-cpp - Lightweight C++ wrapper for WJElement with JSON Schema support
GNU Lesser General Public License v3.0
9 stars 3 forks source link

Exiting when invalid pointer ref passed in #1

Closed sinukus closed 10 years ago

sinukus commented 10 years ago

So I was using this fine library, when by my own bad had added an invalid pointer ref to the schema. Obviously there is not much you can do in this case, but the entire assert seems a bit heavy handed.

Basically in Node::_validateObject this fires: assert(self && this);

in my schema I had: "$ref": "#/definitions/uuid" but I had moved this off, and it should have been an external ref: "$ref": "uuid.json#/definitions/uuid"

So there was no local "definitions" section any more.

Does it seem reasonable to expect an error instead of exits?

waimanu commented 10 years ago

Hi,

I'm not so sure that the assert is wrong.

_validateObject is called from _validateInstance. The first thing _validate Instance does is call _resolveRef which either returns *this (if the schema is not a $ref) or tries and resolves the reference and return a node to the referenced schema.

If an error occurs in resolving the $ref it either throws a runtime_error, returns *this or returns a NULL Node. Returning the NULL Node is wrong, but this is what I think is happening in your case. Not sure if I'm missing something, but I think this can only happen if a none std::runtime_error is thrown.

Could you debug this for me to see what actually throws?

Please also be aware that you should install a schema loader fnSchemaLoader when you get the schema cache via WJPP::Cache::GetCache(fnSchemaLoader). This way wjelement++ will pass "uuid.json" as an std::string& to the loader function so that it can get the file from wherever and return an std::string containing the content. If you don't supply this function wjelement++ will not be able to resolve uuid

Thanks

Pete

From: sinukus [mailto:notifications@github.com] Sent: Thursday, 21 August 2014 3:17 p.m. To: petehug/wjelement-cpp Subject: [wjelement-cpp] Exiting when invalid pointer ref passed in (#1)

So I was using this fine library, when by my own bad had added an invalid pointer ref to the schema. Obviously there is not much you can do in this case, but the entire assert seems a bit heavy handed.

Basically in Node::_validateObject this fires: assert(self && this);

in my schema I had: "$ref": "#/definitions/uuid" but I had moved this off, and it should have been an external ref: "$ref": "uuid.json#/definitions/uuid"

So there was no local "definitions" section any more.

Does it seem reasonable to expect an error instead of exits?

— Reply to this email directly or view it on GitHub https://github.com/petehug/wjelement-cpp/issues/1 . https://github.com/notifications/beacon/181065__eyJzY29wZSI6Ik5ld3NpZXM6QmVhY29uIiwiZXhwaXJlcyI6MTcyNDIxMDI0MywiZGF0YSI6eyJpZCI6NDAyNDgxMTR9fQ==--78fe3e4f917a1e9743a3890348da6f393ba3fe87.gif

petehug commented 10 years ago

I've fixed some issues with invalid $ref including the one mentioned here by sinukus (thanks for that!).

sinukus commented 10 years ago

Oh cool! Yeah it couldn't find the ref as it has been moved to an external ref. I'll check out your fix.