Skip to content

Commit c5121b0

Browse files
authored
specialize hasproperty for APIModel (#21)
- specialize hasproperty for APIModel to check for `nothing` - update `haspropertyat` method to evaluate correctly - add tests
1 parent 677936c commit c5121b0

File tree

5 files changed

+43
-3
lines changed

5 files changed

+43
-3
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 <tanmaykm@gmail.com>", "JuliaHub"]
7-
version = "0.1.6"
7+
version = "0.1.7"
88

99
[deps]
1010
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

src/OpenAPI.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ module OpenAPI
22

33
using HTTP, JSON, URIs, Dates, TimeZones, Base64
44

5-
import Base: getindex, keys, length, iterate
5+
import Base: getindex, keys, length, iterate, hasproperty
66
import JSON: lower
77

88
include("commontypes.jl")

src/client.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,7 @@ function haspropertyat(o::T, path...) where {T<:APIModel}
490490
ret = hasproperty(o, p1)
491491
rempath = path[2:end]
492492
(length(rempath) == 0) && (return ret)
493+
ret || (return false)
493494

494495
val = getproperty(o, p1)
495496
if isa(val, Vector)
@@ -508,6 +509,8 @@ function haspropertyat(o::T, path...) where {T<:APIModel}
508509
haspropertyat(val, rempath...)
509510
end
510511

512+
Base.hasproperty(o::T, name::Symbol) where {T<:APIModel} = ((name in propertynames(o)) && (getproperty(o, name) !== nothing))
513+
511514
convert(::Type{T}, json::Dict{String,Any}) where {T<:APIModel} = from_json(T, json)
512515
convert(::Type{T}, v::Nothing) where {T<:APIModel} = T()
513516

@@ -543,4 +546,4 @@ is_request_interrupted(ex::TaskFailedException) = is_request_interrupted(ex.task
543546
is_request_interrupted(ex::CompositeException) = any(is_request_interrupted, ex.exceptions)
544547
is_request_interrupted(ex::InvocationException) = ex.reason == "request was interrupted"
545548

546-
end # module Clients
549+
end # module Clients

test/client/runtests.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ function runtests()
1515
test_request_interrupted_exception_check()
1616
test_date()
1717
test_misc()
18+
test_has_property()
1819
end
1920
@testset "Validations" begin
2021
test_validations()

test/client/utilstests.jl

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,42 @@ function test_validations()
159159
return nothing
160160
end
161161

162+
struct TestHasPropertyInner <: OpenAPI.APIModel
163+
testval::Union{Nothing,String}
164+
165+
function TestHasPropertyInner(; testval=nothing)
166+
return new(testval)
167+
end
168+
end
169+
170+
struct TestHasProperty <: OpenAPI.APIModel
171+
inner::Union{Nothing,TestHasPropertyInner}
172+
173+
function TestHasProperty(; inner=nothing)
174+
return new(inner)
175+
end
176+
end
177+
178+
function test_has_property()
179+
teststruct = TestHasProperty()
180+
181+
@test !OpenAPI.Clients.haspropertyat(teststruct, :inner, :testval)
182+
@test !OpenAPI.Clients.haspropertyat(teststruct, "inner", "testval")
183+
@test !OpenAPI.Clients.haspropertyat(teststruct, :inner)
184+
185+
teststruct = TestHasProperty(; inner=TestHasPropertyInner())
186+
@test !OpenAPI.Clients.haspropertyat(teststruct, :inner, :testval)
187+
@test !OpenAPI.Clients.haspropertyat(teststruct, "inner", "testval")
188+
@test OpenAPI.Clients.haspropertyat(teststruct, :inner)
189+
190+
teststruct = TestHasProperty(; inner=TestHasPropertyInner(; testval="test"))
191+
@test OpenAPI.Clients.haspropertyat(teststruct, :inner, :testval)
192+
@test OpenAPI.Clients.haspropertyat(teststruct, "inner", "testval")
193+
@test OpenAPI.Clients.haspropertyat(teststruct, :inner)
194+
@test OpenAPI.Clients.getpropertyat(teststruct, :inner, :testval) == "test"
195+
end
196+
197+
162198
struct InvalidModel <: OpenAPI.APIModel
163199
test::Any
164200

0 commit comments

Comments
 (0)