예제 #1
0
def camelcase_parameters(func: Any) -> Any:
    func_sig = signature(func)
    parameters = list(func_sig.parameters.values())
    start_index = -1
    for i, parameter in enumerate(parameters):
        if (parameter.default and isinstance(parameter.default,
                                             (params.Query, params.Path))
                and parameter.default.alias is None):
            if start_index < 0:
                start_index = i
            parameter.default.alias = snake2camel(parameter.name,
                                                  start_lower=True)

    if start_index >= 0:
        parameters.insert(
            start_index,
            Parameter(
                "camelcase_parameters_dependency",
                kind=Parameter.POSITIONAL_OR_KEYWORD,
                default=Depends(camelcase_parameters_dependency),
            ),
        )
    new_sig = func_sig.replace(parameters=parameters)

    @wraps(func, new_sig=new_sig)
    def wrapper(*args: Any, **kwargs: Any) -> Any:
        if "camelcase_parameters_dependency" in kwargs:
            del kwargs["camelcase_parameters_dependency"]
        return func(*args, **kwargs)

    return wrapper
예제 #2
0
    def _generate_next_value_(name, start, count,
                              last_values) -> str:  # type: ignore
        """
        Uses the camelCase name as the automatic value, rather than an integer

        See https://docs.python.org/3/library/enum.html#using-automatic-values for reference
        """
        return snake2camel(name, start_lower=True)
예제 #3
0
def camelcase_parameters_dependency(request: Request) -> None:
    query_params = request.query_params
    new_params = MultiDict()
    for k, v in query_params.multi_items():
        if "_" in k:
            camel = snake2camel(k, start_lower=True)
            new_params.append(camel, v)
        else:
            new_params.append(k, v)
    request._query_params = new_params
예제 #4
0
def get_param_field(
    *,
    param: inspect.Parameter,
    param_name: str,
    default_field_info: Type[params.Param] = params.Param,
    force_type: Optional[params.ParamTypes] = None,
    ignore_default: bool = False,
) -> ModelField:
    default_value = Required
    had_schema = False
    if not param.default == param.empty and ignore_default is False:
        default_value = param.default
    if isinstance(default_value, FieldInfo):
        had_schema = True
        field_info = default_value
        default_value = field_info.default
        if (
            isinstance(field_info, params.Param)
            and getattr(field_info, "in_", None) is None
        ):
            field_info.in_ = default_field_info.in_
        if force_type:
            field_info.in_ = force_type  # type: ignore
    else:
        field_info = default_field_info(default_value)
    required = default_value == Required
    annotation: Any = Any
    if not param.annotation == param.empty:
        annotation = param.annotation
    annotation = get_annotation_from_field_info(annotation, field_info, param_name)
    if not field_info.alias and getattr(field_info, "convert_underscores", None):
        alias = param.name.replace("_", "-")
    else:
        if field_info.alias:
            alias = field_info.alias
        else:
            alias = snake2camel(param.name, start_lower=True)
    field = create_response_field(
        name=param.name,
        type_=annotation,
        default=None if required else default_value,
        alias=alias,
        required=required,
        field_info=field_info,
    )
    field.required = required
    if not had_schema and not is_scalar_field(field=field):
        field.field_info = params.Body(field_info.default)

    return field
예제 #5
0
from fastapi_utils.camelcase import camel2snake, snake2camel

assert snake2camel("some_field_name", start_lower=False) == "SomeFieldName"
assert snake2camel("some_field_name", start_lower=True) == "someFieldName"
assert camel2snake("someFieldName") == "some_field_name"
assert camel2snake("SomeFieldName") == "some_field_name"
예제 #6
0
def test_snake2camel(value: str, result: str) -> None:
    assert snake2camel(value) == result
예제 #7
0
def test_snake2camel_start_lower(value: str, result: str) -> None:
    assert snake2camel(value, start_lower=True) == result