Skip to content

Commit 3b2ff60

Browse files
committed
fix: fixed key escaping in OrderedItems marshaling
* fixes #216 Signed-off-by: Frédéric BIDON <fredbi@yahoo.com>
1 parent 90efd45 commit 3b2ff60

File tree

2 files changed

+57
-12
lines changed

2 files changed

+57
-12
lines changed

properties.go

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -25,21 +25,26 @@ type OrderSchemaItems []OrderSchemaItem
2525
// of the OrderSchemaItems slice, keeping the original order of the slice.
2626
func (items OrderSchemaItems) MarshalJSON() ([]byte, error) {
2727
buf := bytes.NewBuffer(nil)
28-
buf.WriteString("{")
29-
for i := range items {
30-
if i > 0 {
31-
buf.WriteString(",")
32-
}
33-
buf.WriteString("\"")
34-
buf.WriteString(items[i].Name)
35-
buf.WriteString("\":")
36-
bs, err := json.Marshal(&items[i].Schema)
37-
if err != nil {
28+
buf.WriteByte('{')
29+
30+
if len(items) == 0 {
31+
buf.WriteByte('}')
32+
33+
return buf.Bytes(), nil
34+
}
35+
36+
if err := items.marshalJSONItem(items[0], buf); err != nil {
37+
return nil, err
38+
}
39+
40+
for _, item := range items[1:] {
41+
buf.WriteByte(',')
42+
if err := items.marshalJSONItem(item, buf); err != nil {
3843
return nil, err
3944
}
40-
buf.Write(bs)
4145
}
42-
buf.WriteString("}")
46+
buf.WriteByte('}')
47+
4348
return buf.Bytes(), nil
4449
}
4550

@@ -69,6 +74,22 @@ func (items OrderSchemaItems) Less(i, j int) (ret bool) {
6974
return items[i].Name < items[j].Name
7075
}
7176

77+
func (items OrderSchemaItems) marshalJSONItem(item OrderSchemaItem, output *bytes.Buffer) error {
78+
nameJSON, err := json.Marshal(item.Name)
79+
if err != nil {
80+
return err
81+
}
82+
output.Write(nameJSON)
83+
output.WriteByte(':')
84+
schemaJSON, err := json.Marshal(&item.Schema)
85+
if err != nil {
86+
return err
87+
}
88+
output.Write(schemaJSON)
89+
90+
return nil
91+
}
92+
7293
// SchemaProperties is a map representing the properties of a Schema object.
7394
// It knows how to transform its keys into an ordered slice.
7495
type SchemaProperties map[string]Schema

properties_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ package spec
55

66
import (
77
"testing"
8+
9+
"github.com/go-openapi/testify/v2/require"
810
)
911

1012
func TestPropertySerialization(t *testing.T) {
@@ -45,3 +47,25 @@ func TestPropertySerialization(t *testing.T) {
4547
}
4648

4749
}
50+
51+
func TestOrderedSchemaItem_Issue216(t *testing.T) {
52+
stringSchema := new(Schema).Typed("string", "")
53+
items := OrderSchemaItems{
54+
{
55+
Name: "emails\n", // Key contains newline character
56+
Schema: *stringSchema,
57+
},
58+
{
59+
Name: "regular",
60+
Schema: *stringSchema,
61+
},
62+
}
63+
64+
jazon, err := items.MarshalJSON()
65+
require.NoError(t, err)
66+
67+
require.JSONEqBytes(t,
68+
[]byte(`{"emails\n":{"type":"string"},"regular":{"type":"string"}}`),
69+
jazon,
70+
)
71+
}

0 commit comments

Comments
 (0)