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

get_or_create_ref in json validator uses exceptions for flow control #333

Open
BalrogOfHell opened this issue Oct 31, 2024 · 3 comments
Open

Comments

@BalrogOfHell
Copy link

The following try/catch block exists in the get_or_create_ref function:

                         try {
				auto &subschema = file.unknown_keywords.at(uri.pointer()); // null is returned if not existing
				auto s = schema::make(subschema, this, {}, {{uri}});       //  A JSON Schema MUST be an object or a boolean.
				if (s) {                                                   // nullptr if invalid schema, e.g. null
					file.unknown_keywords.erase(uri.fragment());
					return s;
				}
			} catch (nlohmann::detail::out_of_range &) { // at() did not find it
			}

An out_of_range exception from nlohmann::json::at() is thrown when there is no schema to be found with the given JSON pointer, and this seems to be expected. It is generally frowned upon to use exceptions like this, ref isocpp. I would like to suggest the following fix using nlohmann::json::find instead:

   auto subschema_it = file.unknown_keywords.find(uri.pointer());
   if (subschema_it != file.unknown_keywords.end()) { // if found
      auto &subschema = subschema_it->second;
      auto s = schema::make(subschema, this, {}, {{uri}});
      if (s) { // if schema is valid (non-null)
         file.unknown_keywords.erase(uri.fragment());
         return s;
      }
   }

Noted this issue as our repository started to use more json references, this exception ends up being thrown a lot.

@pboettch
Copy link
Owner

pboettch commented Nov 3, 2024

Good point. Could you provide a PR?

@BalrogOfHell
Copy link
Author

Yessir!
#334

@BalrogOfHell
Copy link
Author

@pboettch Unsure how to debug or re-run the CI builds failing on that PR. Does not seem like I have the ability to re-trigger them. May need assistance there! 🙏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants