Skip to content

Commit f9ff014

Browse files
committed
handle review comments
1 parent cb2331e commit f9ff014

File tree

5 files changed

+25
-29
lines changed

5 files changed

+25
-29
lines changed

src/client.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ using HTTP
1111
using MIMEs
1212

1313
import Base: convert, show, summary, getproperty, setproperty!, iterate
14-
import ..OpenAPI: APIModel, UnionAPIModel, OneOfAPIModel, AnyOfAPIModel, APIClientImpl, OpenAPIException, InvocationException, to_json, from_json, validate_property, property_type
14+
import ..OpenAPI: APIModel, UnionAPIModel, OneOfAPIModel, AnyOfAPIModel, APIClientImpl, OpenAPIException, InvocationException, to_json, from_json, validate_property, property_type, deep_object_serialize
1515
import ..OpenAPI: str2zoneddatetime, str2datetime, str2date
1616

1717

@@ -295,8 +295,8 @@ set_param(params::Dict{String,String}, name::String, value::Nothing; collection_
295295
function set_param(params::Dict{String,String}, name::String, value; collection_format=",", style="form", is_explode=false)
296296
deep_explode = style == "deepObject" && is_explode
297297
if deep_explode
298-
merge!(params, serialize_to_query_dict(Dict(name=>value)))
299-
return params
298+
merge!(params, deep_object_serialize(Dict(name=>value)))
299+
return nothing
300300
end
301301
if isa(value, Dict)
302302
# implements the default serialization (style=form, explode=true, location=queryparams)
@@ -833,12 +833,12 @@ function extract_filename(resp::Downloads.Response)::String
833833
return string("response", extension_from_mime(MIME(content_type_str)))
834834
end
835835

836-
function serialize_to_query_dict(dict::Dict, parent_key::String = "")
836+
function deep_object_serialize(dict::Dict, parent_key::String = "")
837837
parts = Pair[]
838838
for (key, value) in dict
839839
new_key = parent_key == "" ? key : "$parent_key[$key]"
840840
if isa(value, Dict)
841-
append!(parts, collect(serialize_to_query_dict(value, new_key)))
841+
append!(parts, collect(deep_object_serialize(value, new_key)))
842842
elseif isa(value, Vector)
843843
for (i, v) in enumerate(value)
844844
push!(parts, "$new_key[$(i-1)]"=>"$v")

src/json.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ end
4141

4242
is_deep_explode(sctx::StyleCtx) = sctx.name == "deepObject" && sctx.is_explode
4343

44-
function convert_dict_to_array(src::Dict)
44+
function deep_object_to_array(src::Dict)
4545
keys_are_int = all(key -> occursin(r"^\d+$", key), keys(src))
4646
if keys_are_int
4747
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
6666

6767
function from_json(::Type{Vector{T}}, json::Dict{String, Any}; stylectx=nothing) where {T}
6868
if !isnothing(stylectx) && is_deep_explode(stylectx)
69-
cvt = convert_dict_to_array(json)
69+
cvt = deep_object_to_array(json)
7070
if isa(cvt, Vector)
7171
return from_json(Vector{T}, cvt; stylectx)
7272
else

src/server.jl

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module Servers
33
using JSON
44
using HTTP
55

6-
import ..OpenAPI: APIModel, ValidationException, from_json, to_json, convert_dict_to_array, StyleCtx, is_deep_explode
6+
import ..OpenAPI: APIModel, ValidationException, from_json, to_json, deep_object_to_array, StyleCtx, is_deep_explode
77

88
function middleware(impl, read, validate, invoke;
99
init=nothing,
@@ -80,7 +80,6 @@ function get_param(source::Vector{HTTP.Forms.Multipart}, name::String, required:
8080
end
8181
end
8282

83-
8483
function to_param_type(::Type{T}, strval::String; stylectx=nothing) where {T <: Number}
8584
parse(T, strval)
8685
end
@@ -94,15 +93,12 @@ to_param_type(::Type{Vector{T}}, json::Vector{Any}; stylectx=nothing) where {T}
9493

9594
function to_param_type(::Type{Vector{T}}, json::Dict{String, Any}; stylectx=nothing) where {T}
9695
if !isnothing(stylectx) && is_deep_explode(stylectx)
97-
cvt = convert_dict_to_array(json)
96+
cvt = deep_object_to_array(json)
9897
if isa(cvt, Vector)
9998
to_param_type(Vector{T}, cvt; stylectx)
100-
else
101-
to_param_type(T, json; stylectx)
10299
end
103-
else
104-
to_param_type(T, json; stylectx)
105100
end
101+
error("Unable to convert $json to $(Vector{T})")
106102
end
107103

108104
function to_param_type(::Type{T}, strval::String; stylectx=nothing) where {T <: APIModel}

test/client/param_serialize.jl

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,45 @@
1-
using OpenAPI.Clients: serialize_to_query_dict
2-
@testset "Test serialize_to_query_dict" begin
1+
using OpenAPI.Clients: deep_object_serialize
2+
3+
@testset "Test deep_object_serialize" begin
34
@testset "Single level object" begin
45
dict = Dict("key1" => "value1", "key2" => "value2")
56
expected = Dict("key1" => "value1", "key2" => "value2")
6-
@test serialize_to_query_dict(dict) == expected
7+
@test deep_object_serialize(dict) == expected
78
end
89

910
@testset "Nested object" begin
1011
dict = Dict("outer" => Dict("inner" => "value"))
1112
expected = Dict("outer[inner]" => "value")
12-
@test serialize_to_query_dict(dict) == expected
13+
@test deep_object_serialize(dict) == expected
1314
end
1415

1516
@testset "Deeply nested object" begin
1617
dict = Dict("a" => Dict("b" => Dict("c" => Dict("d" => "value"))))
1718
expected = Dict("a[b][c][d]" => "value")
18-
@test serialize_to_query_dict(dict) == expected
19+
@test deep_object_serialize(dict) == expected
1920
end
2021

2122
@testset "Multiple nested objects" begin
2223
dict = Dict("a" => Dict("b" => "value1", "c" => "value2"))
2324
expected = Dict("a[b]" => "value1", "a[c]" => "value2")
24-
@test serialize_to_query_dict(dict) == expected
25+
@test deep_object_serialize(dict) == expected
2526
end
2627

2728
@testset "Dictionary represented array" begin
2829
dict = Dict("a" => ["value1", "value2"])
2930
expected = Dict("a[0]" => "value1", "a[1]" => "value2")
30-
@test serialize_to_query_dict(dict) == expected
31+
@test deep_object_serialize(dict) == expected
3132
end
3233

3334
@testset "Mixed structure" begin
3435
dict = Dict("a" => Dict("b" => "value1", "c" => ["value2", "value3"]))
3536
expected = Dict("a[b]" => "value1", "a[c][0]" => "value2", "a[c][1]" => "value3")
36-
@test serialize_to_query_dict(dict) == expected
37+
@test deep_object_serialize(dict) == expected
3738
end
3839

3940
@testset "Blank values" begin
4041
dict = Dict("a" => Dict("b" => "", "c" => ""))
4142
expected = Dict("a[b]" => "", "a[c]" => "")
42-
@test serialize_to_query_dict(dict) == expected
43+
@test deep_object_serialize(dict) == expected
4344
end
4445
end

test/param_deserialize.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
module ParamSerialization
21
using Test
2+
33
using OpenAPI.Servers: deep_dict_repr
4-
using OpenAPI: convert_dict_to_array
4+
using OpenAPI: deep_object_to_array
55
@testset "Test deep_dict_repr" begin
66
@testset "Single level object" begin
77
query_string = Dict("key1" => "value1", "key2" => "value2")
@@ -41,12 +41,12 @@ using OpenAPI: convert_dict_to_array
4141
@test deep_dict_repr(query_string) == expected
4242
end
4343

44-
@testset "convert_dict_to_array" begin
44+
@testset "deep_object_to_array" begin
4545
example = Dict(
4646
"a" => Dict("b" => "value1", "c" => Dict("0" => "value2", "1" => "value3")),
4747
)
48-
@test convert_dict_to_array(example) == example
49-
@test convert_dict_to_array(example["a"]["c"]) == ["value2", "value3"]
48+
@test deep_object_to_array(example) == example
49+
@test deep_object_to_array(example["a"]["c"]) == ["value2", "value3"]
5050
end
5151

5252
@testset "Blank values" begin
@@ -97,4 +97,3 @@ using OpenAPI: convert_dict_to_array
9797
end
9898

9999
end
100-
end

0 commit comments

Comments
 (0)