JuliaComputing/OpenAPI.jl

Invalid attempt to convert vector elements to date

Closed this issue · 3 comments

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

if ZonedDateTime <: veltype

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
     ...

Thanks @avandecreme. Your suggestions here and in #32 seem valid.
Would you like to open a PR with the changes?

Sure, here it is: #34

fixed by #34