Skip to content

Commit

Permalink
Merge pull request #4826 from Icontech/4766-allow-empty-arrays-and-ob…
Browse files Browse the repository at this point in the history
…jects-in-json-fields-with-datastore_create

Bugfix: Allow empty arrays and objects in json type fields when inserting data into datastore
  • Loading branch information
wardi committed Jun 14, 2019
2 parents 61b785f + 031357f commit 7fd6ca6
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ckanext/datastore/backend/postgres.py
Expand Up @@ -1042,7 +1042,7 @@ def upsert_data(context, data_dict):
row = []
for field in fields:
value = record.get(field['id'])
if value and field['type'].lower() == 'nested':
if value is not None and field['type'].lower() == 'nested':
# a tuple with an empty second value
value = (json.dumps(value), '')
row.append(value)
Expand Down
26 changes: 26 additions & 0 deletions ckanext/datastore/tests/test_create.py
Expand Up @@ -21,6 +21,32 @@


class TestDatastoreCreateNewTests(DatastoreFunctionalTestBase):
def test_create_works_with_empty_array_in_json_field(self):
package = factories.Dataset()
data = {
'resource': {
'package_id': package['id']
},
'fields': [{'id': 'movie', 'type': 'text'},
{'id': 'directors', 'type': 'json'}],
'records': [{'movie': 'sideways', 'directors': []}]
}
result = helpers.call_action('datastore_create', **data)
assert result['resource_id'] is not None

def test_create_works_with_empty_object_in_json_field(self):
package = factories.Dataset()
data = {
'resource': {
'package_id': package['id']
},
'fields': [{'id': 'movie', 'type': 'text'},
{'id': 'director', 'type': 'json'}],
'records': [{'movie': 'sideways', 'director': {}}]
}
result = helpers.call_action('datastore_create', **data)
assert result['resource_id'] is not None

def test_create_creates_index_on_primary_key(self):
package = factories.Dataset()
data = {
Expand Down

0 comments on commit 7fd6ca6

Please sign in to comment.