Invalid attempt to convert vector elements to date
Closed this issue · 3 comments
avandecreme commented
Given the following schema
{
"components": {
"schemas": {
"ArrayWrapper": {
"items": {
"type": "string"
},
"type": "array"
},
"Model": {
"properties": {
"values": {
"anyOf": [
{
"$ref": "#/components/schemas/ArrayWrapper"
},
{
"nullable": true,
"type": "object"
}
]
}
},
"type": "object"
}
}
},
"info": {
"title": "",
"version": ""
},
"openapi": "3.0.0",
"paths": {
"/bar": {
"get": {
"responses": {
"200": {
"content": {
"application/json;charset=utf-8": {
"schema": {
"$ref": "#/components/schemas/Model"
}
}
},
"description": ""
}
}
}
}
}
}
The generated client API errors out when calling the endpoint. The actual failure can be narrowed to the JSON parsing and reproduced like this:
include("julia/src/APIClient.jl")
import JSON
model_json = JSON.parse("{\"values\":[\"foo\"]}")
convert(APIClient.Model, model_json)
Giving the following error:
ERROR: LoadError: OpenAPI.OpenAPIException("Unsupported DateTime format: foo")
Stacktrace:
[1] str2datetime(str::String)
@ OpenAPI ~/.julia/packages/OpenAPI/hCYGY/src/datetime.jl:50
[2] str2zoneddatetime(str::String)
@ OpenAPI ~/.julia/packages/OpenAPI/hCYGY/src/datetime.jl:36
[3] iterate
@ ./generator.jl:47 [inlined]
[4] _collect(c::Vector{Any}, itr::Base.Generator{Vector{Any}, typeof(OpenAPI.str2zoneddatetime)}, #unused#::Base.EltypeUnknown, isz::Base.HasShape{1})
@ Base ./array.jl:802
[5] collect_similar
@ ./array.jl:711 [inlined]
[6] map
@ ./abstractarray.jl:3261 [inlined]
[7] from_json(o::Main.APIClient.Model, name::Symbol, v::Vector{Any})
@ OpenAPI ~/.julia/packages/OpenAPI/hCYGY/src/json.jl:83
[8] from_json(o::Main.APIClient.Model, json::Dict{String, Any})
@ OpenAPI ~/.julia/packages/OpenAPI/hCYGY/src/json.jl:44
[9] from_json
@ ~/.julia/packages/OpenAPI/hCYGY/src/json.jl:32 [inlined]
[10] convert(#unused#::Type{Main.APIClient.Model}, json::Dict{String, Any})
@ OpenAPI.Clients ~/.julia/packages/OpenAPI/hCYGY/src/client.jl:524
[11] top-level scope
@ ~/code/servant-openapi-test/test.jl:71
I was able to narrow the error down to the following line
Line 82 in bf0e362
In our case veltype
is Any
.
Adding the following branch before the above one, together with the fix I suggested in #32 seems to fix the issue:
if veltype === Any
setfield!(o, name, convert(ftype, v))
elseif TimeZones.ZonedDateTime <: veltype
...
tanmaykm commented
Thanks @avandecreme. Your suggestions here and in #32 seem valid.
Would you like to open a PR with the changes?
avandecreme commented
Sure, here it is: #34