Skip to content

Add validation for schema versions issue#245 #253

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

Merged
merged 6 commits into from
Jun 26, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 20 additions & 4 deletions src/controller/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

import jsonschema
from elasticsearch.exceptions import NotFoundError as ESNotFoundError

from utils import decoder
from utils.downloader import Downloader, File

Expand All @@ -23,6 +22,7 @@
config = ConfigParser()
config.read("schemas.ini")


openapis = Downloader()
openapis.download(config["openapi"].keys(), config["openapi"].values())

Expand All @@ -40,10 +40,26 @@ def validate(doc, schemas):
if not isinstance(doc, dict):
doc = decoder.to_dict(doc)

try: # validate agasint every schema
for _name, schema in schemas.items():
jsonschema.validate(doc, schema)
openapi_version = doc.get("openapi")

try: # validate against every schema
openapi_validated = False
for _name, schema in schemas.items():
# If the document is an OpenAPI document, check the version
if _name.startswith("openapi"):
_version = _name[len("openapi_v"):] # take the "v3.0" part of "openapi_v3.0" schema name
if openapi_version:
if openapi_version.startswith(_version):
# validate the document only against the openapi schema with matching version
jsonschema.validate(doc, schema)
openapi_validated = True
else:
# this should not happen, raise an error just in case
raise ValueError("OpenAPI version not found in document during validation.")
else:
jsonschema.validate(doc, schema)
if openapi_version and not openapi_validated:
raise ValueError(f"Unknown OpenAPI version (\"{openapi_version}\") for validation.")
except jsonschema.ValidationError as err:
_ = (
f"Failed {_name} validation at "
Expand Down
4 changes: 2 additions & 2 deletions src/schemas.ini
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@

[swagger]
swagger_v2: https://github.com/swagger-api/swagger-editor/v3.6.1/src/plugins/validate-json-schema/structural-validation/swagger2-schema.js

[openapi]
openapi_v3: https://github.com/swagger-api/swagger-editor/v3.15.6/src/plugins/json-schema-validator/oas3-schema.yaml
openapi_v3.0: https://github.com/swagger-api/swagger-editor/v3.15.6/src/plugins/json-schema-validator/oas3-schema.yaml
openapi_v3.1: https://github.com/swagger-api/validator-badge/v2.1.5/src/main/resources/schemas/31/official.json
x-translator: https://github.com/NCATSTranslator/translator_extensions/production/x-translator/smartapi_x-translator_schema.json
x-trapi: https://github.com/NCATSTranslator/translator_extensions/production/x-trapi/smartapi_x-trapi_schema.json
Loading
Loading