def as_dict(self): ''' Output the specification as a serializable ``dict``. :returns: the full Swagger specification in a serializable format :rtype: dict ''' basepath = self.api.base_path if len(basepath) > 1 and basepath.endswith('/'): basepath = basepath[:-1] infos = { 'title': _v(self.api.title), 'version': _v(self.api.version), } if self.api.description: infos['description'] = _v(self.api.description) if self.api.terms_url: infos['termsOfService'] = _v(self.api.terms_url) if self.api.contact and (self.api.contact_email or self.api.contact_url): infos['contact'] = { 'name': _v(self.api.contact), 'email': _v(self.api.contact_email), 'url': _v(self.api.contact_url), } if self.api.license: infos['license'] = {'name': _v(self.api.license)} if self.api.license_url: infos['license']['url'] = _v(self.api.license_url) paths = {} tags = self.extract_tags(self.api) # register errors responses = self.register_errors() for ns in self.api.namespaces: for resource, urls, kwargs in ns.resources: for url in self.api.ns_urls(ns, urls): paths[extract_path(url)] = self.serialize_resource( ns, resource, url, kwargs) specs = { 'swagger': '2.0', 'basePath': basepath, 'paths': not_none_sorted(paths), 'info': infos, 'produces': list(iterkeys(self.api.representations)), 'consumes': ['application/json'], 'securityDefinitions': self.api.authorizations or None, 'security': self.security_requirements(self.api.security) or None, 'tags': tags, 'definitions': self.serialize_definitions() or None, 'responses': responses or None, 'host': self.get_host(), 'schemes': self.get_schemes() } return not_none(specs)
def as_dict(self): basepath = self.api.base_path if len(basepath) > 1 and basepath.endswith('/'): basepath = basepath[:-1] infos = { 'title': self.api.title, 'version': self.api.version, } if self.api.description: infos['description'] = self.api.description if self.api.terms_url: infos['termsOfService'] = self.api.terms_url if self.api.contact and (self.api.contact_email or self.api.contact_url): infos['contact'] = { 'name': self.api.contact, 'email': self.api.contact_email, 'url': self.api.contact_url, } if self.api.license: infos['license'] = {'name': self.api.license} if self.api.license_url: infos['license']['url'] = self.api.license_url paths = {} tags = [] for ns in self.api.namespace_registry.values(): tags.append({ 'name': ns.name, 'description': ns.description }) for resource, urls, kwargs in ns.resources: for url in urls: # Formerly, when building the `paths` collection, this code would only keep the last serialized # resource for a given normalized URL. Now it'll correctly maintain one per URL per request method. norm_path = extract_path(url) serialized_resource = self.serialize_resource(ns, resource, url) paths.setdefault(norm_path, {}).update(serialized_resource) specs = { 'swagger': '2.0', 'basePath': basepath, 'paths': not_none(paths), 'info': infos, 'produces': list(self.api.representations.keys()), 'consumes': ['application/json'], 'securityDefinitions': self.api.authorizations or None, 'security': self.security_requirements(self.api.security) or None, 'tags': tags, 'definitions': self.serialize_definitions() or None, } return not_none(specs)
def test_extract_path_with_multiple_parameters(self): path = '/test/<parameter>/<string:other>/' self.assertEqual(extract_path(path), '/test/{parameter}/{other}/')
def test_extract_path_with_a_single_typed_parameter(self): path = '/test/<string:parameter>' self.assertEqual(extract_path(path), '/test/{parameter}')
def test_extract_path_with_a_single_simple_parameter(self): path = '/test/<parameter>' self.assertEqual(extract_path(path), '/test/{parameter}')
def test_extract_static_path(self): path = '/test' self.assertEqual(extract_path(path), '/test')
def test_extract_path_with_a_single_typed_parameter_with_arguments(self): path = '/test/<string(length=2):parameter>' self.assertEqual(extract_path(path), '/test/{parameter}')
def test_extract_static_path(self): path = '/test' assert extract_path(path) == '/test'
def test_extract_path_with_multiple_parameters(self): path = '/test/<parameter>/<string:other>/' assert extract_path(path) == '/test/{parameter}/{other}/'
def test_extract_path_with_a_single_typed_parameter(self): path = '/test/<string:parameter>' assert extract_path(path) == '/test/{parameter}'
def test_extract_path_with_a_single_simple_parameter(self): path = '/test/<parameter>' assert extract_path(path) == '/test/{parameter}'
def test_extract_static_path(self): path = "/test" self.assertEqual(extract_path(path), "/test")