Skip to content

Commit c563a12

Browse files
authored
fix some UnionAPIModel marshalling issues (#61)
Fixes some data marshalling and unmarshalling issues with UnionAPIModel (oneof and anyof). Also added more tests around it.
1 parent ca99f1d commit c563a12

31 files changed

+789
-6
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 = ["JuliaHub Inc."]
7-
version = "0.1.17"
7+
version = "0.1.18"
88

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

src/client.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ convert(::Type{T}, v::Nothing) where {T<:APIModel} = T()
711711
convert(::Type{T}, v::T) where {T<:OneOfAPIModel} = v
712712
convert(::Type{T}, json::Dict{String,Any}) where {T<:OneOfAPIModel} = from_json(T, json)
713713
convert(::Type{T}, v) where {T<:OneOfAPIModel} = T(v)
714+
convert(::Type{T}, v::String) where {T<:OneOfAPIModel} = T(v)
714715
convert(::Type{T}, v::T) where {T<:AnyOfAPIModel} = v
715716
convert(::Type{T}, json::Dict{String,Any}) where {T<:AnyOfAPIModel} = from_json(T, json)
716717
convert(::Type{T}, v) where {T<:AnyOfAPIModel} = T(v)

src/json.jl

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

2626
lower(o::T) where {T<:APIModel} = JSONWrapper(o)
27-
lower(o::T) where {T<:UnionAPIModel} = typeof(o.value) <: APIModel ? JSONWrapper(o.value) : to_json(o.value)
27+
function lower(o::T) where {T<:UnionAPIModel}
28+
if typeof(o.value) <: APIModel
29+
return JSONWrapper(o.value)
30+
elseif typeof(o.value) <: Union{String,Real}
31+
return o.value
32+
else
33+
return to_json(o.value)
34+
end
35+
end
2836

2937
to_json(o) = JSON.json(o)
3038

@@ -38,6 +46,12 @@ function from_json(o::T, json::Dict{String,Any}) where {T <: UnionAPIModel}
3846
return from_json(o, :value, json)
3947
end
4048

49+
from_json(::Type{T}, val::Union{String,Real}) where {T <: UnionAPIModel} = T(val)
50+
function from_json(o::T, val::Union{String,Real}) where {T <: UnionAPIModel}
51+
o.value = val
52+
return o
53+
end
54+
4155
function from_json(o::T, json::Dict{String,Any}) where {T <: APIModel}
4256
jsonkeys = [Symbol(k) for k in keys(json)]
4357
for name in intersect(propertynames(o), jsonkeys)
@@ -54,7 +68,7 @@ function from_json(o::T, name::Symbol, json::Dict{String,Any}) where {T <: APIMo
5468
end
5569

5670
function from_json(o::T, name::Symbol, v) where {T <: APIModel}
57-
ftype = property_type(T, name)
71+
ftype = (T <: UnionAPIModel) ? property_type(T, name, Dict{String,Any}()) : property_type(T, name)
5872
if ftype === Any
5973
setfield!(o, name, v)
6074
elseif ZonedDateTime <: ftype
@@ -89,7 +103,13 @@ function from_json(o::T, name::Symbol, v::Vector) where {T <: APIModel}
89103
setfield!(o, name, map(str2date, v))
90104
else
91105
if (vtype <: Vector) && (veltype <: OpenAPI.UnionAPIModel)
92-
setfield!(o, name, map(veltype, v))
106+
vec = veltype[]
107+
for vecelem in v
108+
push!(vec, from_json(veltype(), :value, vecelem))
109+
end
110+
setfield!(o, name, vec)
111+
elseif (vtype <: Vector) && (veltype <: OpenAPI.APIModel)
112+
setfield!(o, name, map(x->convert(veltype,x), v))
93113
elseif (vtype <: Vector) && (veltype <: String)
94114
# ensure that elements are converted to String
95115
# convert is to do the translation to Union{Nothing,String} when necessary
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,25 @@
11
README.md
2+
docs/AnyOfBaseType.md
23
docs/AnyOfMappedPets.md
34
docs/AnyOfPets.md
45
docs/Cat.md
56
docs/DefaultApi.md
67
docs/Dog.md
8+
docs/OneOfBaseType.md
79
docs/OneOfMappedPets.md
810
docs/OneOfPets.md
911
docs/Pet.md
12+
docs/TypeWithAllArrayTypes.md
1013
src/AllAnyClient.jl
1114
src/apis/api_DefaultApi.jl
1215
src/modelincludes.jl
16+
src/models/model_AnyOfBaseType.jl
1317
src/models/model_AnyOfMappedPets.jl
1418
src/models/model_AnyOfPets.jl
1519
src/models/model_Cat.jl
1620
src/models/model_Dog.jl
21+
src/models/model_OneOfBaseType.jl
1722
src/models/model_OneOfMappedPets.jl
1823
src/models/model_OneOfPets.jl
1924
src/models/model_Pet.jl
25+
src/models/model_TypeWithAllArrayTypes.jl
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.0.0-SNAPSHOT
1+
7.0.1-SNAPSHOT

test/client/allany/AllAnyClient/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,21 +20,27 @@ Documentation is also embedded in Julia which can be used with a Julia specific
2020

2121
Class | Method
2222
------------ | -------------
23+
*DefaultApi* | [**echo_anyof_base_type_post**](docs/DefaultApi.md#echo_anyof_base_type_post)<br/>**POST** /echo_anyof_base_type<br/>
2324
*DefaultApi* | [**echo_anyof_mapped_pets_post**](docs/DefaultApi.md#echo_anyof_mapped_pets_post)<br/>**POST** /echo_anyof_mapped_pets<br/>
2425
*DefaultApi* | [**echo_anyof_pets_post**](docs/DefaultApi.md#echo_anyof_pets_post)<br/>**POST** /echo_anyof_pets<br/>
26+
*DefaultApi* | [**echo_arrays_post**](docs/DefaultApi.md#echo_arrays_post)<br/>**POST** /echo_arrays<br/>
27+
*DefaultApi* | [**echo_oneof_base_type_post**](docs/DefaultApi.md#echo_oneof_base_type_post)<br/>**POST** /echo_oneof_base_type<br/>
2528
*DefaultApi* | [**echo_oneof_mapped_pets_post**](docs/DefaultApi.md#echo_oneof_mapped_pets_post)<br/>**POST** /echo_oneof_mapped_pets<br/>
2629
*DefaultApi* | [**echo_oneof_pets_post**](docs/DefaultApi.md#echo_oneof_pets_post)<br/>**POST** /echo_oneof_pets<br/>
2730

2831

2932
## Models
3033

34+
- [AnyOfBaseType](docs/AnyOfBaseType.md)
3135
- [AnyOfMappedPets](docs/AnyOfMappedPets.md)
3236
- [AnyOfPets](docs/AnyOfPets.md)
3337
- [Cat](docs/Cat.md)
3438
- [Dog](docs/Dog.md)
39+
- [OneOfBaseType](docs/OneOfBaseType.md)
3540
- [OneOfMappedPets](docs/OneOfMappedPets.md)
3641
- [OneOfPets](docs/OneOfPets.md)
3742
- [Pet](docs/Pet.md)
43+
- [TypeWithAllArrayTypes](docs/TypeWithAllArrayTypes.md)
3844

3945

4046
<a id="authorization"></a>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# AnyOfBaseType
2+
3+
4+
5+
## Properties
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
**value** | This is a anyOf model. The value must be any of the following types: Float64, String | | [optional]
9+
10+
11+
12+
13+
14+
[[Back to Model list]](../README.md#models) [[Back to API list]](../README.md#api-endpoints) [[Back to README]](../README.md)
15+
16+

test/client/allany/AllAnyClient/docs/DefaultApi.md

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,43 @@ All URIs are relative to *http://localhost*
44

55
Method | HTTP request | Description
66
------------- | ------------- | -------------
7+
[**echo_anyof_base_type_post**](DefaultApi.md#echo_anyof_base_type_post) | **POST** /echo_anyof_base_type |
78
[**echo_anyof_mapped_pets_post**](DefaultApi.md#echo_anyof_mapped_pets_post) | **POST** /echo_anyof_mapped_pets |
89
[**echo_anyof_pets_post**](DefaultApi.md#echo_anyof_pets_post) | **POST** /echo_anyof_pets |
10+
[**echo_arrays_post**](DefaultApi.md#echo_arrays_post) | **POST** /echo_arrays |
11+
[**echo_oneof_base_type_post**](DefaultApi.md#echo_oneof_base_type_post) | **POST** /echo_oneof_base_type |
912
[**echo_oneof_mapped_pets_post**](DefaultApi.md#echo_oneof_mapped_pets_post) | **POST** /echo_oneof_mapped_pets |
1013
[**echo_oneof_pets_post**](DefaultApi.md#echo_oneof_pets_post) | **POST** /echo_oneof_pets |
1114

1215

16+
# **echo_anyof_base_type_post**
17+
> echo_anyof_base_type_post(_api::DefaultApi, any_of_base_type::AnyOfBaseType; _mediaType=nothing) -> AnyOfBaseType, OpenAPI.Clients.ApiResponse <br/>
18+
> echo_anyof_base_type_post(_api::DefaultApi, response_stream::Channel, any_of_base_type::AnyOfBaseType; _mediaType=nothing) -> Channel{ AnyOfBaseType }, OpenAPI.Clients.ApiResponse
19+
20+
21+
22+
### Required Parameters
23+
24+
Name | Type | Description | Notes
25+
------------- | ------------- | ------------- | -------------
26+
**_api** | **DefaultApi** | API context |
27+
**any_of_base_type** | [**AnyOfBaseType**](AnyOfBaseType.md)| |
28+
29+
### Return type
30+
31+
[**AnyOfBaseType**](AnyOfBaseType.md)
32+
33+
### Authorization
34+
35+
No authorization required
36+
37+
### HTTP request headers
38+
39+
- **Content-Type**: application/json
40+
- **Accept**: application/json
41+
42+
[[Back to top]](#) [[Back to API list]](../README.md#api-endpoints) [[Back to Model list]](../README.md#models) [[Back to README]](../README.md)
43+
1344
# **echo_anyof_mapped_pets_post**
1445
> echo_anyof_mapped_pets_post(_api::DefaultApi, any_of_mapped_pets::AnyOfMappedPets; _mediaType=nothing) -> AnyOfMappedPets, OpenAPI.Clients.ApiResponse <br/>
1546
> echo_anyof_mapped_pets_post(_api::DefaultApi, response_stream::Channel, any_of_mapped_pets::AnyOfMappedPets; _mediaType=nothing) -> Channel{ AnyOfMappedPets }, OpenAPI.Clients.ApiResponse
@@ -66,6 +97,62 @@ No authorization required
6697

6798
[[Back to top]](#) [[Back to API list]](../README.md#api-endpoints) [[Back to Model list]](../README.md#models) [[Back to README]](../README.md)
6899

100+
# **echo_arrays_post**
101+
> echo_arrays_post(_api::DefaultApi, type_with_all_array_types::TypeWithAllArrayTypes; _mediaType=nothing) -> TypeWithAllArrayTypes, OpenAPI.Clients.ApiResponse <br/>
102+
> echo_arrays_post(_api::DefaultApi, response_stream::Channel, type_with_all_array_types::TypeWithAllArrayTypes; _mediaType=nothing) -> Channel{ TypeWithAllArrayTypes }, OpenAPI.Clients.ApiResponse
103+
104+
105+
106+
### Required Parameters
107+
108+
Name | Type | Description | Notes
109+
------------- | ------------- | ------------- | -------------
110+
**_api** | **DefaultApi** | API context |
111+
**type_with_all_array_types** | [**TypeWithAllArrayTypes**](TypeWithAllArrayTypes.md)| |
112+
113+
### Return type
114+
115+
[**TypeWithAllArrayTypes**](TypeWithAllArrayTypes.md)
116+
117+
### Authorization
118+
119+
No authorization required
120+
121+
### HTTP request headers
122+
123+
- **Content-Type**: application/json
124+
- **Accept**: application/json
125+
126+
[[Back to top]](#) [[Back to API list]](../README.md#api-endpoints) [[Back to Model list]](../README.md#models) [[Back to README]](../README.md)
127+
128+
# **echo_oneof_base_type_post**
129+
> echo_oneof_base_type_post(_api::DefaultApi, one_of_base_type::OneOfBaseType; _mediaType=nothing) -> OneOfBaseType, OpenAPI.Clients.ApiResponse <br/>
130+
> echo_oneof_base_type_post(_api::DefaultApi, response_stream::Channel, one_of_base_type::OneOfBaseType; _mediaType=nothing) -> Channel{ OneOfBaseType }, OpenAPI.Clients.ApiResponse
131+
132+
133+
134+
### Required Parameters
135+
136+
Name | Type | Description | Notes
137+
------------- | ------------- | ------------- | -------------
138+
**_api** | **DefaultApi** | API context |
139+
**one_of_base_type** | [**OneOfBaseType**](OneOfBaseType.md)| |
140+
141+
### Return type
142+
143+
[**OneOfBaseType**](OneOfBaseType.md)
144+
145+
### Authorization
146+
147+
No authorization required
148+
149+
### HTTP request headers
150+
151+
- **Content-Type**: application/json
152+
- **Accept**: application/json
153+
154+
[[Back to top]](#) [[Back to API list]](../README.md#api-endpoints) [[Back to Model list]](../README.md#models) [[Back to README]](../README.md)
155+
69156
# **echo_oneof_mapped_pets_post**
70157
> echo_oneof_mapped_pets_post(_api::DefaultApi, one_of_mapped_pets::OneOfMappedPets; _mediaType=nothing) -> OneOfMappedPets, OpenAPI.Clients.ApiResponse <br/>
71158
> echo_oneof_mapped_pets_post(_api::DefaultApi, response_stream::Channel, one_of_mapped_pets::OneOfMappedPets; _mediaType=nothing) -> Channel{ OneOfMappedPets }, OpenAPI.Clients.ApiResponse
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# OneOfBaseType
2+
3+
4+
5+
## Properties
6+
Name | Type | Description | Notes
7+
------------ | ------------- | ------------- | -------------
8+
**value** | This is a oneOf model. The value must be exactly one of the following types: Float64, String | | [optional]
9+
10+
11+
12+
13+
[[Back to Model list]](../README.md#models) [[Back to API list]](../README.md#api-endpoints) [[Back to README]](../README.md)
14+
15+
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TypeWithAllArrayTypes
2+
3+
4+
## Properties
5+
Name | Type | Description | Notes
6+
------------ | ------------- | ------------- | -------------
7+
**oneofbase** | [**Vector{OneOfBaseType}**](OneOfBaseType.md) | | [optional] [default to nothing]
8+
**anyofbase** | [**Vector{AnyOfBaseType}**](AnyOfBaseType.md) | | [optional] [default to nothing]
9+
**oneofpets** | [**Vector{OneOfPets}**](OneOfPets.md) | | [optional] [default to nothing]
10+
**anyofpets** | [**Vector{AnyOfPets}**](AnyOfPets.md) | | [optional] [default to nothing]
11+
12+
13+
[[Back to Model list]](../README.md#models) [[Back to API list]](../README.md#api-endpoints) [[Back to README]](../README.md)
14+
15+

0 commit comments

Comments
 (0)