Skip to content

Commit

Permalink
fix: invalid schema for function 'func_name': None is not of type 'ob…
Browse files Browse the repository at this point in the history
…ject' (#429)(#432)
  • Loading branch information
vvatanabe committed Jul 10, 2023
1 parent f028c28 commit df10cf3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 30 deletions.
23 changes: 7 additions & 16 deletions jsonschema/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,23 +36,14 @@ type Definition struct {
Items *Definition `json:"items,omitempty"`
}

func (d *Definition) MarshalJSON() ([]byte, error) {
d.initializeProperties()
return json.Marshal(*d)
}

func (d *Definition) initializeProperties() {
func (d Definition) MarshalJSON() ([]byte, error) {
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
}
type Alias Definition
return json.Marshal(struct {
Alias
}{
Alias: (Alias)(d),
})
}
38 changes: 24 additions & 14 deletions jsonschema/json_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,30 +172,40 @@ func TestDefinition_MarshalJSON(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotBytes, err := json.Marshal(&tt.def)
if err != nil {
t.Errorf("Failed to Marshal JSON: error = %v", err)
return
}

var got map[string]interface{}
err = json.Unmarshal(gotBytes, &got)
if err != nil {
t.Errorf("Failed to Unmarshal JSON: error = %v", err)
return
}

wantBytes := []byte(tt.want)
var want map[string]interface{}
err = json.Unmarshal(wantBytes, &want)
err := json.Unmarshal(wantBytes, &want)
if err != nil {
t.Errorf("Failed to Unmarshal JSON: error = %v", err)
return
}

got := structToMap(t, tt.def)
gotPtr := structToMap(t, &tt.def)

if !reflect.DeepEqual(got, want) {
t.Errorf("MarshalJSON() got = %v, want %v", got, want)
}
if !reflect.DeepEqual(gotPtr, want) {
t.Errorf("MarshalJSON() gotPtr = %v, want %v", gotPtr, want)
}
})
}
}

func structToMap(t *testing.T, v any) map[string]any {
t.Helper()
gotBytes, err := json.Marshal(v)
if err != nil {
t.Errorf("Failed to Marshal JSON: error = %v", err)
return nil
}

var got map[string]interface{}
err = json.Unmarshal(gotBytes, &got)
if err != nil {
t.Errorf("Failed to Unmarshal JSON: error = %v", err)
return nil
}
return got
}

0 comments on commit df10cf3

Please sign in to comment.