inveniosoftware / invenio-rdm-records

DataCite-based data model for InvenioRDM flavour.
https://invenio-rdm-records.readthedocs.io
MIT License
15 stars 87 forks source link

API Error: Unexpected 500 Response When Supplying Duplicate Entries #1313

Closed Samk13 closed 4 months ago

Samk13 commented 1 year ago

Package version (if known): 12 b

Describe the bug

There seems to be a bug within the API validation that causes a server error 500 when feeding identical values such as affiliations or languages. This could potentially apply to all array values.

Steps to Reproduce

  1. feed this JSON body to endpoint: POST {{baseURL}}/api/records
    {
        "access":{
            "record":"public",
            "files":"public"
        },
        "files":{
            "enabled":false
        },
        "type":"community-submission",
        "metadata":{
            "resource_type":{
                "id":"publication-other"
            },
            "description":"I",
            "subjects":[
                {
                    "subject":"B"
                }
            ],
            "creators":[
                {
                    "person_or_org":{
                        "family_name":"T",
                        "given_name":"A",
                        "name":"A T",
                        "type":"personal",
                        "identifiers":[
                            {
                                "identifier":"0000-0001-0001-0001",
                                "scheme":"orcid"
                            }
                        ]
                    },
                    "affiliations":[
                        {
                            "name":"KTH [177] (EES"
                        },
                        {
                            "name":"KTH [177] (EES"
                        }
                    ]
                }
            ],
            "title":"C",
            "publication_date":"2066-06-06",
            "languages":[
                {
                    "id":"eng"
                }
            ]
        }
    }
  2. receive the err

Expected behavior

Normal validation error

Screenshots (if applicable)


<!--

Traceback (most recent call last):
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask/app.py", line 2552, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/werkzeug/middleware/proxy_fix.py", line 187, in __call__
    return self.app(environ, start_response)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/werkzeug/middleware/dispatcher.py", line 78, in __call__
    return app(environ, start_response)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask/app.py", line 2552, in __call__
    return self.wsgi_app(environ, start_response)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/werkzeug/middleware/proxy_fix.py", line 187, in __call__
    return self.app(environ, start_response)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask/app.py", line 2532, in wsgi_app
    response = self.handle_exception(e)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask/app.py", line 2529, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask/app.py", line 1825, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask/app.py", line 1823, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask/app.py", line 1799, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask_resources/resources.py", line 65, in view
    return view_meth()
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask_resources/content_negotiation.py", line 116, in inner_content_negotiation
    return f(*args, **kwargs)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask_resources/parsers/decorators.py", line 51, in inner
    return f(self, *args, **kwargs)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask_resources/parsers/decorators.py", line 90, in inner
    return f(self, *args, **kwargs)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/flask_resources/responses.py", line 39, in inner
    res = f(*args, **kwargs)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/invenio_records_resources/resources/records/resource.py", line 93, in create
    item = self.service.create(
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/invenio_records_resources/services/uow.py", line 324, in inner
    res = f(self, *args, **kwargs)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/invenio_drafts_resources/services/records/service.py", line 265, in create
    res = self._create(
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/invenio_records_resources/services/uow.py", line 328, in inner
    return f(self, *args, **kwargs)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/invenio_records_resources/services/records/service.py", line 350, in _create
    uow.register(RecordCommitOp(record, self.indexer))
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/invenio_records_resources/services/uow.py", line 297, in register
    op.on_register(self)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/invenio_records_resources/services/uow.py", line 150, in on_register
    self._record.commit()
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/invenio_records/api.py", line 453, in commit
    json = self._validate(format_checker=format_checker, validator=validator)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/invenio_records/api.py", line 193, in _validate
    _records_state.validate(
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/invenio_records/ext.py", line 67, in validate
    return validate(data, schema, cls=validator_cls, resolver=resolver, **kwargs)
  File "/home/user/.pyenv/versions/3.9.16/envs/api-test/lib/python3.9/site-packages/jsonschema/validators.py", line 1121, in validate
    raise error
jsonschema.exceptions.ValidationError: [{'name': 'KTH [177] (EES'}, {'name': 'KTH [177] (EES'}] has non-unique elements

Failed validating 'uniqueItems' in schema['properties']['metadata']['properties']['creators']['items']['properties']['affiliations']:
    {'items': {'$ref': '#/affiliation'},
     'type': 'array',
     'uniqueItems': True}

On instance['metadata']['creators'][0]['affiliations']:
    [{'name': 'KTH [177] (EES'}, {'name': 'KTH [177] (EES'}]

-->
Samk13 commented 1 year ago

slint The uniqueItems validation in the JSONSchema (i.e. when saving the record JSON to the DB) but not in the Marshmallow schema when loading the field.

github-actions[bot] commented 1 year ago

This issue was automatically marked as stale.

Samk13 commented 4 months ago

Resolved with https://github.com/inveniosoftware/invenio-rdm-records/pull/1628