Skip to content

Commit

Permalink
handle review comments
Browse files Browse the repository at this point in the history
  • Loading branch information
vdayanand committed Jul 10, 2024
1 parent cb2331e commit f9ff014
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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")
Expand Down
4 changes: 2 additions & 2 deletions src/json.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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
Expand Down
10 changes: 3 additions & 7 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -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}
Expand Down
19 changes: 10 additions & 9 deletions test/client/param_serialize.jl
Original file line number Diff line number Diff line change
@@ -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
11 changes: 5 additions & 6 deletions test/param_deserialize.jl
Original file line number Diff line number Diff line change
@@ -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")
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -97,4 +97,3 @@ using OpenAPI: convert_dict_to_array
end

end
end

0 comments on commit f9ff014

Please sign in to comment.