Skip to content

Commit

Permalink
[#4766] Allow empty arrays and objects in json type fields with datas…
Browse files Browse the repository at this point in the history
…tore_create

Currently, adding an empty array or an empty object to a field of type json using datastore_create results in a validation error and status code 409.

After this change, empty arrays and empty objects can be added to fields of type json.
  • Loading branch information
Icontech committed May 29, 2019
1 parent c8ac563 commit f80f15c
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 f80f15c

Please sign in to comment.