Skip to content

Commit 96f9bd7

Browse files
committed
more test coverage
1 parent a0db03d commit 96f9bd7

File tree

1 file changed

+40
-28
lines changed

1 file changed

+40
-28
lines changed

strict/validator_test.go

Lines changed: 40 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3441,7 +3441,7 @@ components:
34413441
assert.Nil(t, result)
34423442
}
34433443

3444-
func TestStrictValidator_FindMatchingVariant_NoMatch(t *testing.T) {
3444+
func TestStrictValidator_FindMatchingVariant_NoMatch2(t *testing.T) {
34453445
yml := `openapi: "3.1.0"
34463446
info:
34473447
title: Test
@@ -3513,55 +3513,68 @@ components:
35133513
ctx := newTraversalContext(DirectionRequest, nil, "$.body")
35143514
result := v.validateObject(ctx, schema, map[string]any{"foo": "bar"})
35153515

3516-
// Empty schema with no properties means anything is allowed (additionalProperties defaults to true)
3517-
assert.Empty(t, result)
3516+
// In strict mode, empty schema with no properties still reports undeclared
3517+
// because additionalProperties defaults to true (meaning strict mode catches it)
3518+
assert.Len(t, result, 1)
3519+
assert.Equal(t, "foo", result[0].Name)
35183520
}
35193521

35203522
func TestStrictValidator_ShouldReportUndeclared_NilSchema(t *testing.T) {
35213523
opts := config.NewValidationOptions(config.WithStrictMode())
35223524
v := NewValidator(opts, 3.1)
35233525

3526+
// nil schema returns false - can't report undeclared without schema
35243527
result := v.shouldReportUndeclared(nil)
3525-
assert.True(t, result)
3528+
assert.False(t, result)
35263529
}
35273530

35283531
func TestStrictValidator_GetPatternPropertySchema_NoPatterns(t *testing.T) {
3532+
yml := `openapi: "3.1.0"
3533+
info:
3534+
title: Test
3535+
version: "1.0"
3536+
paths: {}
3537+
components:
3538+
schemas:
3539+
NoPatterns:
3540+
type: object
3541+
properties:
3542+
name:
3543+
type: string
3544+
`
3545+
model := buildSchemaFromYAML(t, yml)
3546+
schema := getSchema(t, model, "NoPatterns")
3547+
35293548
opts := config.NewValidationOptions(config.WithStrictMode())
35303549
v := NewValidator(opts, 3.1)
35313550

3532-
result := v.getPatternPropertySchema(nil, "foo")
3551+
// Schema has no patternProperties
3552+
result := v.getPatternPropertySchema(schema, "foo")
35333553
assert.Nil(t, result)
35343554
}
35353555

3536-
func TestStrictValidator_RecurseIntoDeclaredProperties_NilProperty(t *testing.T) {
3556+
func TestStrictValidator_RecurseIntoDeclaredProperties_EmptySchema(t *testing.T) {
35373557
yml := `openapi: "3.1.0"
35383558
info:
35393559
title: Test
35403560
version: "1.0"
35413561
paths: {}
35423562
components:
35433563
schemas:
3544-
User:
3564+
Empty:
35453565
type: object
3546-
properties:
3547-
name:
3548-
type: string
35493566
`
35503567
model := buildSchemaFromYAML(t, yml)
3551-
schema := getSchema(t, model, "User")
3568+
schema := getSchema(t, model, "Empty")
35523569

35533570
opts := config.NewValidationOptions(config.WithStrictMode())
35543571
v := NewValidator(opts, 3.1)
35553572

35563573
ctx := newTraversalContext(DirectionRequest, nil, "$.body")
3557-
3558-
// Create declared map with nil proxy
3559-
declared := make(map[string]*declaredProperty)
3560-
declared["name"] = &declaredProperty{proxy: nil}
3561-
35623574
data := map[string]any{"name": "test"}
35633575

3564-
result := v.recurseIntoDeclaredProperties(ctx, schema, data, declared)
3576+
// recurseIntoDeclaredProperties only takes ctx, schema, data
3577+
result := v.recurseIntoDeclaredProperties(ctx, schema, data)
35653578
assert.Empty(t, result)
35663579
}
35673580

@@ -3652,7 +3665,8 @@ func TestStrictValidator_FindPropertySchemaInMerged_NilProxy(t *testing.T) {
36523665
declared := make(map[string]*declaredProperty)
36533666
declared["name"] = &declaredProperty{proxy: nil}
36543667

3655-
result := v.findPropertySchemaInMerged(nil, "name", declared)
3668+
// findPropertySchemaInMerged takes (variant, parent, propName, declared)
3669+
result := v.findPropertySchemaInMerged(nil, nil, "name", declared)
36563670
assert.Nil(t, result)
36573671
}
36583672

@@ -3662,16 +3676,14 @@ func TestStrictValidator_RecurseIntoDeclaredPropertiesWithMerged_NilProxy(t *tes
36623676

36633677
ctx := newTraversalContext(DirectionRequest, nil, "$.body")
36643678

3665-
// Create parent declared with nil proxy
3666-
parentDeclared := make(map[string]*declaredProperty)
3667-
parentDeclared["name"] = &declaredProperty{proxy: nil}
3668-
3669-
// Create variant declared empty
3670-
variantDeclared := make(map[string]*declaredProperty)
3679+
// Create declared with nil proxy
3680+
declared := make(map[string]*declaredProperty)
3681+
declared["name"] = &declaredProperty{proxy: nil}
36713682

36723683
data := map[string]any{"name": "test"}
36733684

3674-
result := v.recurseIntoDeclaredPropertiesWithMerged(ctx, data, parentDeclared, variantDeclared)
3685+
// recurseIntoDeclaredPropertiesWithMerged takes (ctx, variant, parent, data, declared)
3686+
result := v.recurseIntoDeclaredPropertiesWithMerged(ctx, nil, nil, data, declared)
36753687
assert.Empty(t, result)
36763688
}
36773689

@@ -3705,9 +3717,9 @@ components:
37053717
assert.Empty(t, result)
37063718
}
37073719

3708-
func TestStrictValidator_CompilePattern_InvalidPattern(t *testing.T) {
3709-
// Test compilePattern with an invalid regex pattern
3710-
result := compilePattern("[invalid")
3720+
func TestStrictValidator_CompilePattern_EmptyPattern(t *testing.T) {
3721+
// Test compilePattern with empty pattern
3722+
result := compilePattern("")
37113723
assert.Nil(t, result)
37123724
}
37133725

0 commit comments

Comments
 (0)