def _extract_response_schema(self, response, file_path): """Find schemas defined for a given API response and return the schema object""" schema_loc = None if not response.body: # Handle parsing errors in ramlfications manually, notably for schemas in RAML 1.0 try: schema_loc = response.raw[response.code]["body"]["type"] except (KeyError, TypeError): pass else: for entry in response.body: schema_loc = entry.schema if not schema_loc: if "type" in entry.raw: schema_loc = entry.raw["type"] apis_path = os.path.dirname(file_path) spec_path = os.path.dirname(apis_path) if isinstance(schema_loc, dict): return load_resolved_schema(spec_path, schema_obj=schema_loc) elif schema_loc in self.global_schemas and self.global_schemas[ schema_loc] is not None: return load_resolved_schema( spec_path, schema_obj=self.global_schemas[schema_loc]) else: return None
def check_staged_complies_with_constraints(self, port, portList): """Check that the staged endpoint is using parameters that meet the constents of the /constraints endpoint""" for myPort in portList: dest = "single/" + port + "s/" + myPort + "/staged/" valid, response = self.is05_utils.checkCleanRequestJSON( "GET", dest) if valid: try: schema = load_resolved_schema( self.apis[CONN_API_KEY]["spec_path"], port + "_transport_params_rtp.json") except FileNotFoundError: schema = load_resolved_schema( self.apis[CONN_API_KEY]["spec_path"], "v1.0_" + port + "_transport_params_rtp.json") constraints_valid, constraints_response = self.is05_utils.checkCleanRequestJSON( "GET", "single/" + port + "s/" + myPort + "/constraints/") if constraints_valid: count = 0 try: for params in response['transport_params']: try: schema.update(constraints_response[count]) except IndexError: return False, "Number of 'legs' in constraints does not match the number in " \ "transport_params" schema["items"][ "$schema"] = "http://json-schema.org/draft-04/schema#" try: self.validate_schema(params, schema["items"]) except ValidationError as e: return False, "Staged endpoint does not comply with constraints in leg {}: " \ "{}".format(count, str(e)) except SchemaError as e: return False, "Invalid schema resulted from combining constraints in leg {}: {}".format( count, str(e)) count = count + 1 except KeyError: return False, "Expected 'transport_params' key in '/staged'." else: return False, constraints_response else: return False, response return True, ""
def _extract_body_schema(self, resource, file_path): """Locate the schema for the request body if one exists""" body_schema = None if resource.body is not None: for attr in resource.body: if attr.mime_type == "schema": apis_path = os.path.dirname(file_path) spec_path = os.path.dirname(apis_path) body_schema = load_resolved_schema(spec_path, schema_obj=attr.raw) break return body_schema