Skip to content

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

@RyoichiroNagami

Description

@RyoichiroNagami

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: ""),

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions