Skip to content

Commit e53fe38

Browse files
authored
openapi3: sort extra fields only once, during deserialization (#773)
1 parent cb687bf commit e53fe38

File tree

2 files changed

+10
-20
lines changed

2 files changed

+10
-20
lines changed

openapi3/refs.go

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ func (x *CallbackRef) UnmarshalJSON(data []byte) error {
4646
for key := range extra {
4747
x.extra = append(x.extra, key)
4848
}
49+
sort.Strings(x.extra)
4950
}
5051
return nil
5152
}
@@ -56,8 +57,6 @@ func (x *CallbackRef) UnmarshalJSON(data []byte) error {
5657
func (x *CallbackRef) Validate(ctx context.Context, opts ...ValidationOption) error {
5758
ctx = WithValidationOptions(ctx, opts...)
5859
if extra := x.extra; len(extra) != 0 {
59-
sort.Strings(extra)
60-
6160
extras := make([]string, 0, len(extra))
6261
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
6362
if allowed == nil {
@@ -123,6 +122,7 @@ func (x *ExampleRef) UnmarshalJSON(data []byte) error {
123122
for key := range extra {
124123
x.extra = append(x.extra, key)
125124
}
125+
sort.Strings(x.extra)
126126
}
127127
return nil
128128
}
@@ -133,8 +133,6 @@ func (x *ExampleRef) UnmarshalJSON(data []byte) error {
133133
func (x *ExampleRef) Validate(ctx context.Context, opts ...ValidationOption) error {
134134
ctx = WithValidationOptions(ctx, opts...)
135135
if extra := x.extra; len(extra) != 0 {
136-
sort.Strings(extra)
137-
138136
extras := make([]string, 0, len(extra))
139137
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
140138
if allowed == nil {
@@ -200,6 +198,7 @@ func (x *HeaderRef) UnmarshalJSON(data []byte) error {
200198
for key := range extra {
201199
x.extra = append(x.extra, key)
202200
}
201+
sort.Strings(x.extra)
203202
}
204203
return nil
205204
}
@@ -210,8 +209,6 @@ func (x *HeaderRef) UnmarshalJSON(data []byte) error {
210209
func (x *HeaderRef) Validate(ctx context.Context, opts ...ValidationOption) error {
211210
ctx = WithValidationOptions(ctx, opts...)
212211
if extra := x.extra; len(extra) != 0 {
213-
sort.Strings(extra)
214-
215212
extras := make([]string, 0, len(extra))
216213
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
217214
if allowed == nil {
@@ -277,6 +274,7 @@ func (x *LinkRef) UnmarshalJSON(data []byte) error {
277274
for key := range extra {
278275
x.extra = append(x.extra, key)
279276
}
277+
sort.Strings(x.extra)
280278
}
281279
return nil
282280
}
@@ -287,8 +285,6 @@ func (x *LinkRef) UnmarshalJSON(data []byte) error {
287285
func (x *LinkRef) Validate(ctx context.Context, opts ...ValidationOption) error {
288286
ctx = WithValidationOptions(ctx, opts...)
289287
if extra := x.extra; len(extra) != 0 {
290-
sort.Strings(extra)
291-
292288
extras := make([]string, 0, len(extra))
293289
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
294290
if allowed == nil {
@@ -354,6 +350,7 @@ func (x *ParameterRef) UnmarshalJSON(data []byte) error {
354350
for key := range extra {
355351
x.extra = append(x.extra, key)
356352
}
353+
sort.Strings(x.extra)
357354
}
358355
return nil
359356
}
@@ -364,8 +361,6 @@ func (x *ParameterRef) UnmarshalJSON(data []byte) error {
364361
func (x *ParameterRef) Validate(ctx context.Context, opts ...ValidationOption) error {
365362
ctx = WithValidationOptions(ctx, opts...)
366363
if extra := x.extra; len(extra) != 0 {
367-
sort.Strings(extra)
368-
369364
extras := make([]string, 0, len(extra))
370365
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
371366
if allowed == nil {
@@ -431,6 +426,7 @@ func (x *RequestBodyRef) UnmarshalJSON(data []byte) error {
431426
for key := range extra {
432427
x.extra = append(x.extra, key)
433428
}
429+
sort.Strings(x.extra)
434430
}
435431
return nil
436432
}
@@ -441,8 +437,6 @@ func (x *RequestBodyRef) UnmarshalJSON(data []byte) error {
441437
func (x *RequestBodyRef) Validate(ctx context.Context, opts ...ValidationOption) error {
442438
ctx = WithValidationOptions(ctx, opts...)
443439
if extra := x.extra; len(extra) != 0 {
444-
sort.Strings(extra)
445-
446440
extras := make([]string, 0, len(extra))
447441
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
448442
if allowed == nil {
@@ -508,6 +502,7 @@ func (x *ResponseRef) UnmarshalJSON(data []byte) error {
508502
for key := range extra {
509503
x.extra = append(x.extra, key)
510504
}
505+
sort.Strings(x.extra)
511506
}
512507
return nil
513508
}
@@ -518,8 +513,6 @@ func (x *ResponseRef) UnmarshalJSON(data []byte) error {
518513
func (x *ResponseRef) Validate(ctx context.Context, opts ...ValidationOption) error {
519514
ctx = WithValidationOptions(ctx, opts...)
520515
if extra := x.extra; len(extra) != 0 {
521-
sort.Strings(extra)
522-
523516
extras := make([]string, 0, len(extra))
524517
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
525518
if allowed == nil {
@@ -585,6 +578,7 @@ func (x *SchemaRef) UnmarshalJSON(data []byte) error {
585578
for key := range extra {
586579
x.extra = append(x.extra, key)
587580
}
581+
sort.Strings(x.extra)
588582
}
589583
return nil
590584
}
@@ -595,8 +589,6 @@ func (x *SchemaRef) UnmarshalJSON(data []byte) error {
595589
func (x *SchemaRef) Validate(ctx context.Context, opts ...ValidationOption) error {
596590
ctx = WithValidationOptions(ctx, opts...)
597591
if extra := x.extra; len(extra) != 0 {
598-
sort.Strings(extra)
599-
600592
extras := make([]string, 0, len(extra))
601593
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
602594
if allowed == nil {
@@ -662,6 +654,7 @@ func (x *SecuritySchemeRef) UnmarshalJSON(data []byte) error {
662654
for key := range extra {
663655
x.extra = append(x.extra, key)
664656
}
657+
sort.Strings(x.extra)
665658
}
666659
return nil
667660
}
@@ -672,8 +665,6 @@ func (x *SecuritySchemeRef) UnmarshalJSON(data []byte) error {
672665
func (x *SecuritySchemeRef) Validate(ctx context.Context, opts ...ValidationOption) error {
673666
ctx = WithValidationOptions(ctx, opts...)
674667
if extra := x.extra; len(extra) != 0 {
675-
sort.Strings(extra)
676-
677668
extras := make([]string, 0, len(extra))
678669
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
679670
if allowed == nil {

refs.sh

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ func (x *${type}Ref) UnmarshalJSON(data []byte) error {
8080
for key := range extra {
8181
x.extra = append(x.extra, key)
8282
}
83+
sort.Strings(x.extra)
8384
}
8485
return nil
8586
}
@@ -90,8 +91,6 @@ func (x *${type}Ref) UnmarshalJSON(data []byte) error {
9091
func (x *${type}Ref) Validate(ctx context.Context, opts ...ValidationOption) error {
9192
ctx = WithValidationOptions(ctx, opts...)
9293
if extra := x.extra; len(extra) != 0 {
93-
sort.Strings(extra)
94-
9594
extras := make([]string, 0, len(extra))
9695
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
9796
if allowed == nil {

0 commit comments

Comments
 (0)