fix: function call error due to nil properties (429) (#431)

* fix: fix function call error due to nil properties (429)

* refactor: refactoring initializeProperties func in jsonschema pkg (429)
This commit is contained in:
渡邉祐一 / Yuichi Watanabe
2023-07-10 02:07:01 +09:00
committed by GitHub
parent 181fc2ade9
commit f028c289d2
2 changed files with 225 additions and 1 deletions

View File

@@ -4,6 +4,8 @@
// and/or pass in the schema in []byte format.
package jsonschema
import "encoding/json"
type DataType string
const (
@@ -17,7 +19,7 @@ const (
)
// Definition is a struct for describing a JSON Schema.
// It is fairly limited and you may have better luck using a third-party library.
// It is fairly limited, and you may have better luck using a third-party library.
type Definition struct {
// Type specifies the data type of the schema.
Type DataType `json:"type,omitempty"`
@@ -33,3 +35,24 @@ type Definition struct {
// Items specifies which data type an array contains, if the schema type is Array.
Items *Definition `json:"items,omitempty"`
}
func (d *Definition) MarshalJSON() ([]byte, error) {
d.initializeProperties()
return json.Marshal(*d)
}
func (d *Definition) initializeProperties() {
if d.Properties == nil {
d.Properties = make(map[string]Definition)
return
}
for k, v := range d.Properties {
if v.Properties == nil {
v.Properties = make(map[string]Definition)
} else {
v.initializeProperties()
}
d.Properties[k] = v
}
}