Skip to content

Commit

Permalink
openapi3: sort extra fields only once, during deserialization (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
fenollp committed Feb 27, 2023
1 parent cb687bf commit e53fe38
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 20 deletions.
27 changes: 9 additions & 18 deletions openapi3/refs.go
Expand Up @@ -46,6 +46,7 @@ func (x *CallbackRef) UnmarshalJSON(data []byte) error {
for key := range extra {
x.extra = append(x.extra, key)
}
sort.Strings(x.extra)
}
return nil
}
Expand All @@ -56,8 +57,6 @@ func (x *CallbackRef) UnmarshalJSON(data []byte) error {
func (x *CallbackRef) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)
if extra := x.extra; len(extra) != 0 {
sort.Strings(extra)

extras := make([]string, 0, len(extra))
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
if allowed == nil {
Expand Down Expand Up @@ -123,6 +122,7 @@ func (x *ExampleRef) UnmarshalJSON(data []byte) error {
for key := range extra {
x.extra = append(x.extra, key)
}
sort.Strings(x.extra)
}
return nil
}
Expand All @@ -133,8 +133,6 @@ func (x *ExampleRef) UnmarshalJSON(data []byte) error {
func (x *ExampleRef) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)
if extra := x.extra; len(extra) != 0 {
sort.Strings(extra)

extras := make([]string, 0, len(extra))
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
if allowed == nil {
Expand Down Expand Up @@ -200,6 +198,7 @@ func (x *HeaderRef) UnmarshalJSON(data []byte) error {
for key := range extra {
x.extra = append(x.extra, key)
}
sort.Strings(x.extra)
}
return nil
}
Expand All @@ -210,8 +209,6 @@ func (x *HeaderRef) UnmarshalJSON(data []byte) error {
func (x *HeaderRef) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)
if extra := x.extra; len(extra) != 0 {
sort.Strings(extra)

extras := make([]string, 0, len(extra))
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
if allowed == nil {
Expand Down Expand Up @@ -277,6 +274,7 @@ func (x *LinkRef) UnmarshalJSON(data []byte) error {
for key := range extra {
x.extra = append(x.extra, key)
}
sort.Strings(x.extra)
}
return nil
}
Expand All @@ -287,8 +285,6 @@ func (x *LinkRef) UnmarshalJSON(data []byte) error {
func (x *LinkRef) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)
if extra := x.extra; len(extra) != 0 {
sort.Strings(extra)

extras := make([]string, 0, len(extra))
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
if allowed == nil {
Expand Down Expand Up @@ -354,6 +350,7 @@ func (x *ParameterRef) UnmarshalJSON(data []byte) error {
for key := range extra {
x.extra = append(x.extra, key)
}
sort.Strings(x.extra)
}
return nil
}
Expand All @@ -364,8 +361,6 @@ func (x *ParameterRef) UnmarshalJSON(data []byte) error {
func (x *ParameterRef) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)
if extra := x.extra; len(extra) != 0 {
sort.Strings(extra)

extras := make([]string, 0, len(extra))
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
if allowed == nil {
Expand Down Expand Up @@ -431,6 +426,7 @@ func (x *RequestBodyRef) UnmarshalJSON(data []byte) error {
for key := range extra {
x.extra = append(x.extra, key)
}
sort.Strings(x.extra)
}
return nil
}
Expand All @@ -441,8 +437,6 @@ func (x *RequestBodyRef) UnmarshalJSON(data []byte) error {
func (x *RequestBodyRef) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)
if extra := x.extra; len(extra) != 0 {
sort.Strings(extra)

extras := make([]string, 0, len(extra))
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
if allowed == nil {
Expand Down Expand Up @@ -508,6 +502,7 @@ func (x *ResponseRef) UnmarshalJSON(data []byte) error {
for key := range extra {
x.extra = append(x.extra, key)
}
sort.Strings(x.extra)
}
return nil
}
Expand All @@ -518,8 +513,6 @@ func (x *ResponseRef) UnmarshalJSON(data []byte) error {
func (x *ResponseRef) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)
if extra := x.extra; len(extra) != 0 {
sort.Strings(extra)

extras := make([]string, 0, len(extra))
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
if allowed == nil {
Expand Down Expand Up @@ -585,6 +578,7 @@ func (x *SchemaRef) UnmarshalJSON(data []byte) error {
for key := range extra {
x.extra = append(x.extra, key)
}
sort.Strings(x.extra)
}
return nil
}
Expand All @@ -595,8 +589,6 @@ func (x *SchemaRef) UnmarshalJSON(data []byte) error {
func (x *SchemaRef) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)
if extra := x.extra; len(extra) != 0 {
sort.Strings(extra)

extras := make([]string, 0, len(extra))
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
if allowed == nil {
Expand Down Expand Up @@ -662,6 +654,7 @@ func (x *SecuritySchemeRef) UnmarshalJSON(data []byte) error {
for key := range extra {
x.extra = append(x.extra, key)
}
sort.Strings(x.extra)
}
return nil
}
Expand All @@ -672,8 +665,6 @@ func (x *SecuritySchemeRef) UnmarshalJSON(data []byte) error {
func (x *SecuritySchemeRef) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)
if extra := x.extra; len(extra) != 0 {
sort.Strings(extra)

extras := make([]string, 0, len(extra))
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
if allowed == nil {
Expand Down
3 changes: 1 addition & 2 deletions refs.sh
Expand Up @@ -80,6 +80,7 @@ func (x *${type}Ref) UnmarshalJSON(data []byte) error {
for key := range extra {
x.extra = append(x.extra, key)
}
sort.Strings(x.extra)
}
return nil
}
Expand All @@ -90,8 +91,6 @@ func (x *${type}Ref) UnmarshalJSON(data []byte) error {
func (x *${type}Ref) Validate(ctx context.Context, opts ...ValidationOption) error {
ctx = WithValidationOptions(ctx, opts...)
if extra := x.extra; len(extra) != 0 {
sort.Strings(extra)
extras := make([]string, 0, len(extra))
allowed := getValidationOptions(ctx).extraSiblingFieldsAllowed
if allowed == nil {
Expand Down

0 comments on commit e53fe38

Please sign in to comment.