| 1 |
From: Reinhard Tartler <[email protected]> |
| 2 |
Date: Mon, 15 Jun 2026 09:56:09 -0400 |
| 3 |
Subject: Fix validator to use libopenapi v0.38.0 lazy GetSpecJSON getters |
| 4 |
Description: In libopenapi v0.38.0, SpecJSON and SpecJSONBytes fields were |
| 5 |
made private and replaced with lazy GetSpecJSON() and GetSpecJSONBytes() |
| 6 |
getters. |
| 7 |
Forwarded: https://github.com/pb33f/libopenapi-validator/pull/286 |
| 8 |
Last-Update: 2026-06-15 |
| 9 |
Signed-off-by: Reinhard Tartler <[email protected]> |
| 10 |
--- |
| 11 |
schema_validation/validate_document.go | 14 +++++++------- |
| 12 |
schema_validation/validate_document_test.go | 9 +++++++++ |
| 13 |
2 files changed, 16 insertions(+), 7 deletions(-) |
| 14 |
|
| 15 |
diff --git a/schema_validation/validate_document.go b/schema_validation/validate_document.go |
| 16 |
index d539db4..e1ef930 100644 |
| 17 |
--- a/schema_validation/validate_document.go |
| 18 |
+++ b/schema_validation/validate_document.go |
| 19 |
@@ -236,7 +236,7 @@ func ValidateOpenAPIDocumentWithPrecompiled(doc libopenapi.Document, compiledSch |
| 20 |
var validationErrors []*liberrors.ValidationError |
| 21 |
|
| 22 |
// Check if both JSON representations are nil before proceeding |
| 23 |
- if info.SpecJSON == nil && info.SpecJSONBytes == nil { |
| 24 |
+ if info.GetSpecJSON() == nil && info.GetSpecJSONBytes() == nil { |
| 25 |
validationErrors = append(validationErrors, &liberrors.ValidationError{ |
| 26 |
ValidationType: helpers.Schema, |
| 27 |
ValidationSubType: "document", |
| 28 |
@@ -279,13 +279,13 @@ func ValidateOpenAPIDocumentWithPrecompiled(doc libopenapi.Document, compiledSch |
| 29 |
// Build the normalized document value for validation. |
| 30 |
// Prefer SpecJSONBytes (single unmarshal) over SpecJSON (marshal+unmarshal round-trip). |
| 31 |
var normalized any |
| 32 |
- if info.SpecJSONBytes != nil && len(*info.SpecJSONBytes) > 0 { |
| 33 |
+ if info.GetSpecJSONBytes() != nil && len(*info.GetSpecJSONBytes()) > 0 { |
| 34 |
var err error |
| 35 |
- normalized, err = jsonschema.UnmarshalJSON(bytes.NewReader(*info.SpecJSONBytes)) |
| 36 |
+ normalized, err = jsonschema.UnmarshalJSON(bytes.NewReader(*info.GetSpecJSONBytes())) |
| 37 |
if err != nil { |
| 38 |
// Fall back to normalizeJSON if UnmarshalJSON fails |
| 39 |
- if info.SpecJSON != nil { |
| 40 |
- normalized, err = normalizeJSON(*info.SpecJSON) |
| 41 |
+ if info.GetSpecJSON() != nil { |
| 42 |
+ normalized, err = normalizeJSON(*info.GetSpecJSON()) |
| 43 |
if err != nil { |
| 44 |
return false, []*liberrors.ValidationError{buildDocumentDecodeError( |
| 45 |
fmt.Sprintf("The OpenAPI document cannot be converted to JSON: %s", err.Error()), |
| 46 |
@@ -299,9 +299,9 @@ func ValidateOpenAPIDocumentWithPrecompiled(doc libopenapi.Document, compiledSch |
| 47 |
)} |
| 48 |
} |
| 49 |
} |
| 50 |
- } else if info.SpecJSON != nil { |
| 51 |
+ } else if info.GetSpecJSON() != nil { |
| 52 |
var err error |
| 53 |
- normalized, err = normalizeJSON(*info.SpecJSON) |
| 54 |
+ normalized, err = normalizeJSON(*info.GetSpecJSON()) |
| 55 |
if err != nil { |
| 56 |
return false, []*liberrors.ValidationError{buildDocumentDecodeError( |
| 57 |
fmt.Sprintf("The OpenAPI document cannot be converted to JSON: %s", err.Error()), |
| 58 |
diff --git a/schema_validation/validate_document_test.go b/schema_validation/validate_document_test.go |
| 59 |
index 6f8e2f1..434567a 100644 |
| 60 |
--- a/schema_validation/validate_document_test.go |
| 61 |
+++ b/schema_validation/validate_document_test.go |
| 62 |
@@ -210,6 +210,7 @@ paths: {}` |
| 63 |
}, |
| 64 |
} |
| 65 |
info := doc.GetSpecInfo() |
| 66 |
+ info.GetSpecJSON() |
| 67 |
info.SpecJSON = &badSpecJSON |
| 68 |
info.SpecJSONBytes = nil |
| 69 |
|
| 70 |
@@ -240,6 +241,7 @@ paths: {}` |
| 71 |
} |
| 72 |
corrupt := []byte(`{not valid json!!!}`) |
| 73 |
info := doc.GetSpecInfo() |
| 74 |
+ info.GetSpecJSON() |
| 75 |
info.SpecJSON = &badSpecJSON |
| 76 |
info.SpecJSONBytes = &corrupt |
| 77 |
|
| 78 |
@@ -431,6 +433,7 @@ info: |
| 79 |
|
| 80 |
// Simulate the nil SpecJSON scenario by setting both to nil |
| 81 |
info := doc.GetSpecInfo() |
| 82 |
+ info.GetSpecJSON() |
| 83 |
info.SpecJSON = nil |
| 84 |
info.SpecJSONBytes = nil |
| 85 |
|
| 86 |
@@ -460,6 +463,7 @@ func TestValidateDocument_WithPrecompiledSchema(t *testing.T) { |
| 87 |
doc, _ := libopenapi.NewDocument(petstore) |
| 88 |
|
| 89 |
info := doc.GetSpecInfo() |
| 90 |
+ info.GetSpecJSON() |
| 91 |
|
| 92 |
// Pre-compile the schema |
| 93 |
options := config.NewValidationOptions() |
| 94 |
@@ -482,6 +486,7 @@ func TestValidateDocument_WithPrecompiledSchema_Invalid(t *testing.T) { |
| 95 |
doc, _ := libopenapi.NewDocument(petstore) |
| 96 |
|
| 97 |
info := doc.GetSpecInfo() |
| 98 |
+ info.GetSpecJSON() |
| 99 |
|
| 100 |
// Pre-compile the schema |
| 101 |
options := config.NewValidationOptions() |
| 102 |
@@ -506,6 +511,7 @@ func TestValidateDocument_SpecJSONBytesPath(t *testing.T) { |
| 103 |
doc, _ := libopenapi.NewDocument(petstore) |
| 104 |
|
| 105 |
info := doc.GetSpecInfo() |
| 106 |
+ info.GetSpecJSON() |
| 107 |
|
| 108 |
// Nil out SpecJSON but leave SpecJSONBytes intact — forces the SpecJSONBytes path |
| 109 |
assert.NotNil(t, info.SpecJSONBytes, "SpecJSONBytes should be populated by libopenapi") |
| 110 |
@@ -521,6 +527,7 @@ func TestValidateDocument_SpecJSONBytesCorrupt_NilSpecJSON(t *testing.T) { |
| 111 |
doc, _ := libopenapi.NewDocument(petstore) |
| 112 |
|
| 113 |
info := doc.GetSpecInfo() |
| 114 |
+ info.GetSpecJSON() |
| 115 |
|
| 116 |
// Put corrupt bytes in SpecJSONBytes so UnmarshalJSON fails, |
| 117 |
// and nil out SpecJSON so the fallback normalizeJSON path is skipped. |
| 118 |
@@ -542,6 +549,7 @@ func TestValidateDocument_SpecJSONBytesCorrupt_FallbackToSpecJSON(t *testing.T) |
| 119 |
doc, _ := libopenapi.NewDocument(petstore) |
| 120 |
|
| 121 |
info := doc.GetSpecInfo() |
| 122 |
+ info.GetSpecJSON() |
| 123 |
|
| 124 |
// Put corrupt bytes in SpecJSONBytes so UnmarshalJSON fails, |
| 125 |
// but leave SpecJSON intact so the fallback to normalizeJSON executes. |
| 126 |
@@ -559,6 +567,7 @@ func TestValidateDocument_SpecJSONBytesPath_Invalid(t *testing.T) { |
| 127 |
doc, _ := libopenapi.NewDocument(petstore) |
| 128 |
|
| 129 |
info := doc.GetSpecInfo() |
| 130 |
+ info.GetSpecJSON() |
| 131 |
|
| 132 |
// Nil out SpecJSON but leave SpecJSONBytes intact |
| 133 |
assert.NotNil(t, info.SpecJSONBytes, "SpecJSONBytes should be populated by libopenapi") |