From: Reinhard Tartler Date: Mon, 15 Jun 2026 09:56:09 -0400 Subject: Fix validator to use libopenapi v0.38.0 lazy GetSpecJSON getters Description: In libopenapi v0.38.0, SpecJSON and SpecJSONBytes fields were made private and replaced with lazy GetSpecJSON() and GetSpecJSONBytes() getters. Forwarded: https://github.com/pb33f/libopenapi-validator/pull/286 Last-Update: 2026-06-15 Signed-off-by: Reinhard Tartler --- schema_validation/validate_document.go | 14 +++++++------- schema_validation/validate_document_test.go | 9 +++++++++ 2 files changed, 16 insertions(+), 7 deletions(-) diff --git a/schema_validation/validate_document.go b/schema_validation/validate_document.go index d539db4..e1ef930 100644 --- a/schema_validation/validate_document.go +++ b/schema_validation/validate_document.go @@ -236,7 +236,7 @@ func ValidateOpenAPIDocumentWithPrecompiled(doc libopenapi.Document, compiledSch var validationErrors []*liberrors.ValidationError // Check if both JSON representations are nil before proceeding - if info.SpecJSON == nil && info.SpecJSONBytes == nil { + if info.GetSpecJSON() == nil && info.GetSpecJSONBytes() == nil { validationErrors = append(validationErrors, &liberrors.ValidationError{ ValidationType: helpers.Schema, ValidationSubType: "document", @@ -279,13 +279,13 @@ func ValidateOpenAPIDocumentWithPrecompiled(doc libopenapi.Document, compiledSch // Build the normalized document value for validation. // Prefer SpecJSONBytes (single unmarshal) over SpecJSON (marshal+unmarshal round-trip). var normalized any - if info.SpecJSONBytes != nil && len(*info.SpecJSONBytes) > 0 { + if info.GetSpecJSONBytes() != nil && len(*info.GetSpecJSONBytes()) > 0 { var err error - normalized, err = jsonschema.UnmarshalJSON(bytes.NewReader(*info.SpecJSONBytes)) + normalized, err = jsonschema.UnmarshalJSON(bytes.NewReader(*info.GetSpecJSONBytes())) if err != nil { // Fall back to normalizeJSON if UnmarshalJSON fails - if info.SpecJSON != nil { - normalized, err = normalizeJSON(*info.SpecJSON) + if info.GetSpecJSON() != nil { + normalized, err = normalizeJSON(*info.GetSpecJSON()) if err != nil { return false, []*liberrors.ValidationError{buildDocumentDecodeError( fmt.Sprintf("The OpenAPI document cannot be converted to JSON: %s", err.Error()), @@ -299,9 +299,9 @@ func ValidateOpenAPIDocumentWithPrecompiled(doc libopenapi.Document, compiledSch )} } } - } else if info.SpecJSON != nil { + } else if info.GetSpecJSON() != nil { var err error - normalized, err = normalizeJSON(*info.SpecJSON) + normalized, err = normalizeJSON(*info.GetSpecJSON()) if err != nil { return false, []*liberrors.ValidationError{buildDocumentDecodeError( fmt.Sprintf("The OpenAPI document cannot be converted to JSON: %s", err.Error()), diff --git a/schema_validation/validate_document_test.go b/schema_validation/validate_document_test.go index 6f8e2f1..434567a 100644 --- a/schema_validation/validate_document_test.go +++ b/schema_validation/validate_document_test.go @@ -210,6 +210,7 @@ paths: {}` }, } info := doc.GetSpecInfo() + info.GetSpecJSON() info.SpecJSON = &badSpecJSON info.SpecJSONBytes = nil @@ -240,6 +241,7 @@ paths: {}` } corrupt := []byte(`{not valid json!!!}`) info := doc.GetSpecInfo() + info.GetSpecJSON() info.SpecJSON = &badSpecJSON info.SpecJSONBytes = &corrupt @@ -431,6 +433,7 @@ info: // Simulate the nil SpecJSON scenario by setting both to nil info := doc.GetSpecInfo() + info.GetSpecJSON() info.SpecJSON = nil info.SpecJSONBytes = nil @@ -460,6 +463,7 @@ func TestValidateDocument_WithPrecompiledSchema(t *testing.T) { doc, _ := libopenapi.NewDocument(petstore) info := doc.GetSpecInfo() + info.GetSpecJSON() // Pre-compile the schema options := config.NewValidationOptions() @@ -482,6 +486,7 @@ func TestValidateDocument_WithPrecompiledSchema_Invalid(t *testing.T) { doc, _ := libopenapi.NewDocument(petstore) info := doc.GetSpecInfo() + info.GetSpecJSON() // Pre-compile the schema options := config.NewValidationOptions() @@ -506,6 +511,7 @@ func TestValidateDocument_SpecJSONBytesPath(t *testing.T) { doc, _ := libopenapi.NewDocument(petstore) info := doc.GetSpecInfo() + info.GetSpecJSON() // Nil out SpecJSON but leave SpecJSONBytes intact — forces the SpecJSONBytes path assert.NotNil(t, info.SpecJSONBytes, "SpecJSONBytes should be populated by libopenapi") @@ -521,6 +527,7 @@ func TestValidateDocument_SpecJSONBytesCorrupt_NilSpecJSON(t *testing.T) { doc, _ := libopenapi.NewDocument(petstore) info := doc.GetSpecInfo() + info.GetSpecJSON() // Put corrupt bytes in SpecJSONBytes so UnmarshalJSON fails, // and nil out SpecJSON so the fallback normalizeJSON path is skipped. @@ -542,6 +549,7 @@ func TestValidateDocument_SpecJSONBytesCorrupt_FallbackToSpecJSON(t *testing.T) doc, _ := libopenapi.NewDocument(petstore) info := doc.GetSpecInfo() + info.GetSpecJSON() // Put corrupt bytes in SpecJSONBytes so UnmarshalJSON fails, // but leave SpecJSON intact so the fallback to normalizeJSON executes. @@ -559,6 +567,7 @@ func TestValidateDocument_SpecJSONBytesPath_Invalid(t *testing.T) { doc, _ := libopenapi.NewDocument(petstore) info := doc.GetSpecInfo() + info.GetSpecJSON() // Nil out SpecJSON but leave SpecJSONBytes intact assert.NotNil(t, info.SpecJSONBytes, "SpecJSONBytes should be populated by libopenapi")