Skip to content

Commit

Permalink
Improve hasJSONBody check with raw schema conditions (#35)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Apr 20, 2022
1 parent 5ede87a commit 957b2a7
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 8 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ require (
github.com/bool64/dev v0.2.10
github.com/stretchr/testify v1.7.1
github.com/swaggest/assertjson v1.6.8
github.com/swaggest/jsonschema-go v0.3.33
github.com/swaggest/jsonschema-go v0.3.34
github.com/swaggest/refl v1.0.2
gopkg.in/yaml.v2 v2.4.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMT
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/swaggest/assertjson v1.6.8 h1:1O/9UI5M+2OJI7BeEWKGj0wTvpRXZt5FkOJ4nRkY4rA=
github.com/swaggest/assertjson v1.6.8/go.mod h1:Euf0upn9Vlaf1/llYHTs+Kx5K3vVbpMbsZhth7zlN7M=
github.com/swaggest/jsonschema-go v0.3.33 h1:G9FxhWHdINUXUshHwHr4X2ptdZWEyBBDu53+VY94oy0=
github.com/swaggest/jsonschema-go v0.3.33/go.mod h1:JAF1nm+uIaMOXktuQepmkiRcgQ5yJk4Ccwx9HVt2cXw=
github.com/swaggest/jsonschema-go v0.3.34 h1:f/ErwRNR+Qx/0QTSSIVqAtS9gnHhzBl7IIHO+XKG1GA=
github.com/swaggest/jsonschema-go v0.3.34/go.mod h1:JAF1nm+uIaMOXktuQepmkiRcgQ5yJk4Ccwx9HVt2cXw=
github.com/swaggest/refl v1.0.2 h1:VmP8smuDS1EzUPn31++TzMi13CAaVJdlWpIxzj0up88=
github.com/swaggest/refl v1.0.2/go.mod h1:DoiPoBJPYHU6Z9fIA6zXQ9uI6VRL6M8BFX5YFT+ym9g=
github.com/yosuke-furukawa/json5 v0.1.2-0.20201207051438-cf7bb3f354ff/go.mod h1:sw49aWDqNdRJ6DYUtIQiaA3xyj2IL9tjeNYmX2ixwcU=
Expand Down
21 changes: 16 additions & 5 deletions openapi3/reflect.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package openapi3

import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"mime/multipart"
Expand Down Expand Up @@ -419,15 +421,24 @@ func (r *Reflector) hasJSONBody(output interface{}) (bool, error) {
return false, err
}

if schema.Type == nil {
return false, nil
// Remove non-constraining fields to prepare for marshaling.
schema.Title = nil
schema.Description = nil
schema.Comment = nil
schema.ExtraProperties = nil
schema.ID = nil
schema.Examples = nil

j, err := json.Marshal(schema)
if err != nil {
return false, err
}

if schema.HasType(jsonschema.Object) && schema.AdditionalProperties == nil && len(schema.Properties) == 0 {
return false, nil
if !bytes.Equal([]byte("{}"), j) && !bytes.Equal([]byte(`{"type":"object"}`), j) {
return true, nil
}

return true, nil
return false, nil
}

// SetupResponse sets up operation response.
Expand Down

0 comments on commit 957b2a7

Please sign in to comment.