diff --git a/annif/openapi/annif.yaml b/annif/openapi/annif.yaml index 6412280d..7f86f65f 100644 --- a/annif/openapi/annif.yaml +++ b/annif/openapi/annif.yaml @@ -195,13 +195,13 @@ paths: type: object required: - text - - candidates + - languages properties: text: type: string description: input text example: A quick brown fox jumped over the lazy dog. - candidates: + languages: type: array description: candidate languages as IETF BCP 47 codes items: @@ -209,6 +209,8 @@ paths: maxLength: 3 minLength: 2 example: en + minItems: 1 + maxItems: 5 required: true responses: 200: diff --git a/annif/rest.py b/annif/rest.py index c283a60b..18d5ecfe 100644 --- a/annif/rest.py +++ b/annif/rest.py @@ -87,16 +87,9 @@ def detect_language(body: dict[str, Any]): """return scores for detected languages formatted according to Swagger spec""" text = body.get("text") - candidates = body.get("candidates") + languages = body.get("languages") - if not candidates: - return connexion.problem( - status=400, - title="Bad Request", - detail="no candidate languages given", - ) - - detector = get_language_detector(tuple(candidates)) + detector = get_language_detector(tuple(languages)) try: proportions = detector.proportion_in_each_language(text) except ValueError: diff --git a/tests/test_openapi.py b/tests/test_openapi.py index 76f33695..4b409c5d 100644 --- a/tests/test_openapi.py +++ b/tests/test_openapi.py @@ -126,3 +126,15 @@ def test_openapi_learn_novocab(app_client): data = [] req = app_client.post("http://localhost:8000/v1/projects/novocab/learn", json=data) assert req.status_code == 503 + + +def test_rest_detect_language_no_candidates(app_client): + data = {"text": "example text", "languages": []} + req = app_client.post("http://localhost:8000/v1/detect-language", json=data) + assert req.status_code == 400 + + +def test_rest_detect_language_too_many_candidates(app_client): + data = {"text": "example text", "languages": ["en", "fr", "de", "it", "es", "nl"]} + req = app_client.post("http://localhost:8000/v1/detect-language", json=data) + assert req.status_code == 400 diff --git a/tests/test_rest.py b/tests/test_rest.py index ef32a9cc..43d42d78 100644 --- a/tests/test_rest.py +++ b/tests/test_rest.py @@ -57,7 +57,7 @@ def test_rest_detect_language_english(app): # english text should be detected with app.app_context(): result = annif.rest.detect_language( - {"text": "example text", "candidates": ["en", "fi", "sv"]} + {"text": "example text", "languages": ["en", "fi", "sv"]} )[0] assert {"language": "en", "score": 1} in result["results"] @@ -66,27 +66,21 @@ def test_rest_detect_language_unknown(app): # an unknown language should return None with app.app_context(): result = annif.rest.detect_language( - {"text": "exampley texty", "candidates": ["fi", "sv"]} + {"text": "exampley texty", "languages": ["fi", "sv"]} )[0] assert {"language": None, "score": 1} in result["results"] def test_rest_detect_language_no_text(app): with app.app_context(): - result = annif.rest.detect_language({"text": "", "candidates": ["en"]})[0] + result = annif.rest.detect_language({"text": "", "languages": ["en"]})[0] assert {"language": None, "score": 1} in result["results"] -def test_rest_detect_language_no_candidates(app): - with app.app_context(): - result = annif.rest.detect_language({"text": "example text", "candidates": []}) - assert result.status_code == 400 - - def test_rest_detect_language_unsupported_candidates(app): with app.app_context(): result = annif.rest.detect_language( - {"text": "example text", "candidates": ["unk"]} + {"text": "example text", "languages": ["unk"]} ) assert result.status_code == 400