Skip to content

feat: helpers for serverside validations #89

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
with:
version: ${{ matrix.version }}
arch: ${{ matrix.arch }}
- uses: actions/cache@v1
- uses: actions/cache@v4
env:
cache-name: cache-artifacts
with:
Expand Down
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ keywords = ["Swagger", "OpenAPI", "REST"]
license = "MIT"
desc = "OpenAPI server and client helper for Julia"
authors = ["JuliaHub Inc."]
version = "0.1.28"
version = "0.2.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
2 changes: 2 additions & 0 deletions src/val.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,5 @@ function validate_param(parameter, operation_or_model, rule, value, args...)
end

validate_property(::Type{T}, name::Symbol, val) where {T<:APIModel} = nothing
validate_properties(::T) where {T<:APIModel} = nothing
check_required(::T) where {T<:APIModel} = true
2 changes: 1 addition & 1 deletion test/client/allany/AllAnyClient/.openapi-generator/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.1-SNAPSHOT
7.13.0-SNAPSHOT
1 change: 1 addition & 0 deletions test/client/allany/AllAnyClient/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ API to test code generation for oneof anyof allof
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.

- API version: 0.0.1
- Generator version: 7.13.0-SNAPSHOT
- Build package: org.openapitools.codegen.languages.JuliaClientCodegen


Expand Down
14 changes: 7 additions & 7 deletions test/client/allany/AllAnyClient/docs/DefaultApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Method | HTTP request | Description
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **DefaultApi** | API context |
**any_of_base_type** | [**AnyOfBaseType**](AnyOfBaseType.md)| |
**any_of_base_type** | [**AnyOfBaseType**](AnyOfBaseType.md) | |

### Return type

Expand Down Expand Up @@ -52,7 +52,7 @@ No authorization required
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **DefaultApi** | API context |
**any_of_mapped_pets** | [**AnyOfMappedPets**](AnyOfMappedPets.md)| |
**any_of_mapped_pets** | [**AnyOfMappedPets**](AnyOfMappedPets.md) | |

### Return type

Expand Down Expand Up @@ -80,7 +80,7 @@ No authorization required
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **DefaultApi** | API context |
**any_of_pets** | [**AnyOfPets**](AnyOfPets.md)| |
**any_of_pets** | [**AnyOfPets**](AnyOfPets.md) | |

### Return type

Expand Down Expand Up @@ -108,7 +108,7 @@ No authorization required
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **DefaultApi** | API context |
**type_with_all_array_types** | [**TypeWithAllArrayTypes**](TypeWithAllArrayTypes.md)| |
**type_with_all_array_types** | [**TypeWithAllArrayTypes**](TypeWithAllArrayTypes.md) | |

### Return type

Expand Down Expand Up @@ -136,7 +136,7 @@ No authorization required
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **DefaultApi** | API context |
**one_of_base_type** | [**OneOfBaseType**](OneOfBaseType.md)| |
**one_of_base_type** | [**OneOfBaseType**](OneOfBaseType.md) | |

### Return type

Expand Down Expand Up @@ -164,7 +164,7 @@ No authorization required
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **DefaultApi** | API context |
**one_of_mapped_pets** | [**OneOfMappedPets**](OneOfMappedPets.md)| |
**one_of_mapped_pets** | [**OneOfMappedPets**](OneOfMappedPets.md) | |

### Return type

Expand Down Expand Up @@ -192,7 +192,7 @@ No authorization required
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **DefaultApi** | API context |
**one_of_pets** | [**OneOfPets**](OneOfPets.md)| |
**one_of_pets** | [**OneOfPets**](OneOfPets.md) | |

### Return type

Expand Down
18 changes: 13 additions & 5 deletions test/client/allany/AllAnyClient/src/models/model_Cat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,20 +20,28 @@ Base.@kwdef mutable struct Cat <: OpenAPI.APIModel
age::Union{Nothing, Int64} = nothing

function Cat(pet_type, hunts, age, )
OpenAPI.validate_property(Cat, Symbol("pet_type"), pet_type)
OpenAPI.validate_property(Cat, Symbol("hunts"), hunts)
OpenAPI.validate_property(Cat, Symbol("age"), age)
return new(pet_type, hunts, age, )
o = new(pet_type, hunts, age, )
OpenAPI.validate_properties(o)
return o
end
end # type Cat

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

function check_required(o::Cat)
function OpenAPI.check_required(o::Cat)
o.pet_type === nothing && (return false)
true
end

function OpenAPI.validate_properties(o::Cat)
OpenAPI.validate_property(Cat, Symbol("pet_type"), o.pet_type)
OpenAPI.validate_property(Cat, Symbol("hunts"), o.hunts)
OpenAPI.validate_property(Cat, Symbol("age"), o.age)
end

function OpenAPI.validate_property(::Type{ Cat }, name::Symbol, val)



end
19 changes: 14 additions & 5 deletions test/client/allany/AllAnyClient/src/models/model_Dog.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,32 @@ Base.@kwdef mutable struct Dog <: OpenAPI.APIModel
breed::Union{Nothing, String} = nothing

function Dog(pet_type, bark, breed, )
OpenAPI.validate_property(Dog, Symbol("pet_type"), pet_type)
OpenAPI.validate_property(Dog, Symbol("bark"), bark)
OpenAPI.validate_property(Dog, Symbol("breed"), breed)
return new(pet_type, bark, breed, )
o = new(pet_type, bark, breed, )
OpenAPI.validate_properties(o)
return o
end
end # type Dog

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

function check_required(o::Dog)
function OpenAPI.check_required(o::Dog)
o.pet_type === nothing && (return false)
true
end

function OpenAPI.validate_properties(o::Dog)
OpenAPI.validate_property(Dog, Symbol("pet_type"), o.pet_type)
OpenAPI.validate_property(Dog, Symbol("bark"), o.bark)
OpenAPI.validate_property(Dog, Symbol("breed"), o.breed)
end

function OpenAPI.validate_property(::Type{ Dog }, name::Symbol, val)



if name === Symbol("breed")
OpenAPI.validate_param(name, "Dog", :enum, val, ["Dingo", "Husky", "Retriever", "Shepherd"])
end

end
12 changes: 9 additions & 3 deletions test/client/allany/AllAnyClient/src/models/model_Pet.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,24 @@ Base.@kwdef mutable struct Pet <: OpenAPI.APIModel
pet_type::Union{Nothing, String} = nothing

function Pet(pet_type, )
OpenAPI.validate_property(Pet, Symbol("pet_type"), pet_type)
return new(pet_type, )
o = new(pet_type, )
OpenAPI.validate_properties(o)
return o
end
end # type Pet

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

function check_required(o::Pet)
function OpenAPI.check_required(o::Pet)
o.pet_type === nothing && (return false)
true
end

function OpenAPI.validate_properties(o::Pet)
OpenAPI.validate_property(Pet, Symbol("pet_type"), o.pet_type)
end

function OpenAPI.validate_property(::Type{ Pet }, name::Symbol, val)

end
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,29 @@ Base.@kwdef mutable struct TypeWithAllArrayTypes <: OpenAPI.APIModel
anyofpets::Union{Nothing, Vector} = nothing # spec type: Union{ Nothing, Vector{AnyOfPets} }

function TypeWithAllArrayTypes(oneofbase, anyofbase, oneofpets, anyofpets, )
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("oneofbase"), oneofbase)
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("anyofbase"), anyofbase)
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("oneofpets"), oneofpets)
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("anyofpets"), anyofpets)
return new(oneofbase, anyofbase, oneofpets, anyofpets, )
o = new(oneofbase, anyofbase, oneofpets, anyofpets, )
OpenAPI.validate_properties(o)
return o
end
end # type TypeWithAllArrayTypes

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

function check_required(o::TypeWithAllArrayTypes)
function OpenAPI.check_required(o::TypeWithAllArrayTypes)
true
end

function OpenAPI.validate_properties(o::TypeWithAllArrayTypes)
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("oneofbase"), o.oneofbase)
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("anyofbase"), o.anyofbase)
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("oneofpets"), o.oneofpets)
OpenAPI.validate_property(TypeWithAllArrayTypes, Symbol("anyofpets"), o.anyofpets)
end

function OpenAPI.validate_property(::Type{ TypeWithAllArrayTypes }, name::Symbol, val)




end
Original file line number Diff line number Diff line change
@@ -1 +1 @@
7.0.0-SNAPSHOT
7.13.0-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ This is a sample server Petstore server. For this sample, you can use the api ke
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.

- API version: 1.0.0
- Generator version: 7.13.0-SNAPSHOT
- Build package: org.openapitools.codegen.languages.JuliaClientCodegen


Expand Down
26 changes: 13 additions & 13 deletions test/client/openapigenerator_petstore_v3/petstore/docs/PetApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Add a new pet to the store
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **PetApi** | API context |
**pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store |
**pet** | [**Pet**](Pet.md) | Pet object that needs to be added to the store |

### Return type

Expand Down Expand Up @@ -57,13 +57,13 @@ Deletes a pet
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **PetApi** | API context |
**pet_id** | **Int64**| Pet id to delete | [default to nothing]
**pet_id** | **Int64** | Pet id to delete |

### Optional Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**api_key** | **String**| | [default to nothing]
**api_key** | **String** | | [default to nothing]

### Return type

Expand Down Expand Up @@ -93,7 +93,7 @@ Multiple status values can be provided with comma separated strings
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **PetApi** | API context |
**status** | [**Vector{String}**](String.md)| Status values that need to be considered for filter | [default to nothing]
**status** | [**Vector{String}**](String.md) | Status values that need to be considered for filter |

### Return type

Expand Down Expand Up @@ -123,7 +123,7 @@ Multiple tags can be provided with comma separated strings. Use tag1, tag2, tag3
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **PetApi** | API context |
**tags** | [**Vector{String}**](String.md)| Tags to filter by | [default to nothing]
**tags** | [**Vector{String}**](String.md) | Tags to filter by |

### Return type

Expand Down Expand Up @@ -153,7 +153,7 @@ Returns a single pet
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **PetApi** | API context |
**pet_id** | **Int64**| ID of pet to return | [default to nothing]
**pet_id** | **Int64** | ID of pet to return |

### Return type

Expand Down Expand Up @@ -183,7 +183,7 @@ Update an existing pet
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **PetApi** | API context |
**pet** | [**Pet**](Pet.md)| Pet object that needs to be added to the store |
**pet** | [**Pet**](Pet.md) | Pet object that needs to be added to the store |

### Return type

Expand Down Expand Up @@ -213,14 +213,14 @@ Updates a pet in the store with form data
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **PetApi** | API context |
**pet_id** | **Int64**| ID of pet that needs to be updated | [default to nothing]
**pet_id** | **Int64** | ID of pet that needs to be updated |

### Optional Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**name** | **String**| Updated name of the pet | [default to nothing]
**status** | **String**| Updated status of the pet | [default to nothing]
**name** | **String** | Updated name of the pet | [default to nothing]
**status** | **String** | Updated status of the pet | [default to nothing]

### Return type

Expand Down Expand Up @@ -250,14 +250,14 @@ uploads an image
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **PetApi** | API context |
**pet_id** | **Int64**| ID of pet to update | [default to nothing]
**pet_id** | **Int64** | ID of pet to update |

### Optional Parameters

Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**file** | **String****String**| file to upload | [default to nothing]
**additional_metadata** | **String**| Additional data to pass to server | [default to nothing]
**file** | **String** | file to upload |
**additional_metadata** | **String** | Additional data to pass to server | [default to nothing]

### Return type

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ For valid response try integer IDs with value < 1000. Anything above 1000 or non
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **StoreApi** | API context |
**order_id** | **String**| ID of the order that needs to be deleted | [default to nothing]
**order_id** | **String** | ID of the order that needs to be deleted |

### Return type

Expand Down Expand Up @@ -79,7 +79,7 @@ For valid response try integer IDs with value <= 5 or > 10. Other values will ge
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **StoreApi** | API context |
**order_id** | **Int64**| ID of pet that needs to be fetched | [default to nothing]
**order_id** | **Int64** | ID of pet that needs to be fetched |

### Return type

Expand Down Expand Up @@ -109,7 +109,7 @@ Place an order for a pet
Name | Type | Description | Notes
------------- | ------------- | ------------- | -------------
**_api** | **StoreApi** | API context |
**order** | [**Order**](Order.md)| order placed for purchasing the pet |
**order** | [**Order**](Order.md) | order placed for purchasing the pet |

### Return type

Expand Down
Loading
Loading