Skip to content

Commit

Permalink
Add support for nullable items of maps and slices (#22)
Browse files Browse the repository at this point in the history
  • Loading branch information
vearutop authored Jul 28, 2021
1 parent db23b28 commit 4fb3b7f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 10 deletions.
9 changes: 4 additions & 5 deletions .github/workflows/gorelease.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,19 @@ jobs:
id: gorelease
run: |
test -e ~/go/bin/gorelease || go install golang.org/x/exp/cmd/gorelease@latest
gorelease
OUTPUT=$(gorelease)
OUTPUT=$(gorelease || exit 0)
OUTPUT="${OUTPUT//'%'/'%25'}"
OUTPUT="${OUTPUT//$'\n'/'%0A'}"
OUTPUT="${OUTPUT//$'\r'/'%0D'}"
echo "::set-output name=report::$OUTPUT"
- name: Comment Gorelase Report
- name: Comment Report
uses: marocchino/sticky-pull-request-comment@v2
with:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
header: gorelease
message: |
### Exported API Changes Report
```
<pre>
${{ steps.gorelease.outputs.report }}
```
</pre>
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/swaggest/jsonschema-go
go 1.13

require (
github.com/bool64/dev v0.1.36
github.com/bool64/dev v0.1.37
github.com/stretchr/testify v1.4.0
github.com/swaggest/assertjson v1.6.6
github.com/swaggest/refl v1.0.0
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
github.com/bool64/dev v0.1.17/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.25/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.35/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.36 h1:ZUyNrmb383OeHUSjtncODyq9RSLoDpEWxHvo5yQGhis=
github.com/bool64/dev v0.1.36/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/dev v0.1.37 h1:/c8U4emt4xjMDx8Au+POOYo5LJIw+Mzi4e48j5CmGQo=
github.com/bool64/dev v0.1.37/go.mod h1:cTHiTDNc8EewrQPy3p1obNilpMpdmlUesDkFTF2zRWU=
github.com/bool64/shared v0.1.3 h1:gj7XZPYa1flQsCg3q9AIju+W2A1jaexK0fdFu2XtaG0=
github.com/bool64/shared v0.1.3/go.mod h1:RF1p1Oi29ofgOvinBpetbF5mceOUP3kpMkvLbWOmtm0=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down
4 changes: 2 additions & 2 deletions reflect.go
Original file line number Diff line number Diff line change
Expand Up @@ -457,7 +457,7 @@ func (r *Reflector) kindSwitch(t reflect.Type, v reflect.Value, schema *Schema,
break
}

elemType := refl.DeepIndirect(t.Elem())
elemType := t.Elem()

rc.Path = append(rc.Path, "[]")
itemValue := reflect.Zero(elemType).Interface()
Expand All @@ -475,7 +475,7 @@ func (r *Reflector) kindSwitch(t reflect.Type, v reflect.Value, schema *Schema,
schema.WithItems(*(&Items{}).WithSchemaOrBool(itemsSchema.ToSchemaOrBool()))

case reflect.Map:
elemType := refl.DeepIndirect(t.Elem())
elemType := t.Elem()

rc.Path = append(rc.Path, "{}")
itemValue := reflect.Zero(elemType).Interface()
Expand Down
23 changes: 23 additions & 0 deletions reflect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,26 @@ func TestReflector_Reflect_Ref(t *testing.T) {
"type":"object"
}`), s)
}

func TestReflector_Reflect_MapOfOptionals(t *testing.T) {
type Symbol string

type Optionals struct {
Map map[Symbol]*float64 `json:"map"`
Slice []*float64 `json:"slice"`
}

r := jsonschema.Reflector{}
s, err := r.Reflect(Optionals{})
assert.NoError(t, err)
assertjson.EqualMarshal(t, []byte(`{
"properties":{
"map":{
"additionalProperties":{"type":["null","number"]},
"type":["object","null"]
},
"slice":{"items":{"type":["null","number"]},"type":["array","null"]}
},
"type":"object"
}`), s)
}

0 comments on commit 4fb3b7f

Please sign in to comment.