Skip to content

Commit 7d27c4e

Browse files
committed
feat: helpers for serverside validations
This adds validation method templates that can be used by `openapi-generator` to generate better serverside validation code. All client and server code have been updated using a branch of `openapi-generator` that does that. A PR with that to `openapi-generator` repo will follow. A few test methods had to be changed to conform to the additional validations.
1 parent 3c9d48a commit 7d27c4e

File tree

126 files changed

+2092
-523
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

126 files changed

+2092
-523
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.28"
7+
version = "0.2.0"
88

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

src/val.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,5 @@ function validate_param(parameter, operation_or_model, rule, value, args...)
8484
end
8585

8686
validate_property(::Type{T}, name::Symbol, val) where {T<:APIModel} = nothing
87+
validate_properties(::T) where {T<:APIModel} = nothing
88+
check_required(::T) where {T<:APIModel} = true
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.0.1-SNAPSHOT
1+
7.13.0-SNAPSHOT

test/client/allany/AllAnyClient/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ API to test code generation for oneof anyof allof
66
This API client was generated by the [OpenAPI Generator](https://openapi-generator.tech) project. By using the [openapi-spec](https://openapis.org) from a remote server, you can easily generate an API client.
77

88
- API version: 0.0.1
9+
- Generator version: 7.13.0-SNAPSHOT
910
- Build package: org.openapitools.codegen.languages.JuliaClientCodegen
1011

1112

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ Method | HTTP request | Description
2424
Name | Type | Description | Notes
2525
------------- | ------------- | ------------- | -------------
2626
**_api** | **DefaultApi** | API context |
27-
**any_of_base_type** | [**AnyOfBaseType**](AnyOfBaseType.md)| |
27+
**any_of_base_type** | [**AnyOfBaseType**](AnyOfBaseType.md) | |
2828

2929
### Return type
3030

@@ -52,7 +52,7 @@ No authorization required
5252
Name | Type | Description | Notes
5353
------------- | ------------- | ------------- | -------------
5454
**_api** | **DefaultApi** | API context |
55-
**any_of_mapped_pets** | [**AnyOfMappedPets**](AnyOfMappedPets.md)| |
55+
**any_of_mapped_pets** | [**AnyOfMappedPets**](AnyOfMappedPets.md) | |
5656

5757
### Return type
5858

@@ -80,7 +80,7 @@ No authorization required
8080
Name | Type | Description | Notes
8181
------------- | ------------- | ------------- | -------------
8282
**_api** | **DefaultApi** | API context |
83-
**any_of_pets** | [**AnyOfPets**](AnyOfPets.md)| |
83+
**any_of_pets** | [**AnyOfPets**](AnyOfPets.md) | |
8484

8585
### Return type
8686

@@ -108,7 +108,7 @@ No authorization required
108108
Name | Type | Description | Notes
109109
------------- | ------------- | ------------- | -------------
110110
**_api** | **DefaultApi** | API context |
111-
**type_with_all_array_types** | [**TypeWithAllArrayTypes**](TypeWithAllArrayTypes.md)| |
111+
**type_with_all_array_types** | [**TypeWithAllArrayTypes**](TypeWithAllArrayTypes.md) | |
112112

113113
### Return type
114114

@@ -136,7 +136,7 @@ No authorization required
136136
Name | Type | Description | Notes
137137
------------- | ------------- | ------------- | -------------
138138
**_api** | **DefaultApi** | API context |
139-
**one_of_base_type** | [**OneOfBaseType**](OneOfBaseType.md)| |
139+
**one_of_base_type** | [**OneOfBaseType**](OneOfBaseType.md) | |
140140

141141
### Return type
142142

@@ -164,7 +164,7 @@ No authorization required
164164
Name | Type | Description | Notes
165165
------------- | ------------- | ------------- | -------------
166166
**_api** | **DefaultApi** | API context |
167-
**one_of_mapped_pets** | [**OneOfMappedPets**](OneOfMappedPets.md)| |
167+
**one_of_mapped_pets** | [**OneOfMappedPets**](OneOfMappedPets.md) | |
168168

169169
### Return type
170170

@@ -192,7 +192,7 @@ No authorization required
192192
Name | Type | Description | Notes
193193
------------- | ------------- | ------------- | -------------
194194
**_api** | **DefaultApi** | API context |
195-
**one_of_pets** | [**OneOfPets**](OneOfPets.md)| |
195+
**one_of_pets** | [**OneOfPets**](OneOfPets.md) | |
196196

197197
### Return type
198198

test/client/allany/AllAnyClient/src/models/model_Cat.jl

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,28 @@ Base.@kwdef mutable struct Cat <: OpenAPI.APIModel
2020
age::Union{Nothing, Int64} = nothing
2121

2222
function Cat(pet_type, hunts, age, )
23-
OpenAPI.validate_property(Cat, Symbol("pet_type"), pet_type)
24-
OpenAPI.validate_property(Cat, Symbol("hunts"), hunts)
25-
OpenAPI.validate_property(Cat, Symbol("age"), age)
26-
return new(pet_type, hunts, age, )
23+
o = new(pet_type, hunts, age, )
24+
OpenAPI.validate_properties(o)
25+
return o
2726
end
2827
end # type Cat
2928

3029
const _property_types_Cat = Dict{Symbol,String}(Symbol("pet_type")=>"String", Symbol("hunts")=>"Bool", Symbol("age")=>"Int64", )
3130
OpenAPI.property_type(::Type{ Cat }, name::Symbol) = Union{Nothing,eval(Base.Meta.parse(_property_types_Cat[name]))}
3231

33-
function check_required(o::Cat)
32+
function OpenAPI.check_required(o::Cat)
3433
o.pet_type === nothing && (return false)
3534
true
3635
end
3736

37+
function OpenAPI.validate_properties(o::Cat)
38+
OpenAPI.validate_property(Cat, Symbol("pet_type"), o.pet_type)
39+
OpenAPI.validate_property(Cat, Symbol("hunts"), o.hunts)
40+
OpenAPI.validate_property(Cat, Symbol("age"), o.age)
41+
end
42+
3843
function OpenAPI.validate_property(::Type{ Cat }, name::Symbol, val)
44+
45+
46+
3947
end

test/client/allany/AllAnyClient/src/models/model_Dog.jl

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,23 +20,32 @@ Base.@kwdef mutable struct Dog <: OpenAPI.APIModel
2020
breed::Union{Nothing, String} = nothing
2121

2222
function Dog(pet_type, bark, breed, )
23-
OpenAPI.validate_property(Dog, Symbol("pet_type"), pet_type)
24-
OpenAPI.validate_property(Dog, Symbol("bark"), bark)
25-
OpenAPI.validate_property(Dog, Symbol("breed"), breed)
26-
return new(pet_type, bark, breed, )
23+
o = new(pet_type, bark, breed, )
24+
OpenAPI.validate_properties(o)
25+
return o
2726
end
2827
end # type Dog
2928

3029
const _property_types_Dog = Dict{Symbol,String}(Symbol("pet_type")=>"String", Symbol("bark")=>"Bool", Symbol("breed")=>"String", )
3130
OpenAPI.property_type(::Type{ Dog }, name::Symbol) = Union{Nothing,eval(Base.Meta.parse(_property_types_Dog[name]))}
3231

33-
function check_required(o::Dog)
32+
function OpenAPI.check_required(o::Dog)
3433
o.pet_type === nothing && (return false)
3534
true
3635
end
3736

37+
function OpenAPI.validate_properties(o::Dog)
38+
OpenAPI.validate_property(Dog, Symbol("pet_type"), o.pet_type)
39+
OpenAPI.validate_property(Dog, Symbol("bark"), o.bark)
40+
OpenAPI.validate_property(Dog, Symbol("breed"), o.breed)
41+
end
42+
3843
function OpenAPI.validate_property(::Type{ Dog }, name::Symbol, val)
44+
45+
46+
3947
if name === Symbol("breed")
4048
OpenAPI.validate_param(name, "Dog", :enum, val, ["Dingo", "Husky", "Retriever", "Shepherd"])
4149
end
50+
4251
end

test/client/allany/AllAnyClient/src/models/model_Pet.jl

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,24 @@ Base.@kwdef mutable struct Pet <: OpenAPI.APIModel
1414
pet_type::Union{Nothing, String} = nothing
1515

1616
function Pet(pet_type, )
17-
OpenAPI.validate_property(Pet, Symbol("pet_type"), pet_type)
18-
return new(pet_type, )
17+
o = new(pet_type, )
18+
OpenAPI.validate_properties(o)
19+
return o
1920
end
2021
end # type Pet
2122

2223
const _property_types_Pet = Dict{Symbol,String}(Symbol("pet_type")=>"String", )
2324
OpenAPI.property_type(::Type{ Pet }, name::Symbol) = Union{Nothing,eval(Base.Meta.parse(_property_types_Pet[name]))}
2425

25-
function check_required(o::Pet)
26+
function OpenAPI.check_required(o::Pet)
2627
o.pet_type === nothing && (return false)
2728
true
2829
end
2930

31+
function OpenAPI.validate_properties(o::Pet)
32+
OpenAPI.validate_property(Pet, Symbol("pet_type"), o.pet_type)
33+
end
34+
3035
function OpenAPI.validate_property(::Type{ Pet }, name::Symbol, val)
36+
3137
end

test/client/allany/AllAnyClient/src/models/model_TypeWithAllArrayTypes.jl

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,29 @@ Base.@kwdef mutable struct TypeWithAllArrayTypes <: OpenAPI.APIModel
2323
anyofpets::Union{Nothing, Vector} = nothing # spec type: Union{ Nothing, Vector{AnyOfPets} }
2424

2525
function TypeWithAllArrayTypes(oneofbase, anyofbase, oneofpets, anyofpets, )
26-
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("oneofbase"), oneofbase)
27-
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("anyofbase"), anyofbase)
28-
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("oneofpets"), oneofpets)
29-
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("anyofpets"), anyofpets)
30-
return new(oneofbase, anyofbase, oneofpets, anyofpets, )
26+
o = new(oneofbase, anyofbase, oneofpets, anyofpets, )
27+
OpenAPI.validate_properties(o)
28+
return o
3129
end
3230
end # type TypeWithAllArrayTypes
3331

3432
const _property_types_TypeWithAllArrayTypes = Dict{Symbol,String}(Symbol("oneofbase")=>"Vector{OneOfBaseType}", Symbol("anyofbase")=>"Vector{AnyOfBaseType}", Symbol("oneofpets")=>"Vector{OneOfPets}", Symbol("anyofpets")=>"Vector{AnyOfPets}", )
3533
OpenAPI.property_type(::Type{ TypeWithAllArrayTypes }, name::Symbol) = Union{Nothing,eval(Base.Meta.parse(_property_types_TypeWithAllArrayTypes[name]))}
3634

37-
function check_required(o::TypeWithAllArrayTypes)
35+
function OpenAPI.check_required(o::TypeWithAllArrayTypes)
3836
true
3937
end
4038

39+
function OpenAPI.validate_properties(o::TypeWithAllArrayTypes)
40+
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("oneofbase"), o.oneofbase)
41+
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("anyofbase"), o.anyofbase)
42+
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("oneofpets"), o.oneofpets)
43+
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("anyofpets"), o.anyofpets)
44+
end
45+
4146
function OpenAPI.validate_property(::Type{ TypeWithAllArrayTypes }, name::Symbol, val)
47+
48+
49+
50+
4251
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
7.0.0-SNAPSHOT
1+
7.13.0-SNAPSHOT

0 commit comments

Comments
 (0)