Skip to content

ConfigurationView and JSON is broken for array access #3635

Closed
@Coder666

Description

Hi,

When using JSONConfiguration in combination with ConfigurationView there is an issue with array access

For example, the following code does not work as expected:

std::string json = R"json(   { "foo" : [ "bar" ] }   )json";

std::shared_ptr< Poco::Util::JSONConfiguration > pConfig = std::make_shared< Poco::Util::JSONConfiguration >();
std::istringstream stream(json);
pConfig->load(stream);

Poco::Util::AbstractConfiguration::Ptr pView = pConfig->createView( "foo" );

//now grab item 0 which should be "bar"

std::string shouldBeBar = pView->getString("[0]");

//now grab item 1 which is not valid and should throw an exception

std::string shouldThrow= pView->getString("[1]");

// ERROR!! <--- No exception is thrown

In the code above, shouldThrow is set to the string { "foo" : [ "bar" ] } which is incorrect.

The error can be tracked to this code in the ConfigurationView, the first term evaluates to false (not found) then the second term is evaluated and returns true, also setting value to { "foo" : [ "bar" ] } clearly this is not expected behaviour.

bool ConfigurationView::getRaw(const std::string& key, std::string& value) const
{
	std::string translatedKey = translateKey(key);
	return _pConfig->getRaw(translatedKey, value) || _pConfig->getRaw(key, value);
}

At the moment I am able to hack in a fix by removing the second term with no ill effects. However this may impact other configuration types. Needs some serious investigation...

bool ConfigurationView::getRaw(const std::string& key, std::string& value) const
{
	std::string translatedKey = translateKey(key);
	return _pConfig->getRaw(translatedKey, value);
}

Cheers

Tom

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions