diff --git a/src/client.jl b/src/client.jl index ca080f1..cb4ee4c 100644 --- a/src/client.jl +++ b/src/client.jl @@ -11,7 +11,7 @@ using HTTP using MIMEs import Base: convert, show, summary, getproperty, setproperty!, iterate -import ..OpenAPI: APIModel, UnionAPIModel, OneOfAPIModel, AnyOfAPIModel, APIClientImpl, OpenAPIException, InvocationException, to_json, from_json, validate_property, property_type +import ..OpenAPI: APIModel, UnionAPIModel, OneOfAPIModel, AnyOfAPIModel, APIClientImpl, OpenAPIException, InvocationException, to_json, from_json, validate_property, property_type, deep_object_serialize import ..OpenAPI: str2zoneddatetime, str2datetime, str2date @@ -295,8 +295,8 @@ set_param(params::Dict{String,String}, name::String, value::Nothing; collection_ function set_param(params::Dict{String,String}, name::String, value; collection_format=",", style="form", is_explode=false) deep_explode = style == "deepObject" && is_explode if deep_explode - merge!(params, serialize_to_query_dict(Dict(name=>value))) - return params + merge!(params, deep_object_serialize(Dict(name=>value))) + return nothing end if isa(value, Dict) # implements the default serialization (style=form, explode=true, location=queryparams) @@ -833,12 +833,12 @@ function extract_filename(resp::Downloads.Response)::String return string("response", extension_from_mime(MIME(content_type_str))) end -function serialize_to_query_dict(dict::Dict, parent_key::String = "") +function deep_object_serialize(dict::Dict, parent_key::String = "") parts = Pair[] for (key, value) in dict new_key = parent_key == "" ? key : "$parent_key[$key]" if isa(value, Dict) - append!(parts, collect(serialize_to_query_dict(value, new_key))) + append!(parts, collect(deep_object_serialize(value, new_key))) elseif isa(value, Vector) for (i, v) in enumerate(value) push!(parts, "$new_key[$(i-1)]"=>"$v") diff --git a/src/json.jl b/src/json.jl index 38fc643..c93789b 100644 --- a/src/json.jl +++ b/src/json.jl @@ -41,7 +41,7 @@ end is_deep_explode(sctx::StyleCtx) = sctx.name == "deepObject" && sctx.is_explode -function convert_dict_to_array(src::Dict) +function deep_object_to_array(src::Dict) keys_are_int = all(key -> occursin(r"^\d+$", key), keys(src)) if keys_are_int sorted_keys = sort(collect(keys(src)), by=x->parse(Int, x)) @@ -66,7 +66,7 @@ from_json(::Type{Vector{T}}, j::Vector{Any}; stylectx=nothing) where {T} = j function from_json(::Type{Vector{T}}, json::Dict{String, Any}; stylectx=nothing) where {T} if !isnothing(stylectx) && is_deep_explode(stylectx) - cvt = convert_dict_to_array(json) + cvt = deep_object_to_array(json) if isa(cvt, Vector) return from_json(Vector{T}, cvt; stylectx) else diff --git a/src/server.jl b/src/server.jl index e0da85d..16184d6 100644 --- a/src/server.jl +++ b/src/server.jl @@ -3,7 +3,7 @@ module Servers using JSON using HTTP -import ..OpenAPI: APIModel, ValidationException, from_json, to_json, convert_dict_to_array, StyleCtx, is_deep_explode +import ..OpenAPI: APIModel, ValidationException, from_json, to_json, deep_object_to_array, StyleCtx, is_deep_explode function middleware(impl, read, validate, invoke; init=nothing, @@ -80,7 +80,6 @@ function get_param(source::Vector{HTTP.Forms.Multipart}, name::String, required: end end - function to_param_type(::Type{T}, strval::String; stylectx=nothing) where {T <: Number} parse(T, strval) end @@ -94,15 +93,12 @@ to_param_type(::Type{Vector{T}}, json::Vector{Any}; stylectx=nothing) where {T} function to_param_type(::Type{Vector{T}}, json::Dict{String, Any}; stylectx=nothing) where {T} if !isnothing(stylectx) && is_deep_explode(stylectx) - cvt = convert_dict_to_array(json) + cvt = deep_object_to_array(json) if isa(cvt, Vector) to_param_type(Vector{T}, cvt; stylectx) - else - to_param_type(T, json; stylectx) end - else - to_param_type(T, json; stylectx) end + error("Unable to convert $json to $(Vector{T})") end function to_param_type(::Type{T}, strval::String; stylectx=nothing) where {T <: APIModel} diff --git a/test/client/param_serialize.jl b/test/client/param_serialize.jl index 665bfbf..c6630aa 100644 --- a/test/client/param_serialize.jl +++ b/test/client/param_serialize.jl @@ -1,44 +1,45 @@ -using OpenAPI.Clients: serialize_to_query_dict -@testset "Test serialize_to_query_dict" begin +using OpenAPI.Clients: deep_object_serialize + +@testset "Test deep_object_serialize" begin @testset "Single level object" begin dict = Dict("key1" => "value1", "key2" => "value2") expected = Dict("key1" => "value1", "key2" => "value2") - @test serialize_to_query_dict(dict) == expected + @test deep_object_serialize(dict) == expected end @testset "Nested object" begin dict = Dict("outer" => Dict("inner" => "value")) expected = Dict("outer[inner]" => "value") - @test serialize_to_query_dict(dict) == expected + @test deep_object_serialize(dict) == expected end @testset "Deeply nested object" begin dict = Dict("a" => Dict("b" => Dict("c" => Dict("d" => "value")))) expected = Dict("a[b][c][d]" => "value") - @test serialize_to_query_dict(dict) == expected + @test deep_object_serialize(dict) == expected end @testset "Multiple nested objects" begin dict = Dict("a" => Dict("b" => "value1", "c" => "value2")) expected = Dict("a[b]" => "value1", "a[c]" => "value2") - @test serialize_to_query_dict(dict) == expected + @test deep_object_serialize(dict) == expected end @testset "Dictionary represented array" begin dict = Dict("a" => ["value1", "value2"]) expected = Dict("a[0]" => "value1", "a[1]" => "value2") - @test serialize_to_query_dict(dict) == expected + @test deep_object_serialize(dict) == expected end @testset "Mixed structure" begin dict = Dict("a" => Dict("b" => "value1", "c" => ["value2", "value3"])) expected = Dict("a[b]" => "value1", "a[c][0]" => "value2", "a[c][1]" => "value3") - @test serialize_to_query_dict(dict) == expected + @test deep_object_serialize(dict) == expected end @testset "Blank values" begin dict = Dict("a" => Dict("b" => "", "c" => "")) expected = Dict("a[b]" => "", "a[c]" => "") - @test serialize_to_query_dict(dict) == expected + @test deep_object_serialize(dict) == expected end end diff --git a/test/param_deserialize.jl b/test/param_deserialize.jl index 78bad5c..1881c71 100644 --- a/test/param_deserialize.jl +++ b/test/param_deserialize.jl @@ -1,7 +1,7 @@ -module ParamSerialization using Test + using OpenAPI.Servers: deep_dict_repr -using OpenAPI: convert_dict_to_array +using OpenAPI: deep_object_to_array @testset "Test deep_dict_repr" begin @testset "Single level object" begin query_string = Dict("key1" => "value1", "key2" => "value2") @@ -41,12 +41,12 @@ using OpenAPI: convert_dict_to_array @test deep_dict_repr(query_string) == expected end - @testset "convert_dict_to_array" begin + @testset "deep_object_to_array" begin example = Dict( "a" => Dict("b" => "value1", "c" => Dict("0" => "value2", "1" => "value3")), ) - @test convert_dict_to_array(example) == example - @test convert_dict_to_array(example["a"]["c"]) == ["value2", "value3"] + @test deep_object_to_array(example) == example + @test deep_object_to_array(example["a"]["c"]) == ["value2", "value3"] end @testset "Blank values" begin @@ -97,4 +97,3 @@ using OpenAPI: convert_dict_to_array end end -end