feat(api): define public problem errors, pagination, and retry contract
This commit is contained in:
1 parent
b2f6e87bfe
commit
565ca94bbb
86 files changed
+8200
-154
No files matched your search
@@ -8,7 +8,7 @@ Helper tools that work across multiple parts of the repository.
|
||||
- `check-api-compat.sh` checks public API compatibility against a selected Git ref.
|
||||
- `check-generated.py` detects generated files that differ from Git.
|
||||
- `docs-check.py` checks documentation, README language pairs, and local links.
|
||||
- `test_docs_check.py` and `test_merge_schemas.py` test documentation validation and schema merging.
|
||||
- `test_docs_check.py` and `test_merge_schemas.py` test documentation validation and schema merging; `test_contract_copies.py` checks that the OpenAPI 3.2 fixture and the ML contract keep identical copies of the api/core conventions.
|
||||
|
||||
Run them through Make from the repository root: `make -C api check`, `make -C api check-compat API_BASE_REF=<ref>`, `make check-generated`, and `make docs-check`. Service-specific scripts live in their service directories.
|
||||
|
||||
|
||||
+1
-1
@@ -8,7 +8,7 @@
|
||||
- `check-api-compat.sh` проверяет совместимость публичного API с выбранной Git-ссылкой.
|
||||
- `check-generated.py` обнаруживает расхождения сгенерированных файлов с Git.
|
||||
- `docs-check.py` проверяет документацию, языковые пары README и локальные ссылки.
|
||||
- `test_docs_check.py` и `test_merge_schemas.py` проверяют валидацию документации и объединение схем.
|
||||
- `test_docs_check.py` и `test_merge_schemas.py` проверяют валидацию документации и объединение схем; `test_contract_copies.py` проверяет, что фикстура OpenAPI 3.2 и ML-контракт хранят идентичные копии соглашений api/core.
|
||||
|
||||
Запускайте их через Make из корня: `make -C api check`, `make -C api check-compat API_BASE_REF=<ref>`, `make check-generated` и `make docs-check`. Скрипты конкретного сервиса находятся в его каталоге.
|
||||
|
||||
|
||||
@@ -0,0 +1,107 @@
|
||||
"""Check that contract convention copies stay identical to api/core.
|
||||
|
||||
The OpenAPI 3.2 compatibility fixture and the ML contract cannot reference
|
||||
api/core files: ogen rejects external references and the ML contract is a
|
||||
separate specification. They therefore carry copies of the api/core
|
||||
conventions. These tests read the bundled api/core contract, so run them after
|
||||
`make -C api bundle`.
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
from typing import Any
|
||||
|
||||
import yaml
|
||||
|
||||
API_DIR = Path(__file__).resolve().parents[1] / "api"
|
||||
CORE_BUNDLE = API_DIR / "core" / "openapi.bundle.yml"
|
||||
ML_BUNDLE = API_DIR / "ml" / "openapi.bundle.yml"
|
||||
FIXTURE = API_DIR / "testdata" / "openapi-3.2.0.yml"
|
||||
SHARED_SECTIONS = ("schemas", "parameters", "headers")
|
||||
# Every api/core convention component that the fixture must exercise.
|
||||
EXPECTED_FIXTURE_COPIES = {
|
||||
("schemas", "Problem"),
|
||||
("schemas", "ProblemCode"),
|
||||
("schemas", "ProblemFieldError"),
|
||||
("schemas", "ProblemFieldLocation"),
|
||||
("schemas", "Timestamp"),
|
||||
("schemas", "PageInfo"),
|
||||
("parameters", "IdempotencyKey"),
|
||||
("parameters", "IfMatch"),
|
||||
("parameters", "PageLimit"),
|
||||
("parameters", "PageCursor"),
|
||||
("headers", "ETag"),
|
||||
}
|
||||
PROBLEM_SCHEMAS = ("Problem", "ProblemCode", "ProblemFieldError", "ProblemFieldLocation")
|
||||
|
||||
|
||||
def load_components(path: Path) -> dict[str, Any]:
|
||||
with path.open(encoding="utf-8") as stream:
|
||||
document = yaml.safe_load(stream)
|
||||
components = document.get("components")
|
||||
if not isinstance(components, dict):
|
||||
raise AssertionError(f"{path} has no components mapping")
|
||||
return components
|
||||
|
||||
|
||||
def without_descriptions(value: Any) -> Any:
|
||||
"""Return a copy of a schema without description text."""
|
||||
if isinstance(value, dict):
|
||||
return {
|
||||
key: without_descriptions(item)
|
||||
for key, item in value.items()
|
||||
if key != "description"
|
||||
}
|
||||
if isinstance(value, list):
|
||||
return [without_descriptions(item) for item in value]
|
||||
return value
|
||||
|
||||
|
||||
def without_examples(response: dict[str, Any]) -> dict[str, Any]:
|
||||
"""Return a copy of a response without media type examples."""
|
||||
content = {
|
||||
media_type: {key: item for key, item in media.items() if key != "examples"}
|
||||
for media_type, media in response.get("content", {}).items()
|
||||
}
|
||||
return {**response, "content": content}
|
||||
|
||||
|
||||
class ContractCopiesTest(unittest.TestCase):
|
||||
def setUp(self) -> None:
|
||||
self.core = load_components(CORE_BUNDLE)
|
||||
|
||||
def test_fixture_copies_match_core_definitions(self) -> None:
|
||||
fixture = load_components(FIXTURE)
|
||||
compared = set()
|
||||
for section in SHARED_SECTIONS:
|
||||
for name, definition in fixture.get(section, {}).items():
|
||||
if name not in self.core.get(section, {}):
|
||||
continue
|
||||
compared.add((section, name))
|
||||
with self.subTest(section=section, name=name):
|
||||
self.assertEqual(definition, self.core[section][name])
|
||||
self.assertEqual(compared, EXPECTED_FIXTURE_COPIES)
|
||||
|
||||
def test_fixture_responses_match_core_without_examples(self) -> None:
|
||||
fixture_responses = load_components(FIXTURE)["responses"]
|
||||
for name, definition in fixture_responses.items():
|
||||
with self.subTest(name=name):
|
||||
self.assertIn(name, self.core["responses"])
|
||||
expected = without_examples(self.core["responses"][name])
|
||||
self.assertEqual(definition, expected)
|
||||
|
||||
def test_ml_problem_schemas_match_core_except_descriptions(self) -> None:
|
||||
ml_schemas = load_components(ML_BUNDLE)["schemas"]
|
||||
for name in PROBLEM_SCHEMAS:
|
||||
with self.subTest(name=name):
|
||||
self.assertIn(name, ml_schemas)
|
||||
self.assertEqual(
|
||||
without_descriptions(ml_schemas[name]),
|
||||
without_descriptions(self.core["schemas"][name]),
|
||||
)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in new issue
Block a user