Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Validate OpenAPI specification in CI #481

Merged
merged 7 commits into from
Aug 31, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions .github/workflows/deps_lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,11 @@ jobs:
pip install -r requirements.txt
pip install -r requirements-dev.txt
pip install -e .
docker pull quen2404/openapi-diff

- name: Validate OpenAPI specifications
- name: Pass generated OpenAPI schemas through validator.swagger.io
run: |
openapi-spec-validator openapi/openapi.json
openapi-spec-validator openapi/index_openapi.json
invoke swagger-validator openapi/openapi.json
invoke swagger-validator openapi/index_openapi.json

- name: Check OpenAPI Schemas have not changed
run: invoke check-openapi-diff
Expand Down
1 change: 0 additions & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
pytest==6.0.1
pytest-cov==2.10.1
codecov==2.1.9
openapi-spec-validator==0.2.9
jsondiff==1.2.0
pylint==2.6.0
black==20.8b1
Expand Down
1 change: 0 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
"pytest~=6.0",
"pytest-cov~=2.10",
"codecov~=2.1",
"openapi-spec-validator~=0.2.9",
"jsondiff~=1.2",
] + server_deps
dev_deps = (
Expand Down
37 changes: 37 additions & 0 deletions tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,40 @@ 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={"fname": "The JSON file containing the OpenAPI schema to validate"})
def swagger_validator(_, fname):
"""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

def print_error(string):
for line in string.split("\n"):
print(f"\033[31m{line}\033[0m")

swagger_url = "https://validator.swagger.io/validator/debug"
with open(fname, "r") as f:
schema = json.load(f)
response = requests.post(swagger_url, json=schema)

if response.status_code != 200:
print_error(f"Server returned status code {response.status_code}.")
sys.exit(1)

try:
json_response = response.json()
except json.JSONDecodeError:
print_error(f"Unable to parse validator response as JSON: {response}")
sys.exit(1)

if json_response:
print_error(f"Schema file {fname} did not pass validation.\n")
print_error(json.dumps(response.json(), indent=2))
sys.exit(1)