def _process_params(fsm_obj, ptype, required_func=None): # get buffer and conver # first merge lines without -- to previous lines if required_func is None: required_func = lambda x, y: x == y lines = fsm_obj.buffer.splitlines() cleaned_lines = _clean_lines(lines) params = {} # parse the lines for i, line in enumerate(cleaned_lines): matcher = PARAM_MATCHER.match(line.lstrip()) if not matcher: continue open_type = matcher.group('type') param = Param( name=matcher.group('name'), ptype=ptype, required=required_func( str(matcher.group('required')).lower(), "required"), order=i, ) description = str(matcher.group('description')).strip() desc, kwargs = _get_description_props(description) param.description = desc.strip() param.type = Type(open_type, **kwargs) params[param.name] = param return params
def test_enum(): """Test array""" cases = [ ("Test 1", "enum[a,b,c]", {}, {"type":"string", "enum": ["a", "b", "c"]}) ] for name, inp, kwargs, result in cases: typ = Type(inp, **kwargs) assert typ.schema == result, name
def test_array(): """Test array""" cases = [ ("Test 1", "[integer]", {}, {"type":"array", "items": {"type":"integer"}}) ] for name, inp, kwargs, result in cases: typ = Type(inp, **kwargs) assert typ.schema == result, name
def test_combine_types(): cases = [ ("Test 1", "anyOf[A,B,C]", {}, {"anyOf": [{"$ref":"#/components/schemas/A"},{"$ref":"#/components/schemas/B"},{"$ref":"#/components/schemas/C"}]}), ("Test 2", "allOf[A,B,C]", {}, {"allOf": [{"$ref":"#/components/schemas/A"},{"$ref":"#/components/schemas/B"},{"$ref":"#/components/schemas/C"}]}), ("Test 3", "oneOf[A,B,C]", {}, {"oneOf": [{"$ref":"#/components/schemas/A"},{"$ref":"#/components/schemas/B"},{"$ref":"#/components/schemas/C"}]}), ] for name, inp, kwargs, result in cases: typ = Type(inp, **kwargs) assert typ.schema == result, name
def test_strings(): """Test strings""" cases = [ ("Test 1", "str", {}, {"type": "string"}), ("Test 2", "string", {}, {"type": "string"}), ("Test 3", "str:date", {}, {"type": "string", "format": "date"}), ("Test 4", "uuid", {}, {"type": "string", "format": "uuid"}) ] for name, inp, kwargs, result in cases: typ = Type(inp, **kwargs) assert typ.schema == result, name
def test_floats(): """Test floats""" cases = [ ("Simple float1", "number", {}, {"type":"number"}), ("Simple float2", "float", {}, {"type":"number", "format":"float"}), ("Simple float3", "double", {}, {"type":"number", "format":"double"}), ("Simple float4", "number:float", {"minimum": 1.0}, {"type":"number", "format":"float", "minimum":1.0}), ] for name, inp, kwargs, result in cases: typ = Type(inp, **kwargs) assert typ.schema == result, name
def test_ints(): """Test int type""" cases = [ #name, inp value, kwargs, dict schema result ("Simple int1", "int",{},{"type":"integer"}), ("Simple int2", "integer", {}, {"type":"integer"}), ("Simple int3", "int32", {}, {"type":"integer", "format":"int32"}), ("Simple int4", "long", {}, {"type":"integer", "format":"int64"}), ("simple int5 with kwargs", "integer", {"minimum": 10}, {"type":"integer", "minimum": 10}), ("simple int5 with format kwargs", "integer:int32", {"minimum": 10}, {"type":"integer", "format":"int32", "minimum": 10}), ] for name, inp, kwargs, result in cases: typ = Type(inp, **kwargs) assert typ.schema == result, name
def _process_tags(fsm_obj, **kwargs): fsm_obj.spec.tags = _process_params(fsm_obj, "tags") _set_default_type(fsm_obj.spec.tags, Type("string")) fsm_obj.buffer = ""
def _process_properties(fsm_obj, **kwargs): fsm_obj.spec.properties = _process_params(fsm_obj, "property") _set_default_type(fsm_obj.spec.properties, Type("string")) fsm_obj.buffer = ""
def _process_header(fsm_obj, **kwargs): fsm_obj.spec.header_params = _process_params(fsm_obj, "header") #convert all types to string if None _set_default_type(fsm_obj.spec.header_params, Type("string")) fsm_obj.buffer = ""
def _process_cookie(fsm_obj, **kwargs): fsm_obj.spec.cookie_params = _process_params(fsm_obj, "cookie") _set_default_type(fsm_obj.spec.cookie_params, Type("string")) fsm_obj.buffer = ""
def _process_body(fsm_obj, **kwargs): fsm_obj.spec.body_params = _process_params(fsm_obj, "body", lambda x, y: True) #check the params and guess the content type _set_default_type(fsm_obj.spec.body_params, Type("string")) fsm_obj.buffer = ''
def _process_query(fsm_obj, **kwargs): fsm_obj.spec.query_params = _process_params(fsm_obj, "query") _set_default_type(fsm_obj.spec.query_params, Type("string")) fsm_obj.buffer = ""