Skip to content

[Go] fix validation of property names when a model has required fields and doesn't allow additional properties #17267

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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