From cb44d5b85db2a17d3e7094bd1ef100958d61bdb2 Mon Sep 17 00:00:00 2001 From: Vikrant Balyan Date: Tue, 30 Jul 2024 16:02:54 +0530 Subject: [PATCH] [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 --- bin/configs/go-petstore.yaml | 1 + docs/generators/go.md | 1 + .../codegen/CodegenConstants.java | 2 + .../codegen/languages/AbstractGoCodegen.java | 29 ++++++---- .../codegen/languages/GoClientCodegen.java | 7 +++ .../main/resources/go/model_simple.mustache | 54 +++++++++++++++++++ .../codegen/go/GoClientOptionsTest.java | 1 + .../options/GoClientOptionsProvider.java | 2 + .../echo_api/go-external-refs/model_pet.go | 2 + samples/client/echo_api/go/model_pet.go | 2 + .../model_additional_data.go | 4 ++ .../model_base_item.go | 2 + .../model_final_item.go | 4 ++ .../model_nested_object1.go | 1 + .../model_nested_object2.go | 1 + .../petstore/go/go-petstore/model_animal.go | 1 + .../petstore/go/go-petstore/model_category.go | 1 + .../go/go-petstore/model_enum_test_.go | 1 + .../go/go-petstore/model_format_test_.go | 4 ++ .../petstore/go/go-petstore/model_name.go | 1 + .../petstore/go/go-petstore/model_pet.go | 2 + .../go-petstore/model_type_holder_default.go | 5 ++ .../go-petstore/model_type_holder_example.go | 6 +++ .../model_pet.go | 2 + .../petstore/go/go-petstore/model_animal.go | 20 ++++++- .../go/go-petstore/model_apple_req.go | 20 ++++++- .../go/go-petstore/model_banana_req.go | 20 ++++++- .../petstore/go/go-petstore/model_cat.go | 19 ++++++- .../petstore/go/go-petstore/model_category.go | 29 +++++++++- .../petstore/go/go-petstore/model_dog.go | 19 ++++++- .../model_duplicated_prop_parent.go | 20 ++++++- .../go/go-petstore/model_enum_test_.go | 20 ++++++- .../petstore/go/go-petstore/model_foo.go | 29 +++++++++- .../go/go-petstore/model_format_test_.go | 23 +++++++- .../petstore/go/go-petstore/model_name.go | 20 ++++++- .../petstore/go/go-petstore/model_pet.go | 21 +++++++- .../petstore/go/go-petstore/model_whale.go | 20 ++++++- .../petstore/go/go-petstore/model_zebra.go | 20 ++++++- 38 files changed, 413 insertions(+), 23 deletions(-) diff --git a/bin/configs/go-petstore.yaml b/bin/configs/go-petstore.yaml index 5647bf469e4b..7ab12f332a3e 100644 --- a/bin/configs/go-petstore.yaml +++ b/bin/configs/go-petstore.yaml @@ -15,6 +15,7 @@ additionalProperties: packageName: petstore disallowAdditionalPropertiesIfNotPresent: false generateInterfaces: true + useDefaultValuesForRequiredVars: "true" enumNameMappings: delivered: SHIPPED diff --git a/docs/generators/go.md b/docs/generators/go.md index e81de548e957..328740c461c6 100644 --- a/docs/generators/go.md +++ b/docs/generators/go.md @@ -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| diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java index 33f8e754f676..4ac1a10e75db 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/CodegenConstants.java @@ -449,4 +449,6 @@ public static enum ENUM_PROPERTY_NAMING_TYPE {camelCase, PascalCase, snake_case, 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"; } diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java index d5a68e36bdb7..88da2b2a69fe 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/AbstractGoCodegen.java @@ -40,15 +40,26 @@ public abstract class AbstractGoCodegen extends DefaultCodegen implements Codege private final Logger LOGGER = LoggerFactory.getLogger(AbstractGoCodegen.class); private static final String NUMERIC_ENUM_PREFIX = "_"; - @Setter protected boolean withGoCodegenComment = false; - @Setter protected boolean withAWSV4Signature = false; - @Setter protected boolean withXml = false; - @Setter protected boolean enumClassPrefix = false; - @Setter protected boolean structPrefix = false; - @Setter protected boolean generateInterfaces = false; - @Setter protected boolean withGoMod = false; - @Setter protected boolean generateMarshalJSON = true; - @Setter protected boolean generateUnmarshalJSON = true; + @Setter + protected boolean withGoCodegenComment = false; + @Setter + protected boolean withAWSV4Signature = false; + @Setter + protected boolean withXml = false; + @Setter + protected boolean enumClassPrefix = false; + @Setter + protected boolean structPrefix = false; + @Setter + protected boolean generateInterfaces = false; + @Setter + protected boolean withGoMod = false; + @Setter + protected boolean generateMarshalJSON = true; + @Setter + protected boolean generateUnmarshalJSON = true; + @Setter + protected boolean useDefaultValuesForRequiredVars = false; @Setter protected String packageName = "openapi"; protected Set numberTypes; 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 b4d75b60e230..25588dfa7eac 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 @@ -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 GoClientCodegen() { 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 void processOpts() { 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 securitySchemeMap = openAPI != null ? (openAPI.getComponents() != null ? openAPI.getComponents().getSecuritySchemes() : null) : null; 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 9299b9e5b942..b8287e592bb5 100644 --- a/modules/openapi-generator/src/main/resources/go/model_simple.mustache +++ b/modules/openapi-generator/src/main/resources/go/model_simple.mustache @@ -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}} diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java index 603515e7e9cd..9f99b6c4afe8 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/go/GoClientOptionsTest.java @@ -54,5 +54,6 @@ protected void verifyOptions() { 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); } } diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java index 372e293f636d..54e2348b3f4d 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/options/GoClientOptionsProvider.java @@ -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 Map createOptions() { .put(CodegenConstants.GENERATE_UNMARSHAL_JSON, "true") .put("generateInterfaces", "true") .put("structPrefix", "true") + .put(CodegenConstants.USE_DEFAULT_VALUES_FOR_REQUIRED_VARS, "true") .build(); } diff --git a/samples/client/echo_api/go-external-refs/model_pet.go b/samples/client/echo_api/go-external-refs/model_pet.go index 13f24706dfd5..50af7c6d33e9 100644 --- a/samples/client/echo_api/go-external-refs/model_pet.go +++ b/samples/client/echo_api/go-external-refs/model_pet.go @@ -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) { diff --git a/samples/client/echo_api/go/model_pet.go b/samples/client/echo_api/go/model_pet.go index 13f24706dfd5..50af7c6d33e9 100644 --- a/samples/client/echo_api/go/model_pet.go +++ b/samples/client/echo_api/go/model_pet.go @@ -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) { diff --git a/samples/client/others/go/allof_multiple_ref_and_discriminator/model_additional_data.go b/samples/client/others/go/allof_multiple_ref_and_discriminator/model_additional_data.go index a7186d87f51f..65f35a2f3dc9 100644 --- a/samples/client/others/go/allof_multiple_ref_and_discriminator/model_additional_data.go +++ b/samples/client/others/go/allof_multiple_ref_and_discriminator/model_additional_data.go @@ -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 { diff --git a/samples/client/others/go/allof_multiple_ref_and_discriminator/model_base_item.go b/samples/client/others/go/allof_multiple_ref_and_discriminator/model_base_item.go index 96d4179808c9..9c40caf779de 100644 --- a/samples/client/others/go/allof_multiple_ref_and_discriminator/model_base_item.go +++ b/samples/client/others/go/allof_multiple_ref_and_discriminator/model_base_item.go @@ -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 { diff --git a/samples/client/others/go/allof_multiple_ref_and_discriminator/model_final_item.go b/samples/client/others/go/allof_multiple_ref_and_discriminator/model_final_item.go index d3845f84998f..21c16e930d78 100644 --- a/samples/client/others/go/allof_multiple_ref_and_discriminator/model_final_item.go +++ b/samples/client/others/go/allof_multiple_ref_and_discriminator/model_final_item.go @@ -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 { diff --git a/samples/client/others/go/oneof-anyof-required/model_nested_object1.go b/samples/client/others/go/oneof-anyof-required/model_nested_object1.go index f403748a8968..824b2634b3a9 100644 --- a/samples/client/others/go/oneof-anyof-required/model_nested_object1.go +++ b/samples/client/others/go/oneof-anyof-required/model_nested_object1.go @@ -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 { diff --git a/samples/client/others/go/oneof-anyof-required/model_nested_object2.go b/samples/client/others/go/oneof-anyof-required/model_nested_object2.go index c3f9468bc15c..697e295e8a59 100644 --- a/samples/client/others/go/oneof-anyof-required/model_nested_object2.go +++ b/samples/client/others/go/oneof-anyof-required/model_nested_object2.go @@ -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 { diff --git a/samples/client/petstore/go/go-petstore/model_animal.go b/samples/client/petstore/go/go-petstore/model_animal.go index e23baa88fcc6..028564cf4da9 100644 --- a/samples/client/petstore/go/go-petstore/model_animal.go +++ b/samples/client/petstore/go/go-petstore/model_animal.go @@ -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) { diff --git a/samples/client/petstore/go/go-petstore/model_category.go b/samples/client/petstore/go/go-petstore/model_category.go index d19362640391..5babac8835c2 100644 --- a/samples/client/petstore/go/go-petstore/model_category.go +++ b/samples/client/petstore/go/go-petstore/model_category.go @@ -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 { 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 cf858376f48b..f660e91a0ee8 100644 --- a/samples/client/petstore/go/go-petstore/model_enum_test_.go +++ b/samples/client/petstore/go/go-petstore/model_enum_test_.go @@ -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) { 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 d2cb7e88a331..4c921cb01ae0 100644 --- a/samples/client/petstore/go/go-petstore/model_format_test_.go +++ b/samples/client/petstore/go/go-petstore/model_format_test_.go @@ -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) { diff --git a/samples/client/petstore/go/go-petstore/model_name.go b/samples/client/petstore/go/go-petstore/model_name.go index 7e009e1cf0b5..e6c1650b489d 100644 --- a/samples/client/petstore/go/go-petstore/model_name.go +++ b/samples/client/petstore/go/go-petstore/model_name.go @@ -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) { diff --git a/samples/client/petstore/go/go-petstore/model_pet.go b/samples/client/petstore/go/go-petstore/model_pet.go index de782511daa3..3fe806e7bafd 100644 --- a/samples/client/petstore/go/go-petstore/model_pet.go +++ b/samples/client/petstore/go/go-petstore/model_pet.go @@ -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) { 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 8a67e9f7a2df..ded93fd71f96 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 @@ -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 { 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 a08223070828..532049a81f3b 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 @@ -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 { diff --git a/samples/openapi3/client/petstore/go-petstore-generateMarshalJSON-false/model_pet.go b/samples/openapi3/client/petstore/go-petstore-generateMarshalJSON-false/model_pet.go index 77983744402d..b4271c201c97 100644 --- a/samples/openapi3/client/petstore/go-petstore-generateMarshalJSON-false/model_pet.go +++ b/samples/openapi3/client/petstore/go-petstore-generateMarshalJSON-false/model_pet.go @@ -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) { 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 1e5c17f0a1ef..c20fc86d15bb 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_animal.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_animal.go @@ -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) 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 4d398c185ccd..f8bb88618356 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 @@ -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) 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 173e41024d92..1056fe6ff9c8 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 @@ -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) 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 16b541f887b7..9d818473c8e8 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_cat.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_cat.go @@ -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"` } 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 ff0f7231279c..174ecab638b6 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_category.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_category.go @@ -103,6 +103,12 @@ func (o *Category) SetName(v string) { o.Name = v } +// GetDefaultname function assigns the default value "default-name" 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) 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 258364c75f59..ee38c34caeca 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_dog.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_dog.go @@ -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"` } 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 8e0c89f06f27..d53bea34bf32 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 @@ -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) 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 8a6d6b6c7125..ae19712f35a7 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 @@ -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) diff --git a/samples/openapi3/client/petstore/go/go-petstore/model_foo.go b/samples/openapi3/client/petstore/go/go-petstore/model_foo.go index 2f46e1283d92..79d4c43e8d39 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_foo.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_foo.go @@ -72,6 +72,12 @@ func (o *Foo) SetBar(v string) { o.Bar = v } +// GetDefaultbar function assigns the default value "bar" 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) 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 6aef735d91ee..34ef6aa6beb0 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 @@ -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) 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 14674c8e01bf..09622716e158 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_name.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_name.go @@ -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) 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 2fcb77442379..364d9bc53dd4 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_pet.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_pet.go @@ -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) 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 7d418d153bfe..5803c94fc60b 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_whale.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_whale.go @@ -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) 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 acb4acc5b1f8..9b04e675f17b 100644 --- a/samples/openapi3/client/petstore/go/go-petstore/model_zebra.go +++ b/samples/openapi3/client/petstore/go/go-petstore/model_zebra.go @@ -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)