Skip to content

Commit f040207

Browse files
committed
fix test coverage and liniting issues
1 parent 90324ef commit f040207

File tree

2 files changed

+60
-5
lines changed

2 files changed

+60
-5
lines changed

strict/types_test.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"testing"
88

99
"github.com/pb33f/libopenapi"
10+
"github.com/pb33f/libopenapi/datamodel/high/base"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
1213
)
@@ -51,10 +52,17 @@ paths:
5152
assert.Greater(t, col, 0, "col should be greater than 0")
5253
}
5354

54-
func TestExtractSchemaLocation_InlineSchema(t *testing.T) {
55-
// Test with a schema that has GoLow() returning non-nil but RootNode nil
56-
// This is hard to construct directly, but we can test the nil case
57-
line, col := extractSchemaLocation(nil)
55+
func TestExtractSchemaLocation_SchemaWithNilGoLow(t *testing.T) {
56+
// Create a high-level schema programmatically (no GoLow())
57+
// This covers types.go:108 where low == nil
58+
schema := &base.Schema{
59+
Type: []string{"object"},
60+
}
61+
62+
// GoLow() returns nil for programmatically created schemas
63+
require.Nil(t, schema.GoLow())
64+
65+
line, col := extractSchemaLocation(schema)
5866
assert.Equal(t, 0, line)
5967
assert.Equal(t, 0, col)
6068
}
@@ -117,4 +125,3 @@ func TestNewUndeclaredProperty_WithNilSchema(t *testing.T) {
117125
assert.Equal(t, 0, undeclared.SpecLine, "SpecLine should be 0 for nil schema")
118126
assert.Equal(t, 0, undeclared.SpecCol, "SpecCol should be 0 for nil schema")
119127
}
120-

strict/validator_test.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6135,3 +6135,51 @@ components:
61356135
assert.True(t, result.Valid)
61366136
assert.Empty(t, result.UndeclaredValues)
61376137
}
6138+
6139+
func TestStrictValidator_ValidateVariantWithParent_VariantNilGoLow(t *testing.T) {
6140+
// Covers polymorphic.go:233-234 - fallback to parent when variant.GoLow() is nil
6141+
// This tests the defensive code path for programmatically created schemas
6142+
6143+
// Build a real parent schema from YAML (has GoLow())
6144+
yml := `openapi: "3.1.0"
6145+
info:
6146+
title: Test
6147+
version: "1.0"
6148+
paths: {}
6149+
components:
6150+
schemas:
6151+
ParentSchema:
6152+
type: object
6153+
properties:
6154+
parentProp:
6155+
type: string
6156+
`
6157+
model := buildSchemaFromYAML(t, yml)
6158+
parent := getSchema(t, model, "ParentSchema")
6159+
require.NotNil(t, parent.GoLow())
6160+
6161+
// Create a variant schema programmatically (no GoLow())
6162+
variant := &base.Schema{
6163+
Type: []string{"object"},
6164+
}
6165+
require.Nil(t, variant.GoLow())
6166+
6167+
opts := config.NewValidationOptions(config.WithStrictMode())
6168+
v := NewValidator(opts, 3.1)
6169+
6170+
ctx := newTraversalContext(DirectionRequest, nil, "$.body")
6171+
6172+
// Data with undeclared property - triggers line 230-237
6173+
data := map[string]any{
6174+
"parentProp": "value",
6175+
"undeclared": "triggers undeclared path",
6176+
}
6177+
6178+
result := v.validateVariantWithParent(ctx, parent, variant, data)
6179+
6180+
// Should detect undeclared property and use parent's location (since variant has no GoLow)
6181+
assert.Len(t, result, 1)
6182+
assert.Equal(t, "undeclared", result[0].Name)
6183+
// Location should come from parent (not crash due to nil variant.GoLow())
6184+
assert.Greater(t, result[0].SpecLine, 0)
6185+
}

0 commit comments

Comments
 (0)