forked from loafle/openapi-generator-original
[Go] validate required fields when unmarshalling JSON (#16863)
* validate required properties when unmarshalling JSON * build project & update samples * Add test for required field validation
This commit is contained in:
parent
5d03c4ac82
commit
db9fd9a094
@ -484,14 +484,22 @@ public class GoClientCodegen extends AbstractGoCodegen {
|
||||
}
|
||||
|
||||
// additional import for different cases
|
||||
boolean addedFmtImport = false;
|
||||
|
||||
// oneOf
|
||||
if (model.oneOf != null && !model.oneOf.isEmpty()) {
|
||||
imports.add(createMapping("import", "fmt"));
|
||||
addedFmtImport = true;
|
||||
}
|
||||
|
||||
// anyOf
|
||||
if (model.anyOf != null && !model.anyOf.isEmpty()) {
|
||||
imports.add(createMapping("import", "fmt"));
|
||||
addedFmtImport = true;
|
||||
}
|
||||
|
||||
if (!addedFmtImport && model.hasRequired) {
|
||||
imports.add(createMapping("import", "fmt"));
|
||||
}
|
||||
|
||||
// additionalProperties: true and parent
|
||||
|
@ -32,6 +32,12 @@ type {{classname}} struct {
|
||||
{{#isAdditionalPropertiesTrue}}
|
||||
type _{{{classname}}} {{{classname}}}
|
||||
|
||||
{{/isAdditionalPropertiesTrue}}
|
||||
{{^isAdditionalPropertiesTrue}}
|
||||
{{#hasRequired}}
|
||||
type _{{{classname}}} {{{classname}}}
|
||||
|
||||
{{/hasRequired}}
|
||||
{{/isAdditionalPropertiesTrue}}
|
||||
// New{{classname}} instantiates a new {{classname}} object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
@ -335,6 +341,38 @@ func (o {{classname}}) ToMap() (map[string]interface{}, error) {
|
||||
|
||||
{{#isAdditionalPropertiesTrue}}
|
||||
func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
|
||||
{{/isAdditionalPropertiesTrue}}
|
||||
{{^isAdditionalPropertiesTrue}}
|
||||
{{#hasRequired}}
|
||||
func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
|
||||
{{/hasRequired}}
|
||||
{{/isAdditionalPropertiesTrue}}
|
||||
{{#hasRequired}}
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
{{#requiredVars}}
|
||||
"{{baseName}}",
|
||||
{{/requiredVars}}
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
{{/hasRequired}}
|
||||
{{#isAdditionalPropertiesTrue}}
|
||||
{{#parent}}
|
||||
{{^isMap}}
|
||||
type {{classname}}WithoutEmbeddedStruct struct {
|
||||
@ -448,8 +486,27 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
|
||||
|
||||
return err
|
||||
{{/parent}}
|
||||
{{/isAdditionalPropertiesTrue}}
|
||||
{{#isAdditionalPropertiesTrue}}
|
||||
}
|
||||
|
||||
{{/isAdditionalPropertiesTrue}}
|
||||
{{^isAdditionalPropertiesTrue}}
|
||||
{{#hasRequired}}
|
||||
var{{{classname}}} := _{{{classname}}}{}
|
||||
|
||||
err = json.Unmarshal(bytes, &var{{{classname}}})
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = {{{classname}}}(var{{{classname}}})
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
{{/hasRequired}}
|
||||
{{/isAdditionalPropertiesTrue}}
|
||||
{{#isArray}}
|
||||
func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
|
||||
|
@ -13,6 +13,7 @@ package openapi
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Pet type satisfies the MappedNullable interface at compile time
|
||||
@ -29,6 +30,8 @@ type Pet struct {
|
||||
Status *string `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
type _Pet Pet
|
||||
|
||||
// NewPet instantiates a new Pet object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -251,6 +254,42 @@ func (o Pet) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *Pet) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"name",
|
||||
"photoUrls",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varPet := _Pet{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varPet)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = Pet(varPet)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullablePet struct {
|
||||
value *Pet
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Animal type satisfies the MappedNullable interface at compile time
|
||||
@ -23,6 +24,8 @@ type Animal struct {
|
||||
Color *string `json:"color,omitempty"`
|
||||
}
|
||||
|
||||
type _Animal Animal
|
||||
|
||||
// NewAnimal instantiates a new Animal object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -118,6 +121,41 @@ func (o Animal) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *Animal) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"className",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varAnimal := _Animal{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varAnimal)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = Animal(varAnimal)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullableAnimal struct {
|
||||
value *Animal
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the BigCat type satisfies the MappedNullable interface at compile time
|
||||
@ -23,6 +24,8 @@ type BigCat struct {
|
||||
Kind *string `json:"kind,omitempty"`
|
||||
}
|
||||
|
||||
type _BigCat BigCat
|
||||
|
||||
// NewBigCat instantiates a new BigCat object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -99,6 +102,41 @@ func (o BigCat) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *BigCat) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"className",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varBigCat := _BigCat{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varBigCat)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = BigCat(varBigCat)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullableBigCat struct {
|
||||
value *BigCat
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Cat type satisfies the MappedNullable interface at compile time
|
||||
@ -23,6 +24,8 @@ type Cat struct {
|
||||
Declawed *bool `json:"declawed,omitempty"`
|
||||
}
|
||||
|
||||
type _Cat Cat
|
||||
|
||||
// NewCat instantiates a new Cat object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -99,6 +102,41 @@ func (o Cat) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *Cat) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"className",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varCat := _Cat{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varCat)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = Cat(varCat)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullableCat struct {
|
||||
value *Cat
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Category type satisfies the MappedNullable interface at compile time
|
||||
@ -23,6 +24,8 @@ type Category struct {
|
||||
Name string `json:"name"`
|
||||
}
|
||||
|
||||
type _Category Category
|
||||
|
||||
// NewCategory instantiates a new Category object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -116,6 +119,41 @@ func (o Category) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *Category) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"name",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varCategory := _Category{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varCategory)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = Category(varCategory)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullableCategory struct {
|
||||
value *Category
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Dog type satisfies the MappedNullable interface at compile time
|
||||
@ -23,6 +24,8 @@ type Dog struct {
|
||||
Breed *string `json:"breed,omitempty"`
|
||||
}
|
||||
|
||||
type _Dog Dog
|
||||
|
||||
// NewDog instantiates a new Dog object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -99,6 +102,41 @@ func (o Dog) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *Dog) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"className",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varDog := _Dog{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varDog)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = Dog(varDog)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullableDog struct {
|
||||
value *Dog
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the EnumTest type satisfies the MappedNullable interface at compile time
|
||||
@ -26,6 +27,8 @@ type EnumTest struct {
|
||||
OuterEnum *OuterEnum `json:"outerEnum,omitempty"`
|
||||
}
|
||||
|
||||
type _EnumTest EnumTest
|
||||
|
||||
// NewEnumTest instantiates a new EnumTest object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -222,6 +225,41 @@ func (o EnumTest) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *EnumTest) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"enum_string_required",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varEnumTest := _EnumTest{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varEnumTest)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = EnumTest(varEnumTest)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullableEnumTest struct {
|
||||
value *EnumTest
|
||||
isSet bool
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"time"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the FormatTest type satisfies the MappedNullable interface at compile time
|
||||
@ -37,6 +38,8 @@ type FormatTest struct {
|
||||
BigDecimal *float64 `json:"BigDecimal,omitempty"`
|
||||
}
|
||||
|
||||
type _FormatTest FormatTest
|
||||
|
||||
// NewFormatTest instantiates a new FormatTest object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -521,6 +524,44 @@ func (o FormatTest) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *FormatTest) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"number",
|
||||
"byte",
|
||||
"date",
|
||||
"password",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varFormatTest := _FormatTest{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varFormatTest)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = FormatTest(varFormatTest)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullableFormatTest struct {
|
||||
value *FormatTest
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Name type satisfies the MappedNullable interface at compile time
|
||||
@ -25,6 +26,8 @@ type Name struct {
|
||||
Var123Number *int32 `json:"123Number,omitempty"`
|
||||
}
|
||||
|
||||
type _Name Name
|
||||
|
||||
// NewName instantiates a new Name object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -186,6 +189,41 @@ func (o Name) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *Name) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"name",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varName := _Name{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varName)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = Name(varName)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullableName struct {
|
||||
value *Name
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Pet type satisfies the MappedNullable interface at compile time
|
||||
@ -28,6 +29,8 @@ type Pet struct {
|
||||
Status *string `json:"status,omitempty"`
|
||||
}
|
||||
|
||||
type _Pet Pet
|
||||
|
||||
// NewPet instantiates a new Pet object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -250,6 +253,42 @@ func (o Pet) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *Pet) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"name",
|
||||
"photoUrls",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varPet := _Pet{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varPet)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = Pet(varPet)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullablePet struct {
|
||||
value *Pet
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the TypeHolderDefault type satisfies the MappedNullable interface at compile time
|
||||
@ -26,6 +27,8 @@ type TypeHolderDefault struct {
|
||||
ArrayItem []int32 `json:"array_item"`
|
||||
}
|
||||
|
||||
type _TypeHolderDefault TypeHolderDefault
|
||||
|
||||
// NewTypeHolderDefault instantiates a new TypeHolderDefault object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -190,6 +193,45 @@ func (o TypeHolderDefault) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *TypeHolderDefault) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"string_item",
|
||||
"number_item",
|
||||
"integer_item",
|
||||
"bool_item",
|
||||
"array_item",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varTypeHolderDefault := _TypeHolderDefault{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varTypeHolderDefault)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = TypeHolderDefault(varTypeHolderDefault)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullableTypeHolderDefault struct {
|
||||
value *TypeHolderDefault
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the TypeHolderExample type satisfies the MappedNullable interface at compile time
|
||||
@ -27,6 +28,8 @@ type TypeHolderExample struct {
|
||||
ArrayItem []int32 `json:"array_item"`
|
||||
}
|
||||
|
||||
type _TypeHolderExample TypeHolderExample
|
||||
|
||||
// NewTypeHolderExample instantiates a new TypeHolderExample object
|
||||
// This constructor will assign default values to properties that have it defined,
|
||||
// and makes sure properties required by API are set, but the set of arguments
|
||||
@ -213,6 +216,46 @@ func (o TypeHolderExample) ToMap() (map[string]interface{}, error) {
|
||||
return toSerialize, nil
|
||||
}
|
||||
|
||||
func (o *TypeHolderExample) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"string_item",
|
||||
"number_item",
|
||||
"float_item",
|
||||
"integer_item",
|
||||
"bool_item",
|
||||
"array_item",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varTypeHolderExample := _TypeHolderExample{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varTypeHolderExample)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
*o = TypeHolderExample(varTypeHolderExample)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
type NullableTypeHolderExample struct {
|
||||
value *TypeHolderExample
|
||||
isSet bool
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Animal type satisfies the MappedNullable interface at compile time
|
||||
@ -127,6 +128,27 @@ func (o Animal) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *Animal) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"className",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varAnimal := _Animal{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varAnimal)
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the AppleReq type satisfies the MappedNullable interface at compile time
|
||||
@ -123,6 +124,27 @@ func (o AppleReq) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *AppleReq) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"cultivar",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varAppleReq := _AppleReq{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varAppleReq)
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the BananaReq type satisfies the MappedNullable interface at compile time
|
||||
@ -123,6 +124,27 @@ func (o BananaReq) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *BananaReq) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"lengthCm",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varBananaReq := _BananaReq{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varBananaReq)
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
@ -110,6 +111,27 @@ func (o Cat) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *Cat) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"className",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
type CatWithoutEmbeddedStruct struct {
|
||||
Declawed *bool `json:"declawed,omitempty"`
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Category type satisfies the MappedNullable interface at compile time
|
||||
@ -125,6 +126,27 @@ func (o Category) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *Category) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"name",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varCategory := _Category{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varCategory)
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"reflect"
|
||||
"strings"
|
||||
)
|
||||
@ -110,6 +111,27 @@ func (o Dog) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *Dog) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"className",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
type DogWithoutEmbeddedStruct struct {
|
||||
Breed *string `json:"breed,omitempty"`
|
||||
}
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the DuplicatedPropParent type satisfies the MappedNullable interface at compile time
|
||||
@ -88,6 +89,27 @@ func (o DuplicatedPropParent) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *DuplicatedPropParent) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"dup-prop",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varDuplicatedPropParent := _DuplicatedPropParent{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varDuplicatedPropParent)
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the EnumTest type satisfies the MappedNullable interface at compile time
|
||||
@ -357,6 +358,27 @@ func (o EnumTest) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *EnumTest) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"enum_string_required",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varEnumTest := _EnumTest{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varEnumTest)
|
||||
|
@ -14,6 +14,7 @@ import (
|
||||
"encoding/json"
|
||||
"os"
|
||||
"time"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the FormatTest type satisfies the MappedNullable interface at compile time
|
||||
@ -568,6 +569,30 @@ func (o FormatTest) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *FormatTest) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"number",
|
||||
"byte",
|
||||
"date",
|
||||
"password",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varFormatTest := _FormatTest{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varFormatTest)
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Name type satisfies the MappedNullable interface at compile time
|
||||
@ -195,6 +196,27 @@ func (o Name) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *Name) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"name",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varName := _Name{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varName)
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Pet type satisfies the MappedNullable interface at compile time
|
||||
@ -263,6 +264,28 @@ func (o Pet) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *Pet) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"name",
|
||||
"photoUrls",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varPet := _Pet{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varPet)
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Whale type satisfies the MappedNullable interface at compile time
|
||||
@ -159,6 +160,27 @@ func (o Whale) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *Whale) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"className",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varWhale := _Whale{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varWhale)
|
||||
|
@ -12,6 +12,7 @@ package petstore
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// checks if the Zebra type satisfies the MappedNullable interface at compile time
|
||||
@ -123,6 +124,27 @@ func (o Zebra) ToMap() (map[string]interface{}, error) {
|
||||
}
|
||||
|
||||
func (o *Zebra) UnmarshalJSON(bytes []byte) (err error) {
|
||||
// This validates that all required properties are included in the JSON object
|
||||
// by unmarshalling the object into a generic map with string keys and checking
|
||||
// that every required field exists as a key in the generic map.
|
||||
requiredProperties := []string{
|
||||
"className",
|
||||
}
|
||||
|
||||
allProperties := make(map[string]interface{})
|
||||
|
||||
err = json.Unmarshal(bytes, &allProperties)
|
||||
|
||||
if err != nil {
|
||||
return err;
|
||||
}
|
||||
|
||||
for _, requiredProperty := range(requiredProperties) {
|
||||
if _, exists := allProperties[requiredProperty]; !exists {
|
||||
return fmt.Errorf("no value given for required property %v", requiredProperty)
|
||||
}
|
||||
}
|
||||
|
||||
varZebra := _Zebra{}
|
||||
|
||||
err = json.Unmarshal(bytes, &varZebra)
|
||||
|
@ -4,8 +4,9 @@ import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
sw "go-petstore"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestBanana(t *testing.T) {
|
||||
@ -51,3 +52,14 @@ func TestReadOnlyFirst(t *testing.T) {
|
||||
|
||||
assert.Equal(expected, (string)(json), "ReadOnlyFirst JSON is incorrect")
|
||||
}
|
||||
|
||||
func TestRequiredFieldsAreValidated(t *testing.T) {
|
||||
assert := assert.New(t)
|
||||
|
||||
newPet := (sw.Pet{})
|
||||
jsonPet := `{"foo": "Foo value"}`
|
||||
err := newPet.UnmarshalJSON([]byte(jsonPet))
|
||||
expected := "no value given for required property"
|
||||
|
||||
assert.ErrorContains(err, expected, "Pet should return error when missing required fields")
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user