Skip to content

Commit c5bcbdd

Browse files
openapi3: update tests for OAS 3.1 field gate
Three tests need adjustment now that OAS 3.1-only schema fields are rejected under OAS 3.0 validation: - TestSchemaIfThenElse_Validate and TestSchemaValidate31SubSchemas exercise 3.1 features (If/Then/Else, PrefixItems) directly via Schema.Validate without a doc, so the isOpenAPI31OrLater flag is never set from a doc version. Pass IsOpenAPI31OrLater() explicitly. - TestIssue495WithDraft04{,Bis} load OAS 3.0 documents that $ref external JSON Schema draft-04 meta-schemas (which contain $id and $schema). Add AllowExtraSiblingFields("$id", "$schema") so the tests assertion about the unresolved inner "#" ref surfaces as intended, matching the pattern already used in issue513_test.go. Co-Authored-By: Claude Opus 4 <noreply@anthropic.com>
1 parent 6959da9 commit c5bcbdd

3 files changed

Lines changed: 13 additions & 7 deletions

File tree

openapi3/issue495_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,10 @@ paths:
121121
doc, err := sl.LoadFromData(spec)
122122
require.NoError(t, err)
123123

124-
err = doc.Validate(sl.Context)
124+
// draft-04 meta-schema contains $id and $schema; in OAS 3.0 these require
125+
// opt-in via AllowExtraSiblingFields so the test can assert its real target
126+
// (the unresolved inner "#" ref).
127+
err = doc.Validate(sl.Context, AllowExtraSiblingFields("$id", "$schema"))
125128
require.ErrorContains(t, err, `found unresolved ref: "#"`)
126129
}
127130

@@ -157,6 +160,9 @@ paths:
157160
doc, err := sl.LoadFromData(spec)
158161
require.NoError(t, err)
159162

160-
err = doc.Validate(sl.Context)
163+
// draft-04 meta-schema contains $id and $schema; in OAS 3.0 these require
164+
// opt-in via AllowExtraSiblingFields so the test can assert its real target
165+
// (the unresolved inner "#" ref).
166+
err = doc.Validate(sl.Context, AllowExtraSiblingFields("$id", "$schema"))
161167
require.ErrorContains(t, err, `found unresolved ref: "#"`)
162168
}

openapi3/schema_if_then_else_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ func TestSchemaIfThenElse_Validate(t *testing.T) {
176176
schema := &openapi3.Schema{
177177
If: &openapi3.SchemaRef{Ref: "#/components/schemas/Missing"},
178178
}
179-
err := schema.Validate(context.Background())
179+
err := schema.Validate(context.Background(), openapi3.IsOpenAPI31OrLater())
180180
require.Error(t, err)
181181
require.ErrorContains(t, err, "unresolved ref")
182182
})
@@ -185,7 +185,7 @@ func TestSchemaIfThenElse_Validate(t *testing.T) {
185185
schema := &openapi3.Schema{
186186
Then: &openapi3.SchemaRef{Ref: "#/components/schemas/Missing"},
187187
}
188-
err := schema.Validate(context.Background())
188+
err := schema.Validate(context.Background(), openapi3.IsOpenAPI31OrLater())
189189
require.Error(t, err)
190190
require.ErrorContains(t, err, "unresolved ref")
191191
})
@@ -194,7 +194,7 @@ func TestSchemaIfThenElse_Validate(t *testing.T) {
194194
schema := &openapi3.Schema{
195195
Else: &openapi3.SchemaRef{Ref: "#/components/schemas/Missing"},
196196
}
197-
err := schema.Validate(context.Background())
197+
err := schema.Validate(context.Background(), openapi3.IsOpenAPI31OrLater())
198198
require.Error(t, err)
199199
require.ErrorContains(t, err, "unresolved ref")
200200
})
@@ -205,7 +205,7 @@ func TestSchemaIfThenElse_Validate(t *testing.T) {
205205
Then: &openapi3.SchemaRef{Value: &openapi3.Schema{MinLength: 1}},
206206
Else: &openapi3.SchemaRef{Value: &openapi3.Schema{Type: &openapi3.Types{"number"}}},
207207
}
208-
err := schema.Validate(context.Background())
208+
err := schema.Validate(context.Background(), openapi3.IsOpenAPI31OrLater())
209209
require.NoError(t, err)
210210
})
211211
}

openapi3/schema_validate_31_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
)
1010

1111
func TestSchemaValidate31SubSchemas(t *testing.T) {
12-
ctx := context.Background()
12+
ctx := openapi3.WithValidationOptions(context.Background(), openapi3.IsOpenAPI31OrLater())
1313

1414
// Helper: a schema with an invalid nested schema (pattern with bad regex)
1515
invalidSchema := &openapi3.Schema{

0 commit comments

Comments
 (0)