def _expand(self, uri, schema, doc_scope): _(""" expand a schema to add properties of all definitions it extends if a URI is given, it will be used to identify the schema in a cache store. If no resolver is given, use a resolver with local schema store, with the URI as referring document. :param schema: schema definition :type schema: dict :param uri: schema uri :type uri: string :param doc_scope: current doc uri :type doc_scope: string :rtype: dict """) uri = self._urljoin_cache(self.resolution_scope, uri) schema_scope, frag = urldefrag(uri) ref = schema.pop('$ref', None) if ref: ref = self._urljoin_cache(doc_scope, ref) uri_, schema_ = RefResolver.resolve(self, ref) sch = self._expand(uri_, schema_, doc_scope) schema.update(sch) if 'items' in schema and '$ref' in schema['items']: ref = schema['items'].pop('$ref') uri_, schema_ = RefResolver.resolve(self, ref) schema['items'].update(schema_) for k, v in schema.get('properties', {}).items(): if '$ref' in v: uri_, schema_ = RefResolver.resolve(self, v.pop('$ref')) schema['properties'][k].update(schema_) extends = [ RefResolver.resolve(self, e)[1] for e in schema.pop("extends", []) ] schema['properties'] = ChainMap( schema.get('properties', {}), *[e.get('properties', {}) for e in extends]) if not schema['properties']: del schema['properties'] return schema
class OpenAPISpec: resource_spec_cls = ResourceSpec @classmethod def from_file(cls, filename): with open(filename) as f: if filename.endswith('.json'): spec_dict = json.load(f) else: spec_dict = yaml.safe_load(f) return cls(spec_dict) def __init__(self, spec_dict): self.spec_dict = spec_dict self.resources = self._initialize_resources() self.ref_resolver = RefResolver('', self.spec_dict) def _initialize_resources(self): resources = OrderedDict() for path in self.spec_dict['paths']: resource_spec = self.resource_spec_cls(path, self) resources[resource_spec.url_rule] = resource_spec return resources @property def operations(self): for resource in self.resources.values(): yield from resource.operations.values() def get_operation_spec(self, path, method): # delete ending backslash or query string path = re.sub(r'/?([?#].*)?$', '', path) for url_rule, resource in self.resources.items(): if url_rule.match(path): return resource.operations.get(method.lower()) def resolve_ref(self, schema): schema = deepcopy(schema) def _resolve_ref(schema): """find all ref""" if isinstance(schema, dict): if '$ref' in schema: schema = self.ref_resolver.resolve(schema['$ref'])[1] schema = strict_schema(schema) schema = _resolve_ref(schema) else: schema = { key: _resolve_ref(value) for key, value in schema.items() } elif isinstance(schema, list): schema = [_resolve_ref(item) for item in schema] return schema return _resolve_ref(schema)
class Spec(object): """Represents a Swagger Specification for a service. :param spec_dict: Swagger API specification in json-like dict form :param origin_url: URL from which the spec was retrieved. :param http_client: Used to retrive the spec via http/https. :type http_client: :class:`bravado.http_client.HTTPClient` :param config: Configuration dict. See CONFIG_DEFAULTS. """ def __init__(self, spec_dict, origin_url=None, http_client=None, config=None): self.spec_dict = spec_dict self.origin_url = origin_url self.http_client = http_client self.api_url = None self.config = dict(CONFIG_DEFAULTS, **(config or {})) # (key, value) = (simple format def name, Model type) # (key, value) = (#/ format def ref, Model type) self.definitions = {} # (key, value) = (simple resource name, Resource) # (key, value) = (#/ format resource ref, Resource) self.resources = None # (key, value) = (simple ref name, param_spec in dict form) # (key, value) = (#/ format ref name, param_spec in dict form) self.params = None # Built on-demand - see get_op_for_request(..) self._request_to_op_map = None # (key, value) = (format name, SwaggerFormat) self.user_defined_formats = {} self.format_checker = FormatChecker() self.resolver = RefResolver( base_uri=origin_url or '', referrer=self.spec_dict, handlers=build_http_handlers(http_client), ) self._validate_config() if self.config['internally_dereference_refs']: # If internally_dereference_refs is enabled we do NOT need to resolve references anymore # it's useless to evaluate is_ref every time self.deref = lambda ref_dict: ref_dict else: self.deref = self._force_deref def _validate_config(self): """ Validates the correctness of the configurations injected and makes sure that: - no extra config keys are available on the config dictionary - dependent configs are checked :return: True if the initial configs are valid, False otherwise :rtype: bool """ are_config_changed = False extraneous_keys = set(iterkeys(self.config)) - set( iterkeys(CONFIG_DEFAULTS)) if extraneous_keys: are_config_changed = True for key in extraneous_keys: warnings.warn( message='config {} is not a recognized config key'.format( key), category=Warning, ) if self.config['internally_dereference_refs'] and not self.config[ 'validate_swagger_spec']: are_config_changed = True self.config['internally_dereference_refs'] = False warnings.warn( message= 'internally_dereference_refs config disabled because validate_swagger_spec has to be enabled', category=Warning, ) return not are_config_changed @cached_property def client_spec_dict(self): """Return a copy of spec_dict with x-scope metadata removed so that it is suitable for consumption by Swagger clients. You may be asking, "Why is there a difference between the Swagger spec a client sees and the one used internally?". Well, as part of the ingestion process, x-scope metadata is added to spec_dict so that $refs can be de-reffed successfully during requset/response validation and marshalling. This medatadata is specific to the context of the server and contains files and paths that are not relevant to the client. This is required so the client does not re-use (and in turn, re-creates) the invalid x-scope metadata created by the server. For example, a section of spec_dict that contains a ref would change as folows. Before: 'MON': { '$ref': '#/definitions/DayHours', 'x-scope': [ 'file:///happyhour/api_docs/swagger.json', 'file:///happyhour/api_docs/swagger.json#/definitions/WeekHours' ] } After: 'MON': { '$ref': '#/definitions/DayHours' } """ return strip_xscope(self.spec_dict) @classmethod def from_dict(cls, spec_dict, origin_url=None, http_client=None, config=None): """Build a :class:`Spec` from Swagger API Specificiation :param spec_dict: swagger spec in json-like dict form. :param origin_url: the url used to retrieve the spec, if any :type origin_url: str :param: http_client: http client used to download remote $refs :param config: Configuration dict. See CONFIG_DEFAULTS. """ spec = cls(spec_dict, origin_url, http_client, config) spec.build() return spec def _validate_spec(self): if self.config['validate_swagger_spec']: self.resolver = validator20.validate_spec( spec_dict=self.spec_dict, spec_url=self.origin_url or '', http_handlers=build_http_handlers(self.http_client), ) def build(self): self._validate_spec() post_process_spec( self, on_container_callbacks=[ functools.partial( tag_models, visited_models={}, swagger_spec=self, ), functools.partial( collect_models, models=self.definitions, swagger_spec=self, ), ], ) for format in self.config['formats']: self.register_format(format) self.api_url = build_api_serving_url(self.spec_dict, self.origin_url) self.resources = build_resources(self) @cached_property def _internal_spec_dict(self): if self.config['internally_dereference_refs']: return self.deref_flattened_spec else: return self.spec_dict def _force_deref(self, ref_dict): """Dereference ref_dict (if it is indeed a ref) and return what the ref points to. :param ref_dict: {'$ref': '#/blah/blah'} :return: dereferenced value of ref_dict :rtype: scalar, list, dict """ if ref_dict is None or not is_ref(ref_dict): return ref_dict # Restore attached resolution scope before resolving since the # resolver doesn't have a traversal history (accumulated scope_stack) # when asked to resolve. with in_scope(self.resolver, ref_dict): _, target = self.resolver.resolve(ref_dict['$ref']) return target def deref(self, ref_dict): # This method is actually set in __init__ pass def get_op_for_request(self, http_method, path_pattern): """Return the Swagger operation for the passed in request http method and path pattern. Makes it really easy for server-side implementations to map incoming requests to the Swagger spec. :param http_method: http method of the request :param path_pattern: request path pattern. e.g. /foo/{bar}/baz/{id} :returns: the matching operation or None if a match couldn't be found :rtype: :class:`bravado_core.operation.Operation` """ if self._request_to_op_map is None: # lazy initialization self._request_to_op_map = {} base_path = self.spec_dict.get('basePath', '').rstrip('/') for resource in self.resources.values(): for op in resource.operations.values(): full_path = base_path + op.path_name key = (op.http_method, full_path) self._request_to_op_map[key] = op key = (http_method.lower(), path_pattern) return self._request_to_op_map.get(key) def register_format(self, user_defined_format): """Registers a user-defined format to be used with this spec. :type user_defined_format: :class:`bravado_core.formatter.SwaggerFormat` """ name = user_defined_format.format self.user_defined_formats[name] = user_defined_format validate = return_true_wrapper(user_defined_format.validate) self.format_checker.checks(name, raises=(SwaggerValidationError, ))(validate) def get_format(self, name): """ :param name: Name of the format to retrieve :rtype: :class:`bravado_core.formatters.SwaggerFormat` """ if name in formatter.DEFAULT_FORMATS: return formatter.DEFAULT_FORMATS[name] format = self.user_defined_formats.get(name) if format is None: warnings.warn( message='{0} format is not registered with bravado-core!'. format(name), category=Warning, ) return format @cached_property def security_definitions(self): security_defs = {} for security_name, security_def in iteritems( self.spec_dict.get('securityDefinitions', {})): security_defs[security_name] = SecurityDefinition( self, security_def) return security_defs @cached_property def flattened_spec(self): """ Representation of the current swagger specs that could be written to a single file. NOTE: The representation strips out all the definitions that are not referenced :return: """ if not self.config['validate_swagger_spec']: raise RuntimeError( 'Swagger Specs have to be validated before flattening.') # If resources are defined it means that Spec has been built and so swagger specs have been validated if self.resources is None: self._validate_spec() return strip_xscope(spec_dict=flattened_spec( spec_dict=self.spec_dict, spec_resolver=self.resolver, spec_url=self.origin_url, http_handlers=build_http_handlers(self.http_client), spec_definitions=self.definitions, ), ) @cached_property def deref_flattened_spec(self): deref_spec_dict = JsonRef.replace_refs(self.flattened_spec) @memoize_by_id def descend(obj): # Inline modification of obj # This method is needed because JsonRef could produce performance penalties in accessing # the proxied attributes if isinstance(obj, JsonRef): # Extract the proxied value # http://jsonref.readthedocs.io/en/latest/#jsonref.JsonRef.__subject__ return obj.__subject__ if is_dict_like(obj): for key in list(iterkeys(obj)): obj[key] = descend(obj[key]) elif is_list_like(obj): # obj is list like object provided from flattened_spec specs. # This guarantees that it cannot be a tuple instance and # inline object modification are allowed for index in range(len(obj)): obj[index] = descend(obj[index]) return obj try: return descend(deref_spec_dict) finally: # Make sure that all memory allocated, for caching, could be released descend.cache.clear()
class Spec(object): """Represents a Swagger Specification for a service. :param spec_dict: Swagger API specification in json-like dict form :param origin_url: URL from which the spec was retrieved. :param http_client: Used to retrieve the spec via http/https. :type http_client: :class:`bravado.http_client.HTTPClient` :param config: Configuration dict. See CONFIG_DEFAULTS. """ def __init__( self, spec_dict, origin_url=None, http_client=None, config=None, ): self.spec_dict = spec_dict self.origin_url = origin_url self.http_client = http_client self.api_url = None self.config = dict(CONFIG_DEFAULTS, **(config or {})) # (key, value) = (simple format def name, Model type) # (key, value) = (#/ format def ref, Model type) self.definitions = {} # (key, value) = (simple resource name, Resource) # (key, value) = (#/ format resource ref, Resource) self.resources = None # (key, value) = (simple ref name, param_spec in dict form) # (key, value) = (#/ format ref name, param_spec in dict form) self.params = None # Built on-demand - see get_op_for_request(..) self._request_to_op_map = None # (key, value) = (format name, SwaggerFormat) self.user_defined_formats = {} self.format_checker = FormatChecker() self.resolver = RefResolver( base_uri=origin_url or '', referrer=self.spec_dict, handlers=self.get_ref_handlers(), ) # spec dict used to build resources, in case internally_dereference_refs config is enabled # it will be overridden by the dereferenced specs (by build method). More context in PR#263 self._internal_spec_dict = spec_dict def is_equal(self, other): # Not implemented as __eq__ otherwise we would need to implement __hash__ to preserve # hashability of the class and it would not necessarily be performance effective if id(self) == id(other): return True if not isinstance(other, self.__class__): return False # If self and other are of the same type but not pointing to the same memory location then we're going to inspect # all the attributes. for attr_name in set( chain( iterkeys(self.__dict__), iterkeys(other.__dict__), ), ): # Few attributes have recursive references to Spec or do not define an equality method we're going to ignore them if attr_name in { 'definitions', # Recursively point back to self (Spec instance). It is built via Spec.build so we're ignoring it 'format_checker', # jsonschema.FormatChecker does not define an equality method 'resolver', # jsonschema.validators.RefResolver does not define an equality method 'resources', # Recursively point back to self (Spec instance). It is built via Spec.build so we're ignoring it 'security_definitions', # Recursively point back to self (Spec instance). It is a cached property so ignore it }: continue try: if getattr(self, attr_name) != getattr(other, attr_name): return False except AttributeError: return False return True def __deepcopy__(self, memo=None): if memo is None: memo = {} copied_self = self.__class__(spec_dict=None) memo[id(self)] = copied_self # Copy the attributes that are built via Spec.build for attr_name, attr_value in iteritems(self.__dict__): setattr(copied_self, attr_name, deepcopy(attr_value, memo=memo)) return copied_self @cached_property def client_spec_dict(self): """Return a copy of spec_dict with x-scope metadata removed so that it is suitable for consumption by Swagger clients. You may be asking, "Why is there a difference between the Swagger spec a client sees and the one used internally?". Well, as part of the ingestion process, x-scope metadata is added to spec_dict so that $refs can be de-reffed successfully during request/response validation and marshalling. This metadata is specific to the context of the server and contains files and paths that are not relevant to the client. This is required so the client does not re-use (and in turn, re-creates) the invalid x-scope metadata created by the server. For example, a section of spec_dict that contains a ref would change as follows. Before: 'MON': { '$ref': '#/definitions/DayHours', 'x-scope': [ 'file:///happyhour/api_docs/swagger.json', 'file:///happyhour/api_docs/swagger.json#/definitions/WeekHours' ] } After: 'MON': { '$ref': '#/definitions/DayHours' } """ return strip_xscope(self.spec_dict) @classmethod def from_dict(cls, spec_dict, origin_url=None, http_client=None, config=None): """Build a :class:`Spec` from Swagger API Specification :param spec_dict: swagger spec in json-like dict form. :param origin_url: the url used to retrieve the spec, if any :type origin_url: str :param http_client: http client used to download remote $refs :param config: Configuration dict. See CONFIG_DEFAULTS. """ spec = cls(spec_dict, origin_url, http_client, config) spec.build() return spec def _validate_spec(self): if self.config['validate_swagger_spec']: self.resolver = validator20.validate_spec( spec_dict=self.spec_dict, spec_url=self.origin_url or '', http_handlers=self.get_ref_handlers(), ) def build(self): self._validate_spec() model_discovery(self) if self.config['internally_dereference_refs']: # Avoid to evaluate is_ref every time, no references are possible at this time self.deref = lambda ref_dict: ref_dict self._internal_spec_dict = self.deref_flattened_spec for user_defined_format in self.config['formats']: self.register_format(user_defined_format) self.resources = build_resources(self) self.api_url = build_api_serving_url( spec_dict=self.spec_dict, origin_url=self.origin_url, use_spec_url_for_base_path=self. config['use_spec_url_for_base_path'], ) def get_ref_handlers(self): """Get mapping from URI schemes to handlers that takes a URI. The handlers (callables) are used by the RefResolver to retrieve remote specification $refs. :returns: dict like {'http': callable, 'https': callable} :rtype: dict """ return build_http_handlers(self.http_client) def _force_deref(self, ref_dict): # type: (T) -> T """Dereference ref_dict (if it is indeed a ref) and return what the ref points to. :param ref_dict: {'$ref': '#/blah/blah'} :return: dereferenced value of ref_dict :rtype: scalar, list, dict """ if ref_dict is None or not is_ref(ref_dict): return ref_dict # Restore attached resolution scope before resolving since the # resolver doesn't have a traversal history (accumulated scope_stack) # when asked to resolve. with in_scope(self.resolver, ref_dict): reference_value = ref_dict['$ref'] # type: ignore _, target = self.resolver.resolve(reference_value) return target # NOTE: deref gets overridden, if internally_dereference_refs is enabled, after calling build deref = _force_deref def get_op_for_request(self, http_method, path_pattern): """Return the Swagger operation for the passed in request http method and path pattern. Makes it really easy for server-side implementations to map incoming requests to the Swagger spec. :param http_method: http method of the request :param path_pattern: request path pattern. e.g. /foo/{bar}/baz/{id} :returns: the matching operation or None if a match couldn't be found :rtype: :class:`bravado_core.operation.Operation` """ if self._request_to_op_map is None: # lazy initialization self._request_to_op_map = {} base_path = self.spec_dict.get('basePath', '').rstrip('/') for resource in self.resources.values(): for op in resource.operations.values(): full_path = base_path + op.path_name key = (op.http_method, full_path) self._request_to_op_map[key] = op key = (http_method.lower(), path_pattern) return self._request_to_op_map.get(key) def register_format(self, user_defined_format): """Registers a user-defined format to be used with this spec. :type user_defined_format: :class:`bravado_core.formatter.SwaggerFormat` """ name = user_defined_format.format self.user_defined_formats[name] = user_defined_format validate = return_true_wrapper(user_defined_format.validate) self.format_checker.checks(name, raises=(SwaggerValidationError, ))(validate) def get_format(self, name): # type: (typing.Text) -> SwaggerFormat """ :param name: Name of the format to retrieve :rtype: :class:`bravado_core.formatter.SwaggerFormat` """ user_defined_format = self.user_defined_formats.get(name) if user_defined_format is None: user_defined_format = formatter.DEFAULT_FORMATS.get(name) if name == 'byte' and self.config['use_base64_for_byte_format']: user_defined_format = formatter.BASE64_BYTE_FORMAT if user_defined_format is None: warnings.warn( message='{0} format is not registered with bravado-core!'. format(name), category=Warning, ) return user_defined_format @cached_property def security_definitions(self): security_defs = {} for security_name, security_def in iteritems( self.spec_dict.get('securityDefinitions', {})): security_defs[security_name] = SecurityDefinition( self, security_def) return security_defs @cached_property def flattened_spec(self): """ Representation of the current swagger specs that could be written to a single file. :rtype: dict """ if not self.config['validate_swagger_spec']: log.warning( 'Flattening unvalidated specs could produce invalid specs. ' 'Use it at your risk or enable `validate_swagger_specs`', ) return strip_xscope(spec_dict=flattened_spec(swagger_spec=self), ) @cached_property def deref_flattened_spec(self): deref_spec_dict = JsonRef.replace_refs(self.flattened_spec) @memoize_by_id def descend(obj): # Inline modification of obj # This method is needed because JsonRef could produce performance penalties in accessing # the proxied attributes if isinstance(obj, JsonRef): # Extract the proxied value # http://jsonref.readthedocs.io/en/latest/#jsonref.JsonRef.__subject__ return obj.__subject__ if is_dict_like(obj): for key in list(iterkeys(obj)): obj[key] = descend(obj=obj[key]) elif is_list_like(obj): # obj is list like object provided from flattened_spec specs. # This guarantees that it cannot be a tuple instance and # inline object modification are allowed for index in range(len(obj)): obj[index] = descend(obj=obj[index]) return obj try: return descend(obj=deref_spec_dict) finally: # Make sure that all memory allocated, for caching, could be released descend.cache.clear()
class Spec(object): """Represents a Swagger Specification for a service. :param spec_dict: Swagger API specification in json-like dict form :param origin_url: URL from which the spec was retrieved. :param http_client: Used to retrive the spec via http/https. :type http_client: :class:`bravado.http_client.HTTPClient` :param config: Configuration dict. See CONFIG_DEFAULTS. """ def __init__(self, spec_dict, origin_url=None, http_client=None, config=None): self.spec_dict = spec_dict self.origin_url = origin_url self.http_client = http_client self.api_url = None self.config = dict(CONFIG_DEFAULTS, **(config or {})) # Cached copy of spec_dict with x-scope metadata removed. # See @property client_spec_dict(). self._client_spec_dict = None # (key, value) = (simple format def name, Model type) # (key, value) = (#/ format def ref, Model type) self.definitions = {} # (key, value) = (simple resource name, Resource) # (key, value) = (#/ format resource ref, Resource) self.resources = None # (key, value) = (simple ref name, param_spec in dict form) # (key, value) = (#/ format ref name, param_spec in dict form) self.params = None # Built on-demand - see get_op_for_request(..) self._request_to_op_map = None # (key, value) = (format name, SwaggerFormat) self.user_defined_formats = {} self.format_checker = FormatChecker() self.resolver = RefResolver(base_uri=origin_url or '', referrer=self.spec_dict, handlers=build_http_handlers(http_client)) # generated by @property to avoid multiple swagger validations self._security_definitions = None @property def client_spec_dict(self): """Return a copy of spec_dict with x-scope metadata removed so that it is suitable for consumption by Swagger clients. You may be asking, "Why is there a difference between the Swagger spec a client sees and the one used internally?". Well, as part of the ingestion process, x-scope metadata is added to spec_dict so that $refs can be de-reffed successfully during requset/response validation and marshalling. This medatadata is specific to the context of the server and contains files and paths that are not relevant to the client. This is required so the client does not re-use (and in turn, re-creates) the invalid x-scope metadata created by the server. For example, a section of spec_dict that contains a ref would change as folows. Before: 'MON': { '$ref': '#/definitions/DayHours', 'x-scope': [ 'file:///happyhour/api_docs/swagger.json', 'file:///happyhour/api_docs/swagger.json#/definitions/WeekHours' ] } After: 'MON': { '$ref': '#/definitions/DayHours' } """ if self._client_spec_dict is None: self._client_spec_dict = strip_xscope(self.spec_dict) return self._client_spec_dict @classmethod def from_dict(cls, spec_dict, origin_url=None, http_client=None, config=None): """Build a :class:`Spec` from Swagger API Specificiation :param spec_dict: swagger spec in json-like dict form. :param origin_url: the url used to retrieve the spec, if any :type origin_url: str :param: http_client: http client used to download remote $refs :param config: Configuration dict. See CONFIG_DEFAULTS. """ spec = cls(spec_dict, origin_url, http_client, config) spec.build() return spec def build(self): if self.config['validate_swagger_spec']: self.resolver = validator20.validate_spec( self.spec_dict, spec_url=self.origin_url or '', http_handlers=build_http_handlers(self.http_client)) post_process_spec(self, on_container_callbacks=[ functools.partial(tag_models, visited_models={}, swagger_spec=self), functools.partial(collect_models, models=self.definitions, swagger_spec=self) ]) for format in self.config['formats']: self.register_format(format) self.api_url = build_api_serving_url(self.spec_dict, self.origin_url) self.resources = build_resources(self) def deref(self, ref_dict): """Dereference ref_dict (if it is indeed a ref) and return what the ref points to. :param ref_dict: {'$ref': '#/blah/blah'} :return: dereferenced value of ref_dict :rtype: scalar, list, dict """ if ref_dict is None or not is_ref(ref_dict): return ref_dict # Restore attached resolution scope before resolving since the # resolver doesn't have a traversal history (accumulated scope_stack) # when asked to resolve. with in_scope(self.resolver, ref_dict): log.debug('Resolving {0} with scope {1}: {2}'.format( ref_dict['$ref'], len(self.resolver._scopes_stack), self.resolver._scopes_stack)) _, target = self.resolver.resolve(ref_dict['$ref']) return target def get_op_for_request(self, http_method, path_pattern): """Return the Swagger operation for the passed in request http method and path pattern. Makes it really easy for server-side implementations to map incoming requests to the Swagger spec. :param http_method: http method of the request :param path_pattern: request path pattern. e.g. /foo/{bar}/baz/{id} :returns: the matching operation or None if a match couldn't be found :rtype: :class:`bravado_core.operation.Operation` """ if self._request_to_op_map is None: # lazy initialization self._request_to_op_map = {} base_path = self.spec_dict.get('basePath', '').rstrip('/') for resource in self.resources.values(): for op in resource.operations.values(): full_path = base_path + op.path_name key = (op.http_method, full_path) self._request_to_op_map[key] = op key = (http_method.lower(), path_pattern) return self._request_to_op_map.get(key) def register_format(self, user_defined_format): """Registers a user-defined format to be used with this spec. :type user_defined_format: :class:`bravado_core.formatter.SwaggerFormat` """ name = user_defined_format.format self.user_defined_formats[name] = user_defined_format validate = return_true_wrapper(user_defined_format.validate) self.format_checker.checks(name, raises=(SwaggerValidationError, ))(validate) def get_format(self, name): """ :param name: Name of the format to retrieve :rtype: :class:`bravado_core.formatters.SwaggerFormat` """ if name in formatter.DEFAULT_FORMATS: return formatter.DEFAULT_FORMATS[name] format = self.user_defined_formats.get(name) if format is None: warnings.warn( '{0} format is not registered with bravado-core!'.format(name), Warning) return format @property def security_definitions(self): if self._security_definitions is None: self._security_definitions = {} for security_name, security_def in iteritems( self.spec_dict.get('securityDefinitions', {})): self._security_definitions[security_name] = SecurityDefinition( self, security_def) return self._security_definitions
class Spec(object): """Represents a Swagger Specification for a service. :param spec_dict: Swagger API specification in json-like dict form :param origin_url: URL from which the spec was retrieved. :param http_client: Used to retrive the spec via http/https. :type http_client: :class:`bravado.http_client.HTTPClient` :param config: Configuration dict. See CONFIG_DEFAULTS. """ def __init__(self, spec_dict, origin_url=None, http_client=None, config=None): self.spec_dict = spec_dict self.origin_url = origin_url self.http_client = http_client self.api_url = None self.config = dict(CONFIG_DEFAULTS, **(config or {})) # (key, value) = (simple format def name, Model type) # (key, value) = (#/ format def ref, Model type) self.definitions = {} # (key, value) = (simple resource name, Resource) # (key, value) = (#/ format resource ref, Resource) self.resources = None # (key, value) = (simple ref name, param_spec in dict form) # (key, value) = (#/ format ref name, param_spec in dict form) self.params = None # Built on-demand - see get_op_for_request(..) self._request_to_op_map = None # (key, value) = (format name, SwaggerFormat) self.user_defined_formats = {} self.format_checker = FormatChecker() self.resolver = RefResolver(base_uri=origin_url or '', referrer=self.spec_dict, handlers=build_http_handlers(http_client)) @classmethod def from_dict(cls, spec_dict, origin_url=None, http_client=None, config=None): """Build a :class:`Spec` from Swagger API Specificiation :param spec_dict: swagger spec in json-like dict form. :param origin_url: the url used to retrieve the spec, if any :type origin_url: str :param config: Configuration dict. See CONFIG_DEFAULTS. """ spec = cls(spec_dict, origin_url, http_client, config) spec.build() return spec def build(self): if self.config['validate_swagger_spec']: self.resolver = validator20.validate_spec( self.spec_dict, spec_url=self.origin_url or '', http_handlers=build_http_handlers(self.http_client)) post_process_spec(self, on_container_callbacks=[ functools.partial(tag_models, visited_models={}, swagger_spec=self), functools.partial(collect_models, models=self.definitions, swagger_spec=self) ]) for format in self.config['formats']: self.register_format(format) self.api_url = build_api_serving_url(self.spec_dict, self.origin_url) self.resources = build_resources(self) def deref(self, ref_dict): """Dereference ref_dict (if it is indeed a ref) and return what the ref points to. :param ref_dict: {'$ref': '#/blah/blah'} :return: dereferenced value of ref_dict :rtype: scalar, list, dict """ if ref_dict is None or not is_ref(ref_dict): return ref_dict # Restore attached resolution scope before resolving since the # resolver doesn't have a traversal history (accumulated scope_stack) # when asked to resolve. with in_scope(self.resolver, ref_dict): log.debug('Resolving {0} with scope {1}: {2}'.format( ref_dict['$ref'], len(self.resolver._scopes_stack), self.resolver._scopes_stack)) _, target = self.resolver.resolve(ref_dict['$ref']) return target def get_op_for_request(self, http_method, path_pattern): """Return the Swagger operation for the passed in request http method and path pattern. Makes it really easy for server-side implementations to map incoming requests to the Swagger spec. :param http_method: http method of the request :param path_pattern: request path pattern. e.g. /foo/{bar}/baz/{id} :returns: the matching operation or None if a match couldn't be found :rtype: :class:`bravado_core.operation.Operation` """ if self._request_to_op_map is None: # lazy initialization self._request_to_op_map = {} base_path = self.spec_dict.get('basePath', '').rstrip('/') for resource in self.resources.values(): for op in resource.operations.values(): full_path = base_path + op.path_name key = (op.http_method, full_path) self._request_to_op_map[key] = op key = (http_method.lower(), path_pattern) return self._request_to_op_map.get(key) def register_format(self, user_defined_format): """Registers a user-defined format to be used with this spec. :type user_defined_format: :class:`bravado_core.formatter.SwaggerFormat` """ name = user_defined_format.format self.user_defined_formats[name] = user_defined_format validate = return_true_wrapper(user_defined_format.validate) self.format_checker.checks(name, raises=(SwaggerValidationError, ))(validate) def get_format(self, name): """ :param name: Name of the format to retrieve :rtype: :class:`bravado_core.formatters.SwaggerFormat` """ if name in formatter.DEFAULT_FORMATS: return formatter.DEFAULT_FORMATS[name] format = self.user_defined_formats.get(name) if format is None: warnings.warn( '{0} format is not registered with bravado-core!'.format(name), Warning) return format
class OpenAPISpec: resource_spec_cls = ResourceSpec @classmethod def from_file(cls, filename): # check if filename is a url if filename.startswith('http://') or \ filename.startswith('https://'): url, filename = filename, None ext = '.json' if url.endswith('.json') else '.ext' with tempfile.NamedTemporaryFile(suffix=ext) as f: filename, _ = urllib.request.urlretrieve(url, f.name) spec_dict = cls.load_spec_from_file(filename) else: spec_dict = cls.load_spec_from_file(filename) return cls(spec_dict) @staticmethod def load_spec_from_file(filename): with open(filename) as f: if filename.endswith('.json'): return json.load(f) else: return yaml.safe_load(f) def __init__(self, spec_dict, spec_compat_func=compat_jsonschema): self.spec_dict = spec_compat_func(spec_dict) self.resources = self._initialize_resources() self.ref_resolver = RefResolver('', self.spec_dict) def _initialize_resources(self): resources = OrderedDict() for path in self.spec_dict['paths']: resource_spec = self.resource_spec_cls(path, self) resources[resource_spec.url_rule] = resource_spec return resources @property def operations(self): for resource in self.resources.values(): yield from resource.operations.values() def get_operation_spec(self, path, method): # delete ending backslash or query string path = re.sub(r'/?([?#].*)?$', '', path) for url_rule, resource in self.resources.items(): if url_rule.match(path): return resource.operations.get(method.lower()) def resolve_ref(self, schema): schema = deepcopy(schema) def _resolve_ref(schema): """find all ref""" if isinstance(schema, dict): if '$ref' in schema: schema = self.ref_resolver.resolve(schema['$ref'])[1] schema = _resolve_ref(schema) else: schema = { key: _resolve_ref(value) for key, value in schema.items() } elif isinstance(schema, list): schema = [_resolve_ref(item) for item in schema] return schema return _resolve_ref(schema)