def get_routes(self): """ Parse the Swagger file, and collects all routes :return: Array<String> where each string is "route = OP_KEY" format """ routes = [] paths = self.spec.get(constants.PATHS, {}) for path, config in paths.items(): for method in config.keys(): if method in constants.VALID_METHODS: # Ignore Exclude URLs op_key = f"{method.upper()} {path}" identifier = re.sub(r'[.-]', '_', utils.operation_id_name(path, method)) routes.append(f"{identifier.upper()} = '{op_key}'") return "\n".join(routes)
def parse(self): for url, path_config in self.specs.get(swagger_constants.PATHS, {}).items(): parameters = path_config.get(swagger_constants.PARAMETERS) common_resources = set() if parameters: common_resources = self.parse_params(parameters, url) for method, method_config in path_config.items(): method_resources = set() if method in swagger_constants.VALID_METHODS: parameters = method_config.get( swagger_constants.PARAMETERS) # Detect Operation ID in swagger, and if not present, generate and write back in Swagger # Operation IDs are used as primary key throughout application op_id = method_config.get(swagger_constants.OPERATION) if not op_id: method_config[swagger_constants. OPERATION] = utils.operation_id_name( url, method) if parameters: method_resources = self.parse_params(parameters, url) all_resources = common_resources.union(method_resources) if len(all_resources) > 1: method_config[swagger_constants. DEPENDENT_RESOURCES] = all_resources for ref_name, ref_config in self.specs.get( swagger_constants.DEFINITIONS, {}).items(): self.parse_reference(ref_name, ref_config)
def test_read_method(self): assert utils.operation_id_name("x/{id}/y/{id}", constants.GET) == "x_PARAM_1_y_PARAM_2_read"
def test_list_method(self): assert utils.operation_id_name("x/{id}/y", constants.GET) == "x_PARAM_1_y_list"
def test_create_method(self): assert utils.operation_id_name("x/{id}/y", constants.POST) == "x_PARAM_1_y_create"
def test_delete_method(self): assert utils.operation_id_name("x/{id}/y/{id}", constants.DELETE) == "x_PARAM_1_y_PARAM_2_delete"
def test_patch_method(self): assert utils.operation_id_name("x/{id}/y/{id}", constants.PATCH) == "x_PARAM_1_y_PARAM_2_partial_update"
def test_update_method(self): assert utils.operation_id_name("x/{id}/y/{id}", constants.PUT) == "x_PARAM_1_y_PARAM_2_update"