Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc function: Parsing fails in kotlinx.serialization when content containing whitespace is included in the request #8

Open
RyoichiroNagami opened this issue Dec 2, 2024 · 1 comment

Comments

@RyoichiroNagami
Copy link
Contributor

Sending Requests Containing Whitespace

An error occurs when a parameter of type String contains a whitespace string.

This problem did not occur when converted to json format and then converted in kotlin.

swift code

    func register(title: String, startDate: Date, endDate: Date, details: [PlanDetailDTO], expenses: [ExpenseDTO]) async throws {
        let detailsJsonData = try! JSONEncoder().encode(details)
        let detailsJsonString = String(data: detailsJsonData, encoding: .utf8)!
        
        let expensesJsonData = try! JSONEncoder().encode(expenses)
        let expensesJsonString = String(data: expensesJsonData, encoding: .utf8)!
        
        let _ =
            try await client
            .rpc(
                "insert_education_plan",
                params: [
                    "plan_name_input": title, // Android Plan 1
                    "start_date_input": startDate.format(style: "YYYY/MM/dd"),
                    "end_date_input": endDate.format(style: "YYYY/MM/dd"),
                    "details_input": detailsJsonString,
                    "expenses_input": expensesJsonString
                ]
            )
            .execute()
    }

error log


error: skip.lib.ErrorException: kotlinx.serialization.json.internal.JsonDecodingException: Unexpected JSON token at offset 9: Expected EOF after parsing, but had P instead at path: $JSON input: Android Plan 1

"plan_name_input": title, // Android Plan 1

Failure in plan_name_input parameter

tested rpc function

CREATE OR REPLACE FUNCTION public.insert_education_plan(
    plan_name_input text,
    start_date_input date,
    end_date_input date,
    details_input text,
    expenses_input text
) 
RETURNS VOID 
LANGUAGE plpgsql 
AS $function$
DECLARE 
    farm_id_input UUID;
    plan_id_result UUID;
    detail_result RECORD;
    expense_result RECORD;
    details_jsonb jsonb;
    expenses_jsonb jsonb;
BEGIN 
    -- convert to jsonb from text
    details_jsonb := details_input::jsonb;
    expenses_jsonb := expenses_input::jsonb;
    
    -- ... etc

    RETURN;

EXCEPTION
    WHEN OTHERS THEN
        RAISE;
END;
$function$;

Removing whitespace worked fine.

title.replacingOccurrences(of: " ", with: ""),
@RyoichiroNagami
Copy link
Contributor Author

I will fix this bug once the app development is settled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant