def test_process_data(): data = { 'paths': { '/users': { 'get': {}, 'put': {}, 'head': {}, 'parameters': [] }, '/posts/{post_id}': { 'get': { 'parameters': [{ 'name': 'post_id', 'in': 'path', 'type': 'integer' }, { 'name': 'page', 'in': 'query', 'type': 'integer' }] } } } } swagger = Swagger(data) generator = FalconGenerator(swagger) schemas, routes, view1, view2 = list(generator.generate())[:4] view1, view2 = sorted([view1, view2], key=lambda x: x.data['name']) assert ('posts_post_id', 'GET') in schemas.data['validators'] assert schemas.data['validators'][( 'posts_post_id', 'GET')]['params']['properties']['page']['type'] == 'integer' assert view1.data['url'] == '/posts/{post_id}' assert view1.data['name'] == 'PostsPostId'
def test_swagger_ref_count_01(): data = { 'definitions': { 'Product': { 'properties': { 'price': { 'type': 'number' }, 'name': { 'type': 'string' }, 'seller': { '$ref': '#/definitions/User' } } }, 'User': { 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'number' }, } } } } swagger = Swagger(data) assert swagger.definitions[0] == ('definitions', 'User') assert swagger.definitions[1] == ('definitions', 'Product')
def test_schema_ref_01(): data = { 'definitions': { 'Product': { 'properties': { 'price': { 'type': 'number' }, 'name': { 'type': 'string' }, 'seller': { '$ref': '#/definitions/User' } } }, 'User': { 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'number' }, } } } } swagger = Swagger(data) data = build_data(swagger) print("!!!!!!!!!!!!!!!!!!", data['definitions']['definitions']) assert len(data['definitions']['definitions']) == 2
def test_swagger_ref_count_03(): data = { 'definitions': { 'Order': { 'properties': { 'customer': { '$ref': '#/definitions/User' }, 'seller': { '$ref': '#/definitions/User' }, 'products': { 'type': 'array', 'items': { '$ref': '#/definitions/Product' } } } }, 'Product': { 'properties': { 'price': { 'type': 'number' }, 'name': { 'type': 'string' }, 'seller': { '$ref': '#/definitions/User' } } }, 'User': { 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'number' }, } }, 'OrderList': { 'properties': { 'orders': { '$ref': '#/definitions/Order' } } } } } swagger = Swagger(data) assert swagger.definitions[0] == ('definitions', 'User') assert swagger.definitions[1] == ('definitions', 'Product') assert swagger.definitions[2] == ('definitions', 'Order') assert swagger.definitions[3] == ('definitions', 'OrderList')
def test_swagger_ref_count_04(): data = { 'definitions': { 'Order': { 'properties': { 'customer': { '$ref': '#/definitions/User' }, 'seller': { '$ref': '#/definitions/User' }, 'products': { 'type': 'array', 'items': { '$ref': '#/definitions/Product' } } } }, 'Product': { 'properties': { 'price': { 'type': 'number' }, 'name': { 'type': 'string' }, 'seller': { '$ref': '#/definitions/User' } } }, 'User': { 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'number' }, 'orders': { 'type': 'array', 'items': { '$ref': '#/definitions/Order' } } } } } } with pytest.raises(ValueError) as excinfo: Swagger(data) assert excinfo.type == exceptions.ValueError
def test_schema_base_02(): data = { 'definitions': { 'Product': { 'properties': { 'price': {'type': 'number'}, 'name': {'type': 'string'}, } } } } swagger = Swagger(data) data = build_data(swagger) assert len(data['schemas']) == 1
def test_swagger_ref_node(): data = { 'definitions': { 'Order': { 'properties': { 'customer': { '$ref': '#/definitions/User' }, 'seller': { '$ref': '#/definitions/User' }, 'products': { 'type': 'array', 'items': { '$ref': '#/definitions/Product' } } } }, 'Product': { 'properties': { 'price': { 'type': 'number' }, 'name': { 'type': 'string' }, 'seller': { '$ref': '#/definitions/User' } } }, 'User': { 'properties': { 'name': { 'type': 'string' }, 'age': { 'type': 'number' }, } } } } swagger = Swagger(data) p = swagger.get( ['definitions', 'Order', 'properties', 'products', 'items']) assert p['properties']['name']['type'] == 'string' assert str(p) == 'DefinitionsProduct' assert repr(p) == 'DefinitionsProduct'
def test_schema_base_03(): data = { 'parameters': { "limitParam": { "name": "limit", "in": "query", "description": "max records to return", "required": True, "type": "integer", "format": "int32" } } } swagger = Swagger(data) data = build_data(swagger) assert len(data['schemas']) == 0
def test_swagger_ref_count_04(): data = { 'definitions': { 'Order': { 'properties': { 'customer': { '$ref': '#/definitions/User' }, 'seller': { '$ref': '#/definitions/User' }, 'products': { 'type': 'array', 'items': { '$ref': '#/definitions/Product' } } } }, 'Product': { 'properties': { 'price': {'type': 'number'}, 'name': {'type': 'string'}, 'seller': { '$ref': '#/definitions/User' } } }, 'User': { 'properties': { 'name': {'type': 'string'}, 'age': {'type': 'number'}, 'orders': { 'type': 'array', 'items': { '$ref': '#/definitions/Order' } } } } } } Swagger(data)
def test_schema_ref_01(): data = { 'definitions': { 'Product': { 'properties': { 'price': {'type': 'number'}, 'name': {'type': 'string'}, 'seller': { '$ref': '#/definitions/User' } } }, 'User': { 'properties': { 'name': {'type': 'string'}, 'age': {'type': 'number'}, } } } } swagger = Swagger(data) data = build_data(swagger) assert len(data['schemas']) == 2 assert list(data['schemas'].keys())[0] == 'DefinitionsUser'
def _get_oauth_scopes(self): for path, scopes in self.swagger.search( ('securityDefinitions', '*', 'scopes')): return scopes return None def _process(self): views = self._process_data() for view in views: print(view) def generate(self): for clz in self.dependencies: dependence = clz(self.swagger) g = dependence.generate() for code in g: a = self._dependence_callback(code).data["validators"] if __name__ == "__main__": import yaml tmp = "/Users/sara/Desktop/payment.yaml" with open(tmp, encoding='utf8') as f: data = yaml.load(f) swagger = Swagger(data) interface = InterfaceGenerator(swagger) interface.generate()
def test_validators(): data = { 'paths': { '/users': { 'parameters': [ {'name': 'page', 'in': 'query', 'type': 'number', 'required': True}, {'name': 'limit', 'in': 'query', 'type': 'number'} ], 'post': { 'parameters': [ {'name': 'user', 'in': 'body', 'schema': {'$ref': '#/definitions/User'}} ] }, 'put': { 'parameters': [ {'name': 'user', 'in': 'body', 'schema': { 'properties': { 'id': { 'type': 'number' } } }} ] } } }, 'definitions': { 'Product': { 'properties': { 'price': {'type': 'number'}, 'name': {'type': 'string'}, 'seller': { '$ref': '#/definitions/User' } } }, 'User': { 'properties': { 'name': {'type': 'string'}, 'age': {'type': 'number'}, } } } } swagger = Swagger(data) data = build_data(swagger) schemas = data['schemas'] validators = data['validators'] # body parameters assert ('/users', 'POST') in validators v1 = validators[('/users', 'POST')]['body'] assert v1 == schemas['DefinitionsUser'] # query parameters v2 = validators[('/users', 'POST')]['query'] assert v2 == dict( required=['page'], properties=dict( page=dict(type='number'), limit=dict(type='number'))) # no others parameters assert 'path' not in validators[('/users', 'POST')] # definitions assert 'DefinitionsUser' in schemas assert 'DefinitionsProduct' in schemas assert len(schemas) == 2
def test_schema_base_01(): data = {} swagger = Swagger(data) data = build_data(swagger) assert len(data['schemas']) == 0
def test_filters(): data = { 'paths': { '/users': { 'parameters': [ {'name': 'page', 'in': 'query', 'type': 'number', 'required': True}, {'name': 'limit', 'in': 'query', 'type': 'number'} ], 'post': { 'parameters': [ {'name': 'user', 'in': 'body', 'schema': {'$ref': '#/definitions/User'}} ], 'responses': { '201': { 'schema': {'$ref': '#/definitions/User'}, 'headers': {'X-COUNT': {'type': 'number'}}, 'examples': {'application/json': {'id': 1, 'name': 'Bob', 'age': 12}} }, '422': { 'schema': { 'properties': { 'code': {'type': 'string'}, 'message': {'type': 'string'} } } } } } } }, 'definitions': { 'Product': { 'properties': { 'price': {'type': 'number'}, 'name': {'type': 'string'}, 'seller': { '$ref': '#/definitions/User' } } }, 'User': { 'properties': { 'name': {'type': 'string'}, 'age': {'type': 'number'}, } } } } swagger = Swagger(data) data = build_data(swagger) schemas = data['schemas'] filters = data['filters'] assert 201 in filters[('/users', 'POST')] assert 422 in filters[('/users', 'POST')] r1 = filters[('/users', 'POST')][201] r2 = filters[('/users', 'POST')][422] assert r1['schema'] == schemas['DefinitionsUser'] assert r2['schema']['properties']['code'] == {'type': 'string'}
def build_data(data): swagger = Swagger(data) validators = OrderedDict( ) # (endpoint, method) = {'body': schema_name or schema, 'query': schema_name, ..} filters = OrderedDict( ) # (endpoint, method) = {'200': {'schema':, 'headers':, 'examples':}, 'default': ..} scopes = OrderedDict() # (endpoint, method) = [scope_a, scope_b] operationId = OrderedDict() tags = OrderedDict() desc = OrderedDict() schemas = OrderedDict([(schema_var_name(path), swagger.get(path)) for path in swagger.definitions]) # path parameters for path, _ in swagger.search(['paths', '*']): # methods for p, data in swagger.search(path + ('*', )): if p[-1] not in [ 'get', 'post', 'put', 'delete', 'patch', 'options', 'head' ]: continue endpoint = p[1] # p: ('paths', '/some/path', 'method') method = p[-1].upper() # parameters as schema parameters = data.get('parameters') params = [] # 这边有点小问题,array怎么搞? if parameters: for param in parameters: if param.get( "name") == "body" and "schema" in param.keys(): values = list(param["schema"]["properties"].values()) for value in values: value["in"] = "body" params += values del schemas[str(param["schema"])] else: params.append(param) validators[(endpoint, method)] = params # responses responses = data.get('responses') if responses: filter = {} for status, res_data in six.iteritems(responses): if isinstance(status, int) or status.isdigit(): filter[int(status)] = dict( headers=res_data.get('headers'), schema=res_data.get('schema')) filters[(endpoint, method)] = filter # operationId name = data.get('operationId') if name in ["", None]: name = "{}_{}".format(method, endpoint.replace("/", "_")) operationId[(endpoint, method)] = name # tags tag = data.get('tags') if len(tag) < 1: tag = u"未分配" else: tag = tag[0] tags[(endpoint, method)] = tag # summary and description desc[(endpoint, method)] = "{0} {1}".format(data.get("summary"), data.get("description")) # scopes securitys = data.get('security') or [] if len(securitys) == 0: scopes[(endpoint, method)] = [] else: for security in securitys: scopes[(endpoint, method)] = list(security.keys()).pop() break data = dict( schemas=schemas, validators=validators, filters=filters, scopes=scopes, operationId=operationId, tags=tags, desc=desc # merge_default=getsource(merge_default), # normalize=getsource(normalize) ) return data