Skip to content

Commit

Permalink
[Go] fix validation of property names when a model has required field…
Browse files Browse the repository at this point in the history
…s and doesn't allow additional properties (#17267)

* update template for required field validation when additional properties are not allowed

* regenerate samples

* move bytes import from template to GoClientCodegen

* regenerate samples

* add test for model with required fields and additionalProperties: false
  • Loading branch information
ctreatma authored Dec 22, 2023
1 parent 63f09ee commit da13013
Show file tree
Hide file tree
Showing 77 changed files with 319 additions and 254 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -499,8 +499,14 @@ public ModelsMap postProcessModels(ModelsMap objs) {
addedFmtImport = true;
}

if (!addedFmtImport && model.hasRequired) {
imports.add(createMapping("import", "fmt"));
if (model.hasRequired) {
if (!model.isAdditionalPropertiesTrue) {
imports.add(createMapping("import", "bytes"));
}

if (!addedFmtImport) {
imports.add(createMapping("import", "fmt"));
}
}

// additionalProperties: true and parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,11 @@ func (o {{classname}}) ToMap() (map[string]interface{}, error) {
}
{{#isAdditionalPropertiesTrue}}
func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
func (o *{{{classname}}}) UnmarshalJSON(data []byte) (err error) {
{{/isAdditionalPropertiesTrue}}
{{^isAdditionalPropertiesTrue}}
{{#hasRequired}}
func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
func (o *{{{classname}}}) UnmarshalJSON(data []byte) (err error) {
{{/hasRequired}}
{{/isAdditionalPropertiesTrue}}
{{#hasRequired}}
Expand All @@ -359,7 +359,7 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
allProperties := make(map[string]interface{})
err = json.Unmarshal(bytes, &allProperties)
err = json.Unmarshal(data, &allProperties)
if err != nil {
return err;
Expand Down Expand Up @@ -391,7 +391,7 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
var{{{classname}}}WithoutEmbeddedStruct := {{{classname}}}WithoutEmbeddedStruct{}
err = json.Unmarshal(bytes, &var{{{classname}}}WithoutEmbeddedStruct)
err = json.Unmarshal(data, &var{{{classname}}}WithoutEmbeddedStruct)
if err == nil {
var{{{classname}}} := _{{{classname}}}{}
{{#vars}}
Expand All @@ -404,7 +404,7 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
var{{{classname}}} := _{{{classname}}}{}
err = json.Unmarshal(bytes, &var{{{classname}}})
err = json.Unmarshal(data, &var{{{classname}}})
if err == nil {
o.{{{parent}}} = var{{{classname}}}.{{{parent}}}
} else {
Expand All @@ -413,7 +413,7 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
if err = json.Unmarshal(data, &additionalProperties); err == nil {
{{#vars}}
delete(additionalProperties, "{{{baseName}}}")
{{/vars}}
Expand Down Expand Up @@ -444,7 +444,7 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
{{#isMap}}
var{{{classname}}} := _{{{classname}}}{}
err = json.Unmarshal(bytes, &var{{{classname}}})
err = json.Unmarshal(data, &var{{{classname}}})
if err != nil {
return err
Expand All @@ -454,7 +454,7 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
if err = json.Unmarshal(data, &additionalProperties); err == nil {
{{#vars}}
delete(additionalProperties, "{{{baseName}}}")
{{/vars}}
Expand All @@ -467,7 +467,7 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
{{^parent}}
var{{{classname}}} := _{{{classname}}}{}
err = json.Unmarshal(bytes, &var{{{classname}}})
err = json.Unmarshal(data, &var{{{classname}}})
if err != nil {
return err
Expand All @@ -477,7 +477,7 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
additionalProperties := make(map[string]interface{})
if err = json.Unmarshal(bytes, &additionalProperties); err == nil {
if err = json.Unmarshal(data, &additionalProperties); err == nil {
{{#vars}}
delete(additionalProperties, "{{{baseName}}}")
{{/vars}}
Expand All @@ -495,7 +495,9 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
{{#hasRequired}}
var{{{classname}}} := _{{{classname}}}{}
err = json.Unmarshal(bytes, &var{{{classname}}})
decoder := json.NewDecoder(bytes.NewReader(data))
decoder.DisallowUnknownFields()
err = decoder.Decode(&var{{{classname}}})
if err != nil {
return err
Expand All @@ -509,8 +511,8 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
{{/hasRequired}}
{{/isAdditionalPropertiesTrue}}
{{#isArray}}
func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
return json.Unmarshal(bytes, &o.Items)
func (o *{{{classname}}}) UnmarshalJSON(data []byte) (err error) {
return json.Unmarshal(data, &o.Items)
}
{{/isArray}}
Expand Down
9 changes: 6 additions & 3 deletions samples/client/echo_api/go/model_pet.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions samples/client/petstore/go/go-petstore/model_animal.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 6 additions & 3 deletions samples/client/petstore/go/go-petstore/model_big_cat.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit da13013

Please sign in to comment.