Skip to content

Commit b376012

Browse files
committed
fix json parsing for union types
1 parent 87b715f commit b376012

File tree

3 files changed

+11
-4
lines changed

3 files changed

+11
-4
lines changed

Project.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ keywords = ["Swagger", "OpenAPI", "REST"]
44
license = "MIT"
55
desc = "OpenAPI server and client helper for Julia"
66
authors = ["Tanmay Mohapatra <[email protected]>"]
7-
version = "0.1.1"
7+
version = "0.1.2"
88

99
[deps]
1010
Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"

src/client.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ using TimeZones
99
using LibCURL
1010

1111
import Base: convert, show, summary, getproperty, setproperty!, iterate
12-
import ..OpenAPI: APIModel, APIClientImpl, OpenAPIException, InvocationException, to_json, from_json, validate_property, property_type
12+
import ..OpenAPI: APIModel, UnionAPIModel, APIClientImpl, OpenAPIException, InvocationException, to_json, from_json, validate_property, property_type
1313
import ..OpenAPI: str2zoneddatetime, str2datetime, str2date
1414

1515
# collection formats (OpenAPI v2)
@@ -507,6 +507,7 @@ end
507507
convert(::Type{T}, json::Dict{String,Any}) where {T<:APIModel} = from_json(T, json)
508508
convert(::Type{T}, v::Nothing) where {T<:APIModel} = T()
509509

510+
show(io::IO, model::T) where {T<:UnionAPIModel} = print(io, JSON.json(model.value, 2))
510511
show(io::IO, model::T) where {T<:APIModel} = print(io, JSON.json(model, 2))
511512
summary(model::T) where {T<:APIModel} = print(io, T)
512513

src/json.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ function iterate(w::JSONWrapper, state...)
2424
end
2525

2626
lower(o::T) where {T<:APIModel} = JSONWrapper(o)
27-
lower(o::T) where {T<:UnionAPIModel} = JSONWrapper(o.value)
27+
lower(o::T) where {T<:UnionAPIModel} = typeof(o.value) <: APIModel ? JSONWrapper(o.value) : to_json(o.value)
2828

2929
to_json(o) = JSON.json(o)
3030

@@ -81,7 +81,13 @@ function from_json(o::T, name::Symbol, v::Vector) where {T <: APIModel}
8181
elseif Date <: veltype
8282
setfield!(o, name, map(str2date, v))
8383
else
84-
setfield!(o, name, convert(ftype, v))
84+
if (vtype <: Vector) && (veltype <: OpenAPI.UnionAPIModel)
85+
setfield!(o, name, map(veltype, v))
86+
elseif ftype <: OpenAPI.UnionAPIModel
87+
setfield!(o, name, ftype(v))
88+
else
89+
setfield!(o, name, convert(ftype, v))
90+
end
8591
end
8692
return o
8793
end

0 commit comments

Comments
 (0)