[Go] Set Default Values for Required Variables when a default is defined (#19232)

* use default values for required vars when available

* update tests to existing config file

* cleanup OAS test

* Remove setting value in Get.

* Use Pascal Case for variable naming in Getter Function

* add a CLI option

* add a CLI option

* add a CLI option
This commit is contained in:
Vikrant Balyan 2024-07-30 16:02:54 +05:30 committed by GitHub
parent 753502c255
commit 5d48d224d9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
38 changed files with 395 additions and 14 deletions

View File

@ -15,6 +15,7 @@ additionalProperties:
packageName: petstore
disallowAdditionalPropertiesIfNotPresent: false
generateInterfaces: true
useDefaultValuesForRequiredVars: "true"
enumNameMappings:
delivered: SHIPPED

View File

@ -29,6 +29,7 @@ These options may be applied as additional-properties (cli) or configOptions (pl
|packageVersion|Go package version.| |1.0.0|
|prependFormOrBodyParameters|Add form or body parameters to the beginning of the parameter list.| |false|
|structPrefix|whether to prefix struct with the class name. e.g. DeletePetOpts => PetApiDeletePetOpts| |false|
|useDefaultValuesForRequiredVars|Use default values for required variables when available| |false|
|useOneOfDiscriminatorLookup|Use the discriminator's mapping in oneOf to speed up the model lookup. IMPORTANT: Validation (e.g. one and only one match in oneOf's schemas) will be skipped.| |false|
|withAWSV4Signature|whether to include AWS v4 signature support| |false|
|withGoMod|Generate go.mod and go.sum| |true|

View File

@ -451,4 +451,6 @@ public class CodegenConstants {
public static final String MAX_ATTEMPTS_FOR_RETRY = "maxAttemptsForRetry";
public static final String WAIT_TIME_OF_THREAD = "waitTimeMillis";
public static final String USE_DEFAULT_VALUES_FOR_REQUIRED_VARS = "useDefaultValuesForRequiredVars";
}

View File

@ -58,6 +58,8 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege
protected boolean generateMarshalJSON = true;
@Setter
protected boolean generateUnmarshalJSON = true;
@Setter
protected boolean useDefaultValuesForRequiredVars = false;
@Setter
protected String packageName = "openapi";

View File

@ -55,6 +55,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
public static final String GENERATE_INTERFACES = "generateInterfaces";
public static final String MODEL_FILE_FOLDER = "modelFileFolder";
public static final String WITH_GO_MOD = "withGoMod";
public static final String USE_DEFAULT_VALUES_FOR_REQUIRED_VARS = "useDefaultValuesForRequiredVars";
@Setter protected String goImportAlias = "openapiclient";
protected boolean isGoSubmodule = false;
@Setter protected boolean useOneOfDiscriminatorLookup = false; // use oneOf discriminator's mapping for model lookup
@ -126,6 +127,7 @@ public class GoClientCodegen extends AbstractGoCodegen {
cliOptions.add(CliOption.newBoolean(STRUCT_PREFIX, "whether to prefix struct with the class name. e.g. DeletePetOpts => PetApiDeletePetOpts"));
cliOptions.add(CliOption.newBoolean(WITH_AWSV4_SIGNATURE, "whether to include AWS v4 signature support"));
cliOptions.add(CliOption.newBoolean(GENERATE_INTERFACES, "Generate interfaces for api classes"));
cliOptions.add(CliOption.newBoolean(USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, "Use default values for required variables when available"));
// option to change the order of form/body parameter
cliOptions.add(CliOption.newBoolean(
@ -246,6 +248,11 @@ public class GoClientCodegen extends AbstractGoCodegen {
additionalProperties.put(GENERATE_INTERFACES, generateInterfaces);
}
if (additionalProperties.containsKey(USE_DEFAULT_VALUES_FOR_REQUIRED_VARS)) {
setUseDefaultValuesForRequiredVars(Boolean.parseBoolean(additionalProperties.get(USE_DEFAULT_VALUES_FOR_REQUIRED_VARS).toString()));
additionalProperties.put(USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, useDefaultValuesForRequiredVars);
}
// Generate the 'signing.py' module, but only if the 'HTTP signature' security scheme is specified in the OAS.
Map<String, SecurityScheme> securitySchemeMap = openAPI != null ?
(openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null) : null;

View File

@ -168,6 +168,18 @@ func (o *{{classname}}) Set{{name}}(v {{vendorExtensions.x-go-base-type}}) {
{{/isNullable}}
}
{{#useDefaultValuesForRequiredVars}}
{{^isReadOnly}}
{{#defaultValue}}
// GetDefault{{baseName}} function assigns the default value {{defaultValue}} to the {{name}} field
// of the {{classname}} struct and returns the {{{defaultValue}}}.
func (o *{{classname}}) GetDefault{{nameInPascalCase}}() interface{} {
return {{{defaultValue}}}
}
{{/defaultValue}}
{{/isReadOnly}}
{{/useDefaultValuesForRequiredVars}}
{{/required}}
{{^required}}
// Get{{name}} returns the {{name}} field value if set, zero value otherwise{{#isNullable}} (both if not set or set to explicit null){{/isNullable}}.
@ -318,6 +330,15 @@ func (o {{classname}}) ToMap() (map[string]interface{}, error) {
{{! if argument is not nullable, don't set it if it is nil}}
{{^isNullable}}
{{#required}}
{{#useDefaultValuesForRequiredVars}}
{{^isReadOnly}}
{{#defaultValue}}
if _, exists := toSerialize["{{{baseName}}}"]; !exists {
toSerialize["{{{baseName}}}"] = o.GetDefault{{nameInPascalCase}}()
}
{{/defaultValue}}
{{/isReadOnly}}
{{/useDefaultValuesForRequiredVars}}
toSerialize["{{{baseName}}}"] = o.{{name}}
{{/required}}
{{^required}}
@ -356,6 +377,20 @@ func (o *{{{classname}}}) UnmarshalJSON(data []byte) (err error) {
{{/requiredVars}}
}
{{#useDefaultValuesForRequiredVars}}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
{{#requiredVars}}
{{#defaultValue}}
{{^isReadOnly}}
"{{baseName}}": o.GetDefault{{nameInPascalCase}},
{{/isReadOnly}}
{{/defaultValue}}
{{/requiredVars}}
}
var defaultValueApplied bool
{{/useDefaultValuesForRequiredVars}}
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -365,11 +400,30 @@ func (o *{{{classname}}}) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
{{#useDefaultValuesForRequiredVars}}
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
{{/useDefaultValuesForRequiredVars}}
{{^useDefaultValuesForRequiredVars}}
if _, exists := allProperties[requiredProperty]; !exists {
{{/useDefaultValuesForRequiredVars}}
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
{{#useDefaultValuesForRequiredVars}}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
{{/useDefaultValuesForRequiredVars}}
{{/hasRequired}}
{{#isAdditionalPropertiesTrue}}
{{#parent}}

View File

@ -54,5 +54,6 @@ public class GoClientOptionsTest extends AbstractOptionsTest {
verify(clientCodegen).setWithGoMod(GoClientOptionsProvider.WITH_GO_MOD_VALUE);
verify(clientCodegen).setGenerateMarshalJSON(GoClientOptionsProvider.GENERATE_MARSHAL_JSON_VALUE);
verify(clientCodegen).setGenerateUnmarshalJSON(GoClientOptionsProvider.GENERATE_UNMARSHAL_JSON_VALUE);
verify(clientCodegen).setUseDefaultValuesForRequiredVars(GoClientOptionsProvider.USE_DEFAULT_VALUES_FOR_REQUIRED_VARS_VALUE);
}
}

View File

@ -39,6 +39,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
public static final boolean WITH_GO_MOD_VALUE = true;
public static final boolean GENERATE_MARSHAL_JSON_VALUE = true;
public static final boolean GENERATE_UNMARSHAL_JSON_VALUE = true;
public static final boolean USE_DEFAULT_VALUES_FOR_REQUIRED_VARS_VALUE = true;
@Override
public String getLanguage() {
@ -64,6 +65,7 @@ public class GoClientOptionsProvider implements OptionsProvider {
.put(CodegenConstants.GENERATE_UNMARSHAL_JSON, "true")
.put("generateInterfaces", "true")
.put("structPrefix", "true")
.put(CodegenConstants.USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, "true")
.build();
}

View File

@ -108,6 +108,7 @@ func (o *Pet) SetName(v string) {
o.Name = v
}
// GetCategory returns the Category field value if set, zero value otherwise.
func (o *Pet) GetCategory() Category {
if o == nil || IsNil(o.Category) {
@ -164,6 +165,7 @@ func (o *Pet) SetPhotoUrls(v []string) {
o.PhotoUrls = v
}
// GetTags returns the Tags field value if set, zero value otherwise.
func (o *Pet) GetTags() []Tag {
if o == nil || IsNil(o.Tags) {

View File

@ -108,6 +108,7 @@ func (o *Pet) SetName(v string) {
o.Name = v
}
// GetCategory returns the Category field value if set, zero value otherwise.
func (o *Pet) GetCategory() Category {
if o == nil || IsNil(o.Category) {
@ -164,6 +165,7 @@ func (o *Pet) SetPhotoUrls(v []string) {
o.PhotoUrls = v
}
// GetTags returns the Tags field value if set, zero value otherwise.
func (o *Pet) GetTags() []Tag {
if o == nil || IsNil(o.Tags) {

View File

@ -74,6 +74,7 @@ func (o *AdditionalData) SetProp1(v string) {
o.Prop1 = v
}
// GetQuantity returns the Quantity field value
func (o *AdditionalData) GetQuantity() int32 {
if o == nil {
@ -98,6 +99,7 @@ func (o *AdditionalData) SetQuantity(v int32) {
o.Quantity = v
}
// GetUnitPrice returns the UnitPrice field value
func (o *AdditionalData) GetUnitPrice() float64 {
if o == nil {
@ -122,6 +124,7 @@ func (o *AdditionalData) SetUnitPrice(v float64) {
o.UnitPrice = v
}
// GetTotalPrice returns the TotalPrice field value
func (o *AdditionalData) GetTotalPrice() float64 {
if o == nil {
@ -146,6 +149,7 @@ func (o *AdditionalData) SetTotalPrice(v float64) {
o.TotalPrice = v
}
func (o AdditionalData) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {

View File

@ -70,6 +70,7 @@ func (o *BaseItem) SetTitle(v string) {
o.Title = v
}
// GetType returns the Type field value
func (o *BaseItem) GetType() string {
if o == nil {
@ -94,6 +95,7 @@ func (o *BaseItem) SetType(v string) {
o.Type = v
}
func (o BaseItem) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {

View File

@ -77,6 +77,7 @@ func (o *FinalItem) SetProp1(v string) {
o.Prop1 = v
}
// GetQuantity returns the Quantity field value
func (o *FinalItem) GetQuantity() int32 {
if o == nil {
@ -101,6 +102,7 @@ func (o *FinalItem) SetQuantity(v int32) {
o.Quantity = v
}
// GetUnitPrice returns the UnitPrice field value
func (o *FinalItem) GetUnitPrice() float64 {
if o == nil {
@ -125,6 +127,7 @@ func (o *FinalItem) SetUnitPrice(v float64) {
o.UnitPrice = v
}
// GetTotalPrice returns the TotalPrice field value
func (o *FinalItem) GetTotalPrice() float64 {
if o == nil {
@ -149,6 +152,7 @@ func (o *FinalItem) SetTotalPrice(v float64) {
o.TotalPrice = v
}
func (o FinalItem) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {

View File

@ -69,6 +69,7 @@ func (o *NestedObject1) SetField1(v string) {
o.Field1 = v
}
func (o NestedObject1) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {

View File

@ -69,6 +69,7 @@ func (o *NestedObject2) SetField2(v string) {
o.Field2 = v
}
func (o NestedObject2) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {

View File

@ -73,6 +73,7 @@ func (o *Animal) SetClassName(v string) {
o.ClassName = v
}
// GetColor returns the Color field value if set, zero value otherwise.
func (o *Animal) GetColor() string {
if o == nil || IsNil(o.Color) {

View File

@ -103,6 +103,7 @@ func (o *Category) SetName(v string) {
o.Name = v
}
func (o Category) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {

View File

@ -104,6 +104,7 @@ func (o *EnumTest) SetEnumStringRequired(v string) {
o.EnumStringRequired = v
}
// GetEnumInteger returns the EnumInteger field value if set, zero value otherwise.
func (o *EnumTest) GetEnumInteger() int32 {
if o == nil || IsNil(o.EnumInteger) {

View File

@ -182,6 +182,7 @@ func (o *FormatTest) SetNumber(v float32) {
o.Number = v
}
// GetFloat returns the Float field value if set, zero value otherwise.
func (o *FormatTest) GetFloat() float32 {
if o == nil || IsNil(o.Float) {
@ -302,6 +303,7 @@ func (o *FormatTest) SetByte(v string) {
o.Byte = v
}
// GetBinary returns the Binary field value if set, zero value otherwise.
func (o *FormatTest) GetBinary() *os.File {
if o == nil || IsNil(o.Binary) {
@ -358,6 +360,7 @@ func (o *FormatTest) SetDate(v string) {
o.Date = v
}
// GetDateTime returns the DateTime field value if set, zero value otherwise.
func (o *FormatTest) GetDateTime() time.Time {
if o == nil || IsNil(o.DateTime) {
@ -446,6 +449,7 @@ func (o *FormatTest) SetPassword(v string) {
o.Password = v
}
// GetBigDecimal returns the BigDecimal field value if set, zero value otherwise.
func (o *FormatTest) GetBigDecimal() float64 {
if o == nil || IsNil(o.BigDecimal) {

View File

@ -71,6 +71,7 @@ func (o *Name) SetName(v int32) {
o.Name = v
}
// GetSnakeCase returns the SnakeCase field value if set, zero value otherwise.
func (o *Name) GetSnakeCase() int32 {
if o == nil || IsNil(o.SnakeCase) {

View File

@ -139,6 +139,7 @@ func (o *Pet) SetName(v string) {
o.Name = v
}
// GetPhotoUrls returns the PhotoUrls field value
func (o *Pet) GetPhotoUrls() []string {
if o == nil {
@ -163,6 +164,7 @@ func (o *Pet) SetPhotoUrls(v []string) {
o.PhotoUrls = v
}
// GetTags returns the Tags field value if set, zero value otherwise.
func (o *Pet) GetTags() []Tag {
if o == nil || IsNil(o.Tags) {

View File

@ -80,6 +80,7 @@ func (o *TypeHolderDefault) SetStringItem(v string) {
o.StringItem = v
}
// GetNumberItem returns the NumberItem field value
func (o *TypeHolderDefault) GetNumberItem() float32 {
if o == nil {
@ -104,6 +105,7 @@ func (o *TypeHolderDefault) SetNumberItem(v float32) {
o.NumberItem = v
}
// GetIntegerItem returns the IntegerItem field value
func (o *TypeHolderDefault) GetIntegerItem() int32 {
if o == nil {
@ -128,6 +130,7 @@ func (o *TypeHolderDefault) SetIntegerItem(v int32) {
o.IntegerItem = v
}
// GetBoolItem returns the BoolItem field value
func (o *TypeHolderDefault) GetBoolItem() bool {
if o == nil {
@ -152,6 +155,7 @@ func (o *TypeHolderDefault) SetBoolItem(v bool) {
o.BoolItem = v
}
// GetArrayItem returns the ArrayItem field value
func (o *TypeHolderDefault) GetArrayItem() []int32 {
if o == nil {
@ -176,6 +180,7 @@ func (o *TypeHolderDefault) SetArrayItem(v []int32) {
o.ArrayItem = v
}
func (o TypeHolderDefault) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {

View File

@ -78,6 +78,7 @@ func (o *TypeHolderExample) SetStringItem(v string) {
o.StringItem = v
}
// GetNumberItem returns the NumberItem field value
func (o *TypeHolderExample) GetNumberItem() float32 {
if o == nil {
@ -102,6 +103,7 @@ func (o *TypeHolderExample) SetNumberItem(v float32) {
o.NumberItem = v
}
// GetFloatItem returns the FloatItem field value
func (o *TypeHolderExample) GetFloatItem() float32 {
if o == nil {
@ -126,6 +128,7 @@ func (o *TypeHolderExample) SetFloatItem(v float32) {
o.FloatItem = v
}
// GetIntegerItem returns the IntegerItem field value
func (o *TypeHolderExample) GetIntegerItem() int32 {
if o == nil {
@ -150,6 +153,7 @@ func (o *TypeHolderExample) SetIntegerItem(v int32) {
o.IntegerItem = v
}
// GetBoolItem returns the BoolItem field value
func (o *TypeHolderExample) GetBoolItem() bool {
if o == nil {
@ -174,6 +178,7 @@ func (o *TypeHolderExample) SetBoolItem(v bool) {
o.BoolItem = v
}
// GetArrayItem returns the ArrayItem field value
func (o *TypeHolderExample) GetArrayItem() []int32 {
if o == nil {
@ -198,6 +203,7 @@ func (o *TypeHolderExample) SetArrayItem(v []int32) {
o.ArrayItem = v
}
func (o TypeHolderExample) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {

View File

@ -140,6 +140,7 @@ func (o *Pet) SetName(v string) {
o.Name = v
}
// GetPhotoUrls returns the PhotoUrls field value
func (o *Pet) GetPhotoUrls() []string {
if o == nil {
@ -164,6 +165,7 @@ func (o *Pet) SetPhotoUrls(v []string) {
o.PhotoUrls = v
}
// GetTags returns the Tags field value if set, zero value otherwise.
func (o *Pet) GetTags() []Tag {
if o == nil || IsNil(o.Tags) {

View File

@ -73,6 +73,7 @@ func (o *Animal) SetClassName(v string) {
o.ClassName = v
}
// GetColor returns the Color field value if set, zero value otherwise.
func (o *Animal) GetColor() string {
if o == nil || IsNil(o.Color) {
@ -135,6 +136,11 @@ func (o *Animal) UnmarshalJSON(data []byte) (err error) {
"className",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -144,11 +150,23 @@ func (o *Animal) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varAnimal := _Animal{}
err = json.Unmarshal(data, &varAnimal)

View File

@ -69,6 +69,7 @@ func (o *AppleReq) SetCultivar(v string) {
o.Cultivar = v
}
// GetMealy returns the Mealy field value if set, zero value otherwise.
func (o *AppleReq) GetMealy() bool {
if o == nil || IsNil(o.Mealy) {
@ -131,6 +132,11 @@ func (o *AppleReq) UnmarshalJSON(data []byte) (err error) {
"cultivar",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -140,11 +146,23 @@ func (o *AppleReq) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varAppleReq := _AppleReq{}
err = json.Unmarshal(data, &varAppleReq)

View File

@ -69,6 +69,7 @@ func (o *BananaReq) SetLengthCm(v float32) {
o.LengthCm = v
}
// GetSweet returns the Sweet field value if set, zero value otherwise.
func (o *BananaReq) GetSweet() bool {
if o == nil || IsNil(o.Sweet) {
@ -131,6 +132,11 @@ func (o *BananaReq) UnmarshalJSON(data []byte) (err error) {
"lengthCm",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -140,11 +146,23 @@ func (o *BananaReq) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varBananaReq := _BananaReq{}
err = json.Unmarshal(data, &varBananaReq)

View File

@ -118,6 +118,11 @@ func (o *Cat) UnmarshalJSON(data []byte) (err error) {
"className",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -127,11 +132,23 @@ func (o *Cat) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
type CatWithoutEmbeddedStruct struct {
Declawed *bool `json:"declawed,omitempty"`
}

View File

@ -103,6 +103,12 @@ func (o *Category) SetName(v string) {
o.Name = v
}
// GetDefaultname function assigns the default value &quot;default-name&quot; to the Name field
// of the Category struct and returns the "default-name".
func (o *Category) GetDefaultName() interface{} {
return "default-name"
}
func (o Category) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {
@ -116,6 +122,9 @@ func (o Category) ToMap() (map[string]interface{}, error) {
if !IsNil(o.Id) {
toSerialize["id"] = o.Id
}
if _, exists := toSerialize["name"]; !exists {
toSerialize["name"] = o.GetDefaultName()
}
toSerialize["name"] = o.Name
for key, value := range o.AdditionalProperties {
@ -133,6 +142,12 @@ func (o *Category) UnmarshalJSON(data []byte) (err error) {
"name",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
"name": o.GetDefaultName,
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -142,11 +157,23 @@ func (o *Category) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varCategory := _Category{}
err = json.Unmarshal(data, &varCategory)

View File

@ -118,6 +118,11 @@ func (o *Dog) UnmarshalJSON(data []byte) (err error) {
"className",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -127,11 +132,23 @@ func (o *Dog) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
type DogWithoutEmbeddedStruct struct {
Breed *string `json:"breed,omitempty"`
}

View File

@ -69,6 +69,7 @@ func (o *DuplicatedPropParent) SetDupProp(v string) {
o.DupProp = v
}
func (o DuplicatedPropParent) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {
@ -96,6 +97,11 @@ func (o *DuplicatedPropParent) UnmarshalJSON(data []byte) (err error) {
"dup-prop",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -105,11 +111,23 @@ func (o *DuplicatedPropParent) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varDuplicatedPropParent := _DuplicatedPropParent{}
err = json.Unmarshal(data, &varDuplicatedPropParent)

View File

@ -115,6 +115,7 @@ func (o *EnumTest) SetEnumStringRequired(v string) {
o.EnumStringRequired = v
}
// GetEnumInteger returns the EnumInteger field value if set, zero value otherwise.
func (o *EnumTest) GetEnumInteger() int32 {
if o == nil || IsNil(o.EnumInteger) {
@ -365,6 +366,11 @@ func (o *EnumTest) UnmarshalJSON(data []byte) (err error) {
"enum_string_required",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -374,11 +380,23 @@ func (o *EnumTest) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varEnumTest := _EnumTest{}
err = json.Unmarshal(data, &varEnumTest)

View File

@ -72,6 +72,12 @@ func (o *Foo) SetBar(v string) {
o.Bar = v
}
// GetDefaultbar function assigns the default value &quot;bar&quot; to the Bar field
// of the Foo struct and returns the "bar".
func (o *Foo) GetDefaultBar() interface{} {
return "bar"
}
// GetMap returns the Map field value if set, zero value otherwise.
func (o *Foo) GetMap() map[string][]time.Time {
if o == nil || IsNil(o.Map) {
@ -114,6 +120,9 @@ func (o Foo) MarshalJSON() ([]byte, error) {
func (o Foo) ToMap() (map[string]interface{}, error) {
toSerialize := map[string]interface{}{}
if _, exists := toSerialize["bar"]; !exists {
toSerialize["bar"] = o.GetDefaultBar()
}
toSerialize["bar"] = o.Bar
if !IsNil(o.Map) {
toSerialize["map"] = o.Map
@ -134,6 +143,12 @@ func (o *Foo) UnmarshalJSON(data []byte) (err error) {
"bar",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
"bar": o.GetDefaultBar,
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -143,11 +158,23 @@ func (o *Foo) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varFoo := _Foo{}
err = json.Unmarshal(data, &varFoo)

View File

@ -185,6 +185,7 @@ func (o *FormatTest) SetNumber(v float32) {
o.Number = v
}
// GetFloat returns the Float field value if set, zero value otherwise.
func (o *FormatTest) GetFloat() float32 {
if o == nil || IsNil(o.Float) {
@ -305,6 +306,7 @@ func (o *FormatTest) SetByte(v string) {
o.Byte = v
}
// GetBinary returns the Binary field value if set, zero value otherwise.
func (o *FormatTest) GetBinary() *os.File {
if o == nil || IsNil(o.Binary) {
@ -361,6 +363,7 @@ func (o *FormatTest) SetDate(v string) {
o.Date = v
}
// GetDateTime returns the DateTime field value if set, zero value otherwise.
func (o *FormatTest) GetDateTime() time.Time {
if o == nil || IsNil(o.DateTime) {
@ -449,6 +452,7 @@ func (o *FormatTest) SetPassword(v string) {
o.Password = v
}
// GetPatternWithDigits returns the PatternWithDigits field value if set, zero value otherwise.
func (o *FormatTest) GetPatternWithDigits() string {
if o == nil || IsNil(o.PatternWithDigits) {
@ -579,6 +583,11 @@ func (o *FormatTest) UnmarshalJSON(data []byte) (err error) {
"password",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -588,11 +597,23 @@ func (o *FormatTest) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varFormatTest := _FormatTest{}
err = json.Unmarshal(data, &varFormatTest)

View File

@ -71,6 +71,7 @@ func (o *Name) SetName(v int32) {
o.Name = v
}
// GetSnakeCase returns the SnakeCase field value if set, zero value otherwise.
func (o *Name) GetSnakeCase() int32 {
if o == nil || IsNil(o.SnakeCase) {
@ -203,6 +204,11 @@ func (o *Name) UnmarshalJSON(data []byte) (err error) {
"name",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -212,11 +218,23 @@ func (o *Name) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varName := _Name{}
err = json.Unmarshal(data, &varName)

View File

@ -140,6 +140,7 @@ func (o *Pet) SetName(v string) {
o.Name = v
}
// GetPhotoUrls returns the PhotoUrls field value
func (o *Pet) GetPhotoUrls() []string {
if o == nil {
@ -164,6 +165,7 @@ func (o *Pet) SetPhotoUrls(v []string) {
o.PhotoUrls = v
}
// GetTags returns the Tags field value if set, zero value otherwise.
func (o *Pet) GetTags() []Tag {
if o == nil || IsNil(o.Tags) {
@ -272,6 +274,11 @@ func (o *Pet) UnmarshalJSON(data []byte) (err error) {
"photoUrls",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -281,11 +288,23 @@ func (o *Pet) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varPet := _Pet{}
err = json.Unmarshal(data, &varPet)

View File

@ -134,6 +134,7 @@ func (o *Whale) SetClassName(v string) {
o.ClassName = v
}
func (o Whale) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {
@ -167,6 +168,11 @@ func (o *Whale) UnmarshalJSON(data []byte) (err error) {
"className",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -176,11 +182,23 @@ func (o *Whale) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varWhale := _Whale{}
err = json.Unmarshal(data, &varWhale)

View File

@ -101,6 +101,7 @@ func (o *Zebra) SetClassName(v string) {
o.ClassName = v
}
func (o Zebra) MarshalJSON() ([]byte, error) {
toSerialize,err := o.ToMap()
if err != nil {
@ -131,6 +132,11 @@ func (o *Zebra) UnmarshalJSON(data []byte) (err error) {
"className",
}
// defaultValueFuncMap captures the default values for required properties.
// These values are used when required properties are missing from the payload.
defaultValueFuncMap := map[string]func() interface{} {
}
var defaultValueApplied bool
allProperties := make(map[string]interface{})
err = json.Unmarshal(data, &allProperties)
@ -140,11 +146,23 @@ func (o *Zebra) UnmarshalJSON(data []byte) (err error) {
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
if value, exists := allProperties[requiredProperty]; !exists || value == "" {
if _, ok := defaultValueFuncMap[requiredProperty]; ok {
allProperties[requiredProperty] = defaultValueFuncMap[requiredProperty]()
defaultValueApplied = true
}
}
if value, exists := allProperties[requiredProperty]; !exists || value == ""{
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
if defaultValueApplied {
data, err = json.Marshal(allProperties)
if err != nil{
return err
}
}
varZebra := _Zebra{}
err = json.Unmarshal(data, &varZebra)