Skip to content

Commit

Permalink
Add invoke task to push OpenAPI schema through swagger validator, and…
Browse files Browse the repository at this point in the history
… call it in CI
  • Loading branch information
ml-evs committed Aug 31, 2020
1 parent 878d932 commit 99d148b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .github/workflows/deps_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,11 @@ jobs:
- name: Run previously skipped tests for adapter conversion
run: pytest -rs -vvv --cov=./optimade/ --cov-report=xml --cov-append tests/adapters/

- name: Pass generated OpenAPI schemas through validator.swagger.io
run: |
invoke run-swagger-validator-on-url --url raw.githubusercontent.com/${GITHUB_REPOSITORY}/${GITHUB_SHA}/openapi/openapi.json
invoke run-swagger-validator-on-url --url raw.githubusercontent.com/${GITHUB_REPOSITORY}/${GITHUB_SHA}/openapi/index_openapi.json
- name: Upload coverage to Codecov
if: matrix.python-version == 3.8 && github.repository == 'Materials-Consortia/optimade-python-tools'
uses: codecov/codecov-action@v1
Expand Down
32 changes: 32 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,35 @@ def write_file(full_path: Path, content: str):
full_path=docs_sub_dir.joinpath(md_filename),
content=template.format(name=basename, py_path=py_path),
)


@task(help={"url": "URL of the file to validate"})
def run_swagger_validator_on_url(_, url=None):
""" This task can be used in the CI to test the generated OpenAPI schemas
with the online swagger validator.
Returns:
non-zero exit code if validation fails, otherwise returns 0.
"""

import requests

swagger_url = "https://validator.swagger.io/validator/debug"
response = requests.get(f"{swagger_url}/?url={url}")
if response.status_code != 200:
raise RuntimeError(f"Server returned status code {response.status_code}.")

try:
json_response = response.json()
except json.JSONDecodeError:
raise json.JSONDecodeError(
f"Unable to parse validator response as JSON: {response}"
)(response.json().get("messages", "Failed to get error messages from response"))

if len(json_response) > 0:
raise RuntimeError(
response.json().get(
"messages", "Failed to get error messages from response"
)
)

0 comments on commit 99d148b

Please sign in to comment.