Skip to content

Commit

Permalink
[Go] validate required fields when unmarshalling JSON (#16863)
Browse files Browse the repository at this point in the history
* validate required properties when unmarshalling JSON

* build project & update samples

* Add test for required field validation
  • Loading branch information
ctreatma authored Nov 4, 2023
1 parent 5d03c4a commit db9fd9a
Show file tree
Hide file tree
Showing 28 changed files with 838 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -484,14 +484,22 @@ public ModelsMap postProcessModels(ModelsMap objs) {
}

// additional import for different cases
boolean addedFmtImport = false;

// oneOf
if (model.oneOf != null && !model.oneOf.isEmpty()) {
imports.add(createMapping("import", "fmt"));
addedFmtImport = true;
}

// anyOf
if (model.anyOf != null && !model.anyOf.isEmpty()) {
imports.add(createMapping("import", "fmt"));
addedFmtImport = true;
}

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

// additionalProperties: true and parent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ type {{classname}} struct {
{{#isAdditionalPropertiesTrue}}
type _{{{classname}}} {{{classname}}}

{{/isAdditionalPropertiesTrue}}
{{^isAdditionalPropertiesTrue}}
{{#hasRequired}}
type _{{{classname}}} {{{classname}}}

{{/hasRequired}}
{{/isAdditionalPropertiesTrue}}
// New{{classname}} instantiates a new {{classname}} object
// This constructor will assign default values to properties that have it defined,
Expand Down Expand Up @@ -335,6 +341,38 @@ func (o {{classname}}) ToMap() (map[string]interface{}, error) {
{{#isAdditionalPropertiesTrue}}
func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
{{/isAdditionalPropertiesTrue}}
{{^isAdditionalPropertiesTrue}}
{{#hasRequired}}
func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
{{/hasRequired}}
{{/isAdditionalPropertiesTrue}}
{{#hasRequired}}
// This validates that all required properties are included in the JSON object
// by unmarshalling the object into a generic map with string keys and checking
// that every required field exists as a key in the generic map.
requiredProperties := []string{
{{#requiredVars}}
"{{baseName}}",
{{/requiredVars}}
}
allProperties := make(map[string]interface{})
err = json.Unmarshal(bytes, &allProperties)
if err != nil {
return err;
}
for _, requiredProperty := range(requiredProperties) {
if _, exists := allProperties[requiredProperty]; !exists {
return fmt.Errorf("no value given for required property %v", requiredProperty)
}
}
{{/hasRequired}}
{{#isAdditionalPropertiesTrue}}
{{#parent}}
{{^isMap}}
type {{classname}}WithoutEmbeddedStruct struct {
Expand Down Expand Up @@ -448,8 +486,27 @@ func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
return err
{{/parent}}
{{/isAdditionalPropertiesTrue}}
{{#isAdditionalPropertiesTrue}}
}
{{/isAdditionalPropertiesTrue}}
{{^isAdditionalPropertiesTrue}}
{{#hasRequired}}
var{{{classname}}} := _{{{classname}}}{}
err = json.Unmarshal(bytes, &var{{{classname}}})
if err != nil {
return err
}
*o = {{{classname}}}(var{{{classname}}})
return err
}
{{/hasRequired}}
{{/isAdditionalPropertiesTrue}}
{{#isArray}}
func (o *{{{classname}}}) UnmarshalJSON(bytes []byte) (err error) {
Expand Down
39 changes: 39 additions & 0 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.

38 changes: 38 additions & 0 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.

38 changes: 38 additions & 0 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.

38 changes: 38 additions & 0 deletions samples/client/petstore/go/go-petstore/model_cat.go

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

38 changes: 38 additions & 0 deletions samples/client/petstore/go/go-petstore/model_category.go

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

Loading

0 comments on commit db9fd9a

Please sign in to comment.