diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java index 4cbb6045ca0a..499e1c73ba4a 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/GoClientCodegen.java @@ -479,14 +479,22 @@ public ModelsMap postProcessModels(ModelsMap objs) { } // 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 diff --git a/modules/openapi-generator/src/main/resources/go/model_simple.mustache b/modules/openapi-generator/src/main/resources/go/model_simple.mustache index bbc53ddbbe55..97fdb24c1ee8 100644 --- a/modules/openapi-generator/src/main/resources/go/model_simple.mustache +++ b/modules/openapi-generator/src/main/resources/go/model_simple.mustache @@ -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, @@ -333,6 +339,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 { @@ -446,8 +484,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) { diff --git a/samples/client/echo_api/go/model_pet.go b/samples/client/echo_api/go/model_pet.go index 62ed88418efe..ca46658c236d 100644 --- a/samples/client/echo_api/go/model_pet.go +++ b/samples/client/echo_api/go/model_pet.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_animal.go b/samples/client/petstore/go/go-petstore/model_animal.go index a884b8473f60..bb96f81e892b 100644 --- a/samples/client/petstore/go/go-petstore/model_animal.go +++ b/samples/client/petstore/go/go-petstore/model_animal.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_big_cat.go b/samples/client/petstore/go/go-petstore/model_big_cat.go index 503a742ab8f5..4193ee0679c7 100644 --- a/samples/client/petstore/go/go-petstore/model_big_cat.go +++ b/samples/client/petstore/go/go-petstore/model_big_cat.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_cat.go b/samples/client/petstore/go/go-petstore/model_cat.go index ab791a527ac1..1c3b87e12af9 100644 --- a/samples/client/petstore/go/go-petstore/model_cat.go +++ b/samples/client/petstore/go/go-petstore/model_cat.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_category.go b/samples/client/petstore/go/go-petstore/model_category.go index 5275e7150648..b756bcf401c6 100644 --- a/samples/client/petstore/go/go-petstore/model_category.go +++ b/samples/client/petstore/go/go-petstore/model_category.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_dog.go b/samples/client/petstore/go/go-petstore/model_dog.go index f0fdbf8b7120..3c6a8dbd9e1f 100644 --- a/samples/client/petstore/go/go-petstore/model_dog.go +++ b/samples/client/petstore/go/go-petstore/model_dog.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_enum_test_.go b/samples/client/petstore/go/go-petstore/model_enum_test_.go index 25114a9f9bb2..64db2237811e 100644 --- a/samples/client/petstore/go/go-petstore/model_enum_test_.go +++ b/samples/client/petstore/go/go-petstore/model_enum_test_.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_format_test_.go b/samples/client/petstore/go/go-petstore/model_format_test_.go index 233c531076bb..0feac05dd05f 100644 --- a/samples/client/petstore/go/go-petstore/model_format_test_.go +++ b/samples/client/petstore/go/go-petstore/model_format_test_.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_name.go b/samples/client/petstore/go/go-petstore/model_name.go index e1fe302008f3..24741e9b9386 100644 --- a/samples/client/petstore/go/go-petstore/model_name.go +++ b/samples/client/petstore/go/go-petstore/model_name.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_pet.go b/samples/client/petstore/go/go-petstore/model_pet.go index 04ae05c1e182..d22f60a621a0 100644 --- a/samples/client/petstore/go/go-petstore/model_pet.go +++ b/samples/client/petstore/go/go-petstore/model_pet.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_type_holder_default.go b/samples/client/petstore/go/go-petstore/model_type_holder_default.go index a06478caad71..ac8e0889c046 100644 --- a/samples/client/petstore/go/go-petstore/model_type_holder_default.go +++ b/samples/client/petstore/go/go-petstore/model_type_holder_default.go @@ -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 diff --git a/samples/client/petstore/go/go-petstore/model_type_holder_example.go b/samples/client/petstore/go/go-petstore/model_type_holder_example.go index be5869db9b77..7ad8753a7852 100644 --- a/samples/client/petstore/go/go-petstore/model_type_holder_example.go +++ b/samples/client/petstore/go/go-petstore/model_type_holder_example.go @@ -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 diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_animal.go b/samples/openapi3/client/petstore/go/go-petstore/model_animal.go index c01aa9aeadc2..43c57d16ca47 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_animal.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_animal.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_apple_req.go b/samples/openapi3/client/petstore/go/go-petstore/model_apple_req.go index 23fa5ed2867b..72eac7b3602d 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_apple_req.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_apple_req.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_banana_req.go b/samples/openapi3/client/petstore/go/go-petstore/model_banana_req.go index 5d5923fc5a7c..2c9381772f41 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_banana_req.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_banana_req.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_cat.go b/samples/openapi3/client/petstore/go/go-petstore/model_cat.go index 440973ad5140..c01f8d5d0993 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_cat.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_cat.go @@ -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"` } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_category.go b/samples/openapi3/client/petstore/go/go-petstore/model_category.go index bd4d6815e72e..7f6f7865a46b 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_category.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_category.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_dog.go b/samples/openapi3/client/petstore/go/go-petstore/model_dog.go index 000bcf7e45fa..0b9680238fec 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_dog.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_dog.go @@ -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"` } diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_duplicated_prop_parent.go b/samples/openapi3/client/petstore/go/go-petstore/model_duplicated_prop_parent.go index bb2e2132e122..f378abf406a8 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_duplicated_prop_parent.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_duplicated_prop_parent.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_enum_test_.go b/samples/openapi3/client/petstore/go/go-petstore/model_enum_test_.go index eb00c70e14f6..317d3112bb65 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_enum_test_.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_enum_test_.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_format_test_.go b/samples/openapi3/client/petstore/go/go-petstore/model_format_test_.go index 499ceba4acb0..1fa58f722323 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_format_test_.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_format_test_.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_name.go b/samples/openapi3/client/petstore/go/go-petstore/model_name.go index 84a92878766a..ef0bece258a3 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_name.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_name.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_pet.go b/samples/openapi3/client/petstore/go/go-petstore/model_pet.go index 0f1ffe373e5b..d6f1a0a016f2 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_pet.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_pet.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_whale.go b/samples/openapi3/client/petstore/go/go-petstore/model_whale.go index 7e450a046cd9..a9810a7321d4 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_whale.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_whale.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_zebra.go b/samples/openapi3/client/petstore/go/go-petstore/model_zebra.go index 01a0ae804d41..41daab4f63dd 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_zebra.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_zebra.go @@ -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) diff --git a/samples/openapi3/client/petstore/go/model_test.go b/samples/openapi3/client/petstore/go/model_test.go index 34868a43fdb7..cc6eabbeaa0a 100644 --- a/samples/openapi3/client/petstore/go/model_test.go +++ b/samples/openapi3/client/petstore/go/model_test.go @@ -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") +}