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

Develop JSON Schema validator #70

Open
RalfG opened this issue Jul 10, 2020 · 3 comments
Open

Develop JSON Schema validator #70

RalfG opened this issue Jul 10, 2020 · 3 comments

Comments

@RalfG
Copy link

RalfG commented Jul 10, 2020

As discussed during today's call, we need a validator for the JSON response that goes further than only checking whether there is a response or not.

@RalfG
Copy link
Author

RalfG commented Jul 10, 2020

I guess some of these might come in handy: https://github.com/json-schema-org/JSON-Schema-Test-Suite#python

@RalfG
Copy link
Author

RalfG commented Aug 25, 2020

@edeutsch, @bittremieux,

Here's a simple Python script that does exactly what we want:

"""
Validate API response with OpenAPI YAML schema.

For example:

$ python proxi_validator.py --openapi-yaml "specs/swagger.yaml" --request-url "http://proteomecentral.proteomexchange.org/api/proxi/v0.1/spectra?resultType=full&usi=mzspec:PXL000006:02-14-2019:index:1250"

"""

import click
import requests
import yaml
from jsonschema import validate
from openapi_schema_to_json_schema import to_json_schema


def jsonschema_from_yaml_file(path_to_yaml):
    """Read OpenAPI schema yaml file and return as JSON Schema dictionary."""
    with open(path_to_yaml, 'r') as stream:
        open_api_schema = yaml.safe_load(stream)
    jsonschema = to_json_schema(open_api_schema)
    return jsonschema


def get_api_response(url):
    """Get API response JSON object."""
    response = requests.get(url)
    return response.json()


@click.command()
@click.argument("openapi-yaml", type=str)
@click.argument("request-url", type=str)
def main(**kwargs):
    """Validate API response with OpenAPI YAML schema."""
    validate(
        get_api_response(kwargs["request_url"]),
        schema=jsonschema_from_yaml_file(kwargs["openapi_yaml"])
    )


if __name__ == "__main__":
    main()

A ValidationError is thrown if the API response is invalid for the given swagger.yaml file.

Do I open a PR to add the script to the repository?

@bittremieux
Copy link

Ah crap, I should've checked GitHub first, because I started on my own script.

With the flex library you can actually avoid having to convert the Swagger file to JSON schema, so that should be a bit more efficient.

The code is very simple:

import requests
from flex.core import load, validate_api_call

schema = load('https://raw.githubusercontent.com/HUPO-PSI/proxi-schemas/master/specs/swagger.yaml')

url = 'http://proteomecentral.proteomexchange.org/api/proxi/v0.1/spectra?resultType=full&usi=mzspec:PXL000006:02-14-2019:index:1250'
response = requests.get(url)

validate_api_call(schema, raw_request=response.request, raw_response=response)

See also the flex documentation on API call validation.

There seem to be several issues with the Swagger definition file and the API response preventing successful validation. So I guess it's good to have a validator. 😉

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants