rledisez 44ad6d5eac [go-server] Support min/max/defaults for values (#15185)
* [go-server] Support min/max/defaults for values

Enforce, for the go-server, to check the minimum and maximum values
specified in the openapi description. Also apply the default if the
parameter is not passed.

Fix #14013

* Fix merge conflict

Co-authored-by: Ween Jiann <16207788+lwj5@users.noreply.github.com>

* Improve UnmarshalJSON implementation

Co-authored-by: Ween Jiann <16207788+lwj5@users.noreply.github.com>

* Improve default value handling for string

Co-authored-by: Ween Jiann <16207788+lwj5@users.noreply.github.com>

* Fix suggested changes

* rework option pattern

* add imports based on types/min max values

---------

Co-authored-by: Ween Jiann <16207788+lwj5@users.noreply.github.com>
2023-05-17 01:58:28 +08:00

71 lines
1.5 KiB
Go

/*
* OpenAPI Petstore
*
* This is a sample server Petstore server. For this sample, you can use the api key `special-key` to test the authorization filters.
*
* API version: 1.0.0
* Generated by: OpenAPI Generator (https://openapi-generator.tech)
*/
package petstoreserver
import (
"encoding/json"
)
// Pet - A pet for sale in the pet store
type Pet struct {
Id int64 `json:"id,omitempty"`
Category Category `json:"category,omitempty"`
Name string `json:"name"`
PhotoUrls []string `json:"photoUrls"`
Tags []Tag `json:"tags,omitempty"`
// pet status in the store
// Deprecated
Status string `json:"status,omitempty"`
}
// UnmarshalJSON sets *m to a copy of data while respecting defaults if specified.
func (m *Pet) UnmarshalJSON(data []byte) error {
type Alias Pet // To avoid infinite recursion
return json.Unmarshal(data, (*Alias)(m))
}
// AssertPetRequired checks if the required fields are not zero-ed
func AssertPetRequired(obj Pet) error {
elements := map[string]interface{}{
"name": obj.Name,
"photoUrls": obj.PhotoUrls,
}
for name, el := range elements {
if isZero := IsZeroValue(el); isZero {
return &RequiredError{Field: name}
}
}
if err := AssertCategoryRequired(obj.Category); err != nil {
return err
}
for _, el := range obj.Tags {
if err := AssertTagRequired(el); err != nil {
return err
}
}
return nil
}
// AssertPetConstraints checks if the values respects the defined constraints
func AssertPetConstraints(obj Pet) error {
return nil
}