4.1.1.1. oastools.parse package

4.1.1.1.1. Submodules

4.1.1.1.2. oastools.parse.parser module

parser

Author:Bilal Shaikh < bilalshaikh42@gmail.com >
Date:2019-08-29
Copyright:2019, Bilal Shaikh
License:MIT

Adapted from Swagger_Parser (MIT Licence) to work with openapi 3 https://github.com/Trax-air/swagger-parser/blob/master/swagger_parser/swagger_parser.py

class oastools.parse.parser.OASParser(oas_spec, use_example=True)

Bases: object

build_definitions_example()

Parse all definitions in the swagger specification.

build_one_definition_example(def_name)

Build the example for the given definition. :param def_name: Name of the definition.

Returns:True if the example has been created, False if an error occured.
static check_type(value, type_def)

Check if the value is in the type given in type_def. :param value: the var to test. :param type_def: string representing the type in swagger.

Returns:True if the type is correct, False otherwise.
static get_definition_name_from_ref(ref)

Get the definition name of the given $ref value(Swagger value). :param ref: ref value (ex: “#/definitions/CustomDefinition”)

Returns:The definition name corresponding to the ref.
get_dict_definition(dict, get_list=False)

Get the definition name of the given dict. :param dict: dict to test. :param get_list: if set to true, return a list of definition that match the body.

if False, only return the first.
Returns:The definition name or None if the dict does not match any definition. If get_list is True, return a list of definition_name.
get_example_from_prop_spec(prop_spec, from_allof=False)

Return an example value from a property specification. :param prop_spec: the specification of the property. :param from_allof: whether these properties are part of an

allOf section
Returns:An example value
get_path_spec(path, action=None)

Get the specification matching with the given path. :param path: path we want the specification. :param action: get the specification for the given action.

Returns:A tuple with the base name of the path and the specification. Or (None, None) if no specification is found.
get_paths_data()

Get data for each paths in the swagger specification. Get also the list of operationId.

get_request_data(path, action, body=None)

Get the default data and status code of the given path + action request. :param path: path of the request. :param action: action of the request(get, post, delete…) :param body: body sent, used to sent it back for post request.

Returns:A tuple with the default response data and status code In case of default status_code, use 0
get_response_example(resp_spec)

Get a response example from a response spec.

get_send_request_correct_body(path, action)

Get an example body which is correct to send to the given path with the given action. :param path: path of the request :param action: action of the request (get, post, put, delete)

Returns:A dict representing a correct body for the request or None if no body is required.
validate_additional_properties(valid_response, response)
Validates additional properties. In additional properties, we only
need to compare the values of the dict, not the keys
Parameters:
  • valid_response – An example response (for example generated in _get_example_from_properties(self, spec)) Type is DICT
  • response – The actual dict coming from the response Type is DICT
Returns:

A boolean - whether the actual response validates against the given example

validate_definition(definition_name, dict_to_test, definition=None)

Validate the given dict according to the given definition. :param definition_name: name of the the definition. :param dict_to_test: dict to test.

Returns:True if the given dict match the definition, False otherwise.
validate_request(path, action, body=None, query=None)
Check if the given request is valid.
Validates the body and the query # Rules to validate the BODY: # Let’s limit this to mime types that either contain ‘text’ or ‘json’ # 1. if body is None, there must not be any required parameters in # the given schema # 2. if the mime type contains ‘json’, body must not be ‘’, but can # be {} # 3. if the mime type contains ‘text’, body can be any string # 4. if no mime type (‘consumes’) is given.. DISALLOW # 5. if the body is empty (‘’ or {}), there must not be any required parameters # 6. if there is something in the body, it must adhere to the given schema # -> will call the validate body function
Parameters:
  • path – path of the request.
  • action – action of the request(get, post, delete…).
  • body – body of the request.
  • query – dict with the query parameters.
Returns:

True if the request is valid, False otherwise.

class oastools.parse.parser.OpenApiParser(path=None)

Bases: object

4.1.1.1.3. Module contents