Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions jsonschema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ type Schema struct {
MaxProperties *int `json:"maxProperties,omitempty"`
Required []string `json:"required,omitempty"`
DependentRequired map[string][]string `json:"dependentRequired,omitempty"`
Properties map[string]*Schema `json:"properties"`
Properties map[string]*Schema `json:"properties,omitempty"`
PatternProperties map[string]*Schema `json:"patternProperties,omitempty"`
AdditionalProperties *Schema `json:"additionalProperties,omitempty"`
PropertyNames *Schema `json:"propertyNames,omitempty"`
Expand Down Expand Up @@ -285,7 +285,8 @@ func (s Schema) MarshalJSON() ([]byte, error) {
Items: items,
schemaWithoutMethods: (*schemaWithoutMethods)(&s),
}
if len(s.Properties) > 0 {
// Marshal properties, even if the empty map (but not nil).
if s.Properties != nil {
ms.Properties = orderedProperties{
props: s.Properties,
order: s.PropertyOrder,
Expand Down
13 changes: 13 additions & 0 deletions jsonschema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,19 @@ func TestMarshalJSONConsistency(t *testing.T) {
t.Errorf("Interface value marshaling mismatch\ngot: %s\nwant: %s", got, expectedJSON)
}
})

t.Run("EmptyPropertiesMap", func(t *testing.T) {
// Test that an empty map in Properties marshals as "{}".
s := &Schema{Type: "object", Properties: map[string]*Schema{}}
got, err := json.Marshal(s)
if err != nil {
t.Fatalf("Failed to marshal interface value: %v", err)
}
want := `{"type":"object","properties":{}}`
if string(got) != want {
t.Errorf("\ngot %s\nwant %s", got, want)
}
})
}

func TestGoRoundTrip(t *testing.T) {
Expand Down