Skip to content

Commit 6697da8

Browse files
committed
fix: improve test coverage
1 parent 0bb210b commit 6697da8

2 files changed

Lines changed: 112 additions & 3 deletions

File tree

parameters/validate_parameter.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,6 @@ func ValidateParameterSchema(
156156
hash := schema.GoLow().Hash()
157157
if cached, ok := validationOptions.SchemaCache.Load(hash); ok && cached != nil && cached.CompiledSchema != nil {
158158
jsch = cached.CompiledSchema
159-
jsonSchema = cached.RenderedJSON
160159
}
161160
}
162161

parameters/validate_parameter_test.go

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@ import (
77
"testing"
88

99
"github.com/pb33f/libopenapi"
10+
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"
1011
"github.com/stretchr/testify/assert"
1112
"github.com/stretchr/testify/require"
1213

13-
lowv3 "github.com/pb33f/libopenapi/datamodel/low/v3"
14-
1514
"github.com/pb33f/libopenapi-validator/cache"
1615
"github.com/pb33f/libopenapi-validator/config"
1716
"github.com/pb33f/libopenapi-validator/helpers"
@@ -964,6 +963,117 @@ func Test_GetRenderedSchema_NilCache(t *testing.T) {
964963
assert.NotEmpty(t, result, "GetRenderedSchema should render schema even with nil cache")
965964
}
966965

966+
// Test_GetRenderedSchema_CacheMiss verifies GetRenderedSchema renders fresh when cache entry has empty RenderedJSON.
967+
// This tests the code path where cache lookup succeeds but RenderedJSON is empty.
968+
func Test_GetRenderedSchema_CacheMiss(t *testing.T) {
969+
spec := []byte(`{
970+
"openapi": "3.1.0",
971+
"info": {"title": "Test", "version": "1.0.0"},
972+
"paths": {
973+
"/test": {
974+
"get": {
975+
"parameters": [{
976+
"name": "id",
977+
"in": "query",
978+
"schema": {"type": "integer"}
979+
}],
980+
"responses": {"200": {"description": "OK"}}
981+
}
982+
}
983+
}
984+
}`)
985+
986+
doc, err := libopenapi.NewDocument(spec)
987+
require.NoError(t, err)
988+
989+
v3Model, errs := doc.BuildV3Model()
990+
require.Nil(t, errs)
991+
992+
// Get the parameter schema
993+
pathItem := v3Model.Model.Paths.PathItems.GetOrZero("/test")
994+
param := pathItem.Get.Parameters[0]
995+
schema := param.Schema.Schema()
996+
997+
// Create options with cache enabled
998+
opts := config.NewValidationOptions()
999+
require.NotNil(t, opts.SchemaCache)
1000+
1001+
// Store an entry with empty RenderedJSON to simulate cache miss scenario
1002+
hash := schema.GoLow().Hash()
1003+
opts.SchemaCache.Store(hash, &cache.SchemaCacheEntry{
1004+
Schema: schema,
1005+
RenderedJSON: nil, // Empty - should trigger fresh rendering
1006+
})
1007+
1008+
// GetRenderedSchema should render fresh since RenderedJSON is empty
1009+
result := GetRenderedSchema(schema, opts)
1010+
assert.NotEmpty(t, result, "GetRenderedSchema should render fresh when cached RenderedJSON is empty")
1011+
}
1012+
1013+
// Test_ValidateSingleParameterSchema_CacheMissCompiledSchema tests the path where cache entry
1014+
// exists but CompiledSchema is nil, forcing recompilation.
1015+
func Test_ValidateSingleParameterSchema_CacheMissCompiledSchema(t *testing.T) {
1016+
spec := []byte(`{
1017+
"openapi": "3.1.0",
1018+
"info": {"title": "Test", "version": "1.0.0"},
1019+
"paths": {
1020+
"/test/{id}": {
1021+
"get": {
1022+
"parameters": [{
1023+
"name": "id",
1024+
"in": "path",
1025+
"required": true,
1026+
"schema": {"type": "integer", "minimum": 1}
1027+
}],
1028+
"responses": {"200": {"description": "OK"}}
1029+
}
1030+
}
1031+
}
1032+
}`)
1033+
1034+
doc, err := libopenapi.NewDocument(spec)
1035+
require.NoError(t, err)
1036+
1037+
v3Model, errs := doc.BuildV3Model()
1038+
require.Nil(t, errs)
1039+
1040+
// Get the parameter schema
1041+
pathItem := v3Model.Model.Paths.PathItems.GetOrZero("/test/{id}")
1042+
param := pathItem.Get.Parameters[0]
1043+
schema := param.Schema.Schema()
1044+
1045+
// Create options with cache enabled
1046+
opts := config.NewValidationOptions()
1047+
require.NotNil(t, opts.SchemaCache)
1048+
1049+
// Store an entry with nil CompiledSchema to force recompilation
1050+
hash := schema.GoLow().Hash()
1051+
opts.SchemaCache.Store(hash, &cache.SchemaCacheEntry{
1052+
Schema: schema,
1053+
CompiledSchema: nil, // nil - should trigger recompilation
1054+
})
1055+
1056+
// Validate should still work by recompiling the schema
1057+
result := ValidateSingleParameterSchema(
1058+
schema,
1059+
int64(5), // valid integer
1060+
"Path parameter",
1061+
"The path parameter",
1062+
"id",
1063+
helpers.ParameterValidation,
1064+
helpers.ParameterValidationPath,
1065+
opts,
1066+
"/test/{id}",
1067+
"get",
1068+
)
1069+
assert.Empty(t, result, "Validation should pass for valid integer")
1070+
1071+
// Now verify the cache was populated with the compiled schema
1072+
cached, ok := opts.SchemaCache.Load(hash)
1073+
assert.True(t, ok, "Cache entry should exist")
1074+
assert.NotNil(t, cached.CompiledSchema, "CompiledSchema should be populated after validation")
1075+
}
1076+
9671077
// arrayValidationSpec is used to test array parameter validation with the updated function signatures
9681078
var arrayValidationSpec = []byte(`{
9691079
"openapi": "3.1.0",

0 commit comments

Comments
 (0)