Skip to content

Commit e819a65

Browse files
committed
introduce AllowExtraSiblingFields
Signed-off-by: Pierre Fenoll <[email protected]>
1 parent c988603 commit e819a65

25 files changed

+231
-45
lines changed

openapi3/components.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ func (components *Components) Validate(ctx context.Context, opts ...ValidationOp
224224
}
225225
}
226226

227-
return validateExtensions(components.Extensions)
227+
return validateExtensions(ctx, components.Extensions)
228228
}
229229

230230
const identifierPattern = `^[a-zA-Z0-9._-]+$`

openapi3/discriminator.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (discriminator *Discriminator) UnmarshalJSON(data []byte) error {
4343

4444
// Validate returns an error if Discriminator does not comply with the OpenAPI spec.
4545
func (discriminator *Discriminator) Validate(ctx context.Context, opts ...ValidationOption) error {
46-
// ctx = WithValidationOptions(ctx, opts...)
46+
ctx = WithValidationOptions(ctx, opts...)
4747

48-
return validateExtensions(discriminator.Extensions)
48+
return validateExtensions(ctx, discriminator.Extensions)
4949
}

openapi3/encoding.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,5 +132,5 @@ func (encoding *Encoding) Validate(ctx context.Context, opts ...ValidationOption
132132
return fmt.Errorf("serialization method with style=%q and explode=%v is not supported by media type", sm.Style, sm.Explode)
133133
}
134134

135-
return validateExtensions(encoding.Extensions)
135+
return validateExtensions(ctx, encoding.Extensions)
136136
}

openapi3/example.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func (example *Example) UnmarshalJSON(data []byte) error {
8080

8181
// Validate returns an error if Example does not comply with the OpenAPI spec.
8282
func (example *Example) Validate(ctx context.Context, opts ...ValidationOption) error {
83-
// ctx = WithValidationOptions(ctx, opts...)
83+
ctx = WithValidationOptions(ctx, opts...)
8484

8585
if example.Value != nil && example.ExternalValue != "" {
8686
return errors.New("value and externalValue are mutually exclusive")
@@ -89,5 +89,5 @@ func (example *Example) Validate(ctx context.Context, opts ...ValidationOption)
8989
return errors.New("no value or externalValue field")
9090
}
9191

92-
return validateExtensions(example.Extensions)
92+
return validateExtensions(ctx, example.Extensions)
9393
}

openapi3/extension.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,24 @@
11
package openapi3
22

33
import (
4+
"context"
45
"fmt"
56
"sort"
67
"strings"
78
)
89

9-
func validateExtensions(extensions map[string]interface{}) error { // FIXME: newtype + Validate(...)
10+
func validateExtensions(ctx context.Context, extensions map[string]interface{}) error { // FIXME: newtype + Validate(...)
1011
var unknowns []string
1112
for k := range extensions {
1213
if !strings.HasPrefix(k, "x-") {
1314
unknowns = append(unknowns, k)
1415
}
1516
}
17+
1618
if len(unknowns) != 0 {
1719
sort.Strings(unknowns)
1820
return fmt.Errorf("extra sibling fields: %+v", unknowns)
1921
}
22+
2023
return nil
2124
}

openapi3/external_docs.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (e *ExternalDocs) UnmarshalJSON(data []byte) error {
4848

4949
// Validate returns an error if ExternalDocs does not comply with the OpenAPI spec.
5050
func (e *ExternalDocs) Validate(ctx context.Context, opts ...ValidationOption) error {
51-
// ctx = WithValidationOptions(ctx, opts...)
51+
ctx = WithValidationOptions(ctx, opts...)
5252

5353
if e.URL == "" {
5454
return errors.New("url is required")
@@ -57,5 +57,5 @@ func (e *ExternalDocs) Validate(ctx context.Context, opts ...ValidationOption) e
5757
return fmt.Errorf("url is incorrect: %w", err)
5858
}
5959

60-
return validateExtensions(e.Extensions)
60+
return validateExtensions(ctx, e.Extensions)
6161
}

openapi3/info.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func (info *Info) Validate(ctx context.Context, opts ...ValidationOption) error
8484
return errors.New("value of title must be a non-empty string")
8585
}
8686

87-
return validateExtensions(info.Extensions)
87+
return validateExtensions(ctx, info.Extensions)
8888
}
8989

9090
// Contact is specified by OpenAPI/Swagger standard version 3.
@@ -132,9 +132,9 @@ func (contact *Contact) UnmarshalJSON(data []byte) error {
132132

133133
// Validate returns an error if Contact does not comply with the OpenAPI spec.
134134
func (contact *Contact) Validate(ctx context.Context, opts ...ValidationOption) error {
135-
// ctx = WithValidationOptions(ctx, opts...)
135+
ctx = WithValidationOptions(ctx, opts...)
136136

137-
return validateExtensions(contact.Extensions)
137+
return validateExtensions(ctx, contact.Extensions)
138138
}
139139

140140
// License is specified by OpenAPI/Swagger standard version 3.
@@ -175,11 +175,11 @@ func (license *License) UnmarshalJSON(data []byte) error {
175175

176176
// Validate returns an error if License does not comply with the OpenAPI spec.
177177
func (license *License) Validate(ctx context.Context, opts ...ValidationOption) error {
178-
// ctx = WithValidationOptions(ctx, opts...)
178+
ctx = WithValidationOptions(ctx, opts...)
179179

180180
if license.Name == "" {
181181
return errors.New("value of license name must be a non-empty string")
182182
}
183183

184-
return validateExtensions(license.Extensions)
184+
return validateExtensions(ctx, license.Extensions)
185185
}

openapi3/issue513_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,3 +131,43 @@ components:
131131
err = doc.Validate(sl.Context)
132132
require.ErrorContains(t, err, `extra sibling fields: [description]`)
133133
}
134+
135+
func TestIssue513KOMixesRefAlongWithOtherFieldsAllowed(t *testing.T) {
136+
spec := `
137+
openapi: "3.0.3"
138+
info:
139+
title: 'My app'
140+
version: 1.0.0
141+
description: 'An API'
142+
143+
paths:
144+
/v1/operation:
145+
delete:
146+
summary: Delete something
147+
responses:
148+
200:
149+
description: A sibling field that the spec says is ignored
150+
$ref: '#/components/responses/SomeResponseBody'
151+
components:
152+
responses:
153+
SomeResponseBody:
154+
description: Success
155+
content:
156+
application/json:
157+
schema:
158+
$ref: '#/components/schemas/Error'
159+
schemas:
160+
Error:
161+
type: object
162+
description: An error response body.
163+
properties:
164+
message:
165+
description: A detailed message describing the error.
166+
type: string
167+
`[1:]
168+
sl := NewLoader()
169+
doc, err := sl.LoadFromData([]byte(spec))
170+
require.NoError(t, err)
171+
err = doc.Validate(sl.Context, AllowExtraSiblingFields("description"))
172+
require.NoError(t, err)
173+
}

openapi3/link.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ func (link *Link) UnmarshalJSON(data []byte) error {
8888

8989
// Validate returns an error if Link does not comply with the OpenAPI spec.
9090
func (link *Link) Validate(ctx context.Context, opts ...ValidationOption) error {
91-
// ctx = WithValidationOptions(ctx, opts...)
91+
ctx = WithValidationOptions(ctx, opts...)
9292

9393
if link.OperationID == "" && link.OperationRef == "" {
9494
return errors.New("missing operationId or operationRef on link")
@@ -97,5 +97,5 @@ func (link *Link) Validate(ctx context.Context, opts ...ValidationOption) error
9797
return fmt.Errorf("operationId %q and operationRef %q are mutually exclusive", link.OperationID, link.OperationRef)
9898
}
9999

100-
return validateExtensions(link.Extensions)
100+
return validateExtensions(ctx, link.Extensions)
101101
}

openapi3/media_type.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ func (mediaType *MediaType) Validate(ctx context.Context, opts ...ValidationOpti
142142
}
143143
}
144144

145-
return validateExtensions(mediaType.Extensions)
145+
return validateExtensions(ctx, mediaType.Extensions)
146146
}
147147

148148
// JSONLookup implements github.com/go-openapi/jsonpointer#JSONPointable

0 commit comments

Comments
 (0)