Skip to content

Commit 408847e

Browse files
committed
updated coverage
1 parent bc1a3a6 commit 408847e

File tree

1 file changed

+69
-60
lines changed

1 file changed

+69
-60
lines changed

schema_validation/validate_xml_test.go

Lines changed: 69 additions & 60 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
)
1213

@@ -748,36 +749,30 @@ func TestValidateXML_NilSchema(t *testing.T) {
748749
assert.Len(t, validationErrors, 0)
749750
}
750751

751-
func TestValidateXML_TrulyMalformedXML(t *testing.T) {
752-
spec := `openapi: 3.0.0
753-
paths:
754-
/test:
755-
get:
756-
responses:
757-
'200':
758-
content:
759-
application/xml:
760-
schema:
761-
type: object
762-
xml:
763-
name: Test`
764-
765-
doc, err := libopenapi.NewDocument([]byte(spec))
766-
assert.NoError(t, err)
752+
func TestValidateXML_NilSchemaInTransformation(t *testing.T) {
753+
// directly test applyXMLTransformations with nil schema (line 94)
754+
result := applyXMLTransformations(map[string]interface{}{"test": "value"}, nil)
755+
assert.NotNil(t, result)
756+
assert.Equal(t, map[string]interface{}{"test": "value"}, result)
757+
}
767758

768-
v3Doc, err := doc.BuildV3Model()
769-
assert.NoError(t, err)
759+
func TestValidateXML_TransformWithNilPropertySchemaProxy(t *testing.T) {
760+
// directly test applyXMLTransformations when a property schema proxy returns nil (line 119)
761+
// this can happen with circular refs or unresolved refs in edge cases
770762

771-
schema := v3Doc.Model.Paths.PathItems.GetOrZero("/test").Get.Responses.Codes.GetOrZero("200").
772-
Content.GetOrZero("application/xml").Schema.Schema()
763+
// create a schema with properties but we'll simulate a nil schema scenario
764+
// by testing the transformation directly
765+
data := map[string]interface{}{
766+
"test": "value",
767+
}
773768

774-
validator := NewSchemaValidator()
769+
// schema with properties but no XML config - tests property iteration
770+
schema := &base.Schema{
771+
Properties: nil, // will trigger line 109 early return
772+
}
775773

776-
// test with completely malformed xml - mismatched tags
777-
valid, validationErrors := validator.ValidateXMLString(schema, "<Test><bad>value</wrong></Test>")
778-
assert.False(t, valid)
779-
assert.NotEmpty(t, validationErrors)
780-
assert.Contains(t, validationErrors[0].Reason, "xml")
774+
result := applyXMLTransformations(data, schema)
775+
assert.Equal(t, data, result)
781776
}
782777

783778
func TestValidateXML_NoProperties(t *testing.T) {
@@ -922,49 +917,63 @@ paths:
922917
valid, validationErrors := validator.ValidateXMLString(schema, xmlWithWrongItemName)
923918

924919
// it should still process (might fail schema validation but won't crash)
920+
_ = valid
925921
assert.NotNil(t, validationErrors)
926922
}
927923

928-
func TestValidateXML_WrappedArrayAsNonMap(t *testing.T) {
929-
spec := `openapi: 3.0.0
930-
paths:
931-
/list:
932-
get:
933-
responses:
934-
'200':
935-
content:
936-
application/xml:
937-
schema:
938-
type: object
939-
properties:
940-
values:
941-
type: array
942-
xml:
943-
wrapped: true
944-
items:
945-
type: string
946-
xml:
947-
name: value
948-
xml:
949-
name: List`
924+
func TestValidateXML_DirectArrayValue(t *testing.T) {
925+
// test unwrapArrayElement with non-map value (line 160)
926+
schema := &base.Schema{
927+
Type: []string{"array"},
928+
Items: &base.DynamicValue[*base.SchemaProxy, bool]{
929+
A: &base.SchemaProxy{},
930+
},
931+
XML: &base.XML{
932+
Wrapped: true,
933+
},
934+
}
950935

951-
doc, err := libopenapi.NewDocument([]byte(spec))
952-
assert.NoError(t, err)
936+
// when val is already an array (not a map), it should return as-is
937+
arrayVal := []interface{}{"one", "two", "three"}
938+
result := unwrapArrayElement(arrayVal, schema)
939+
assert.Equal(t, arrayVal, result)
940+
}
953941

954-
v3Doc, err := doc.BuildV3Model()
955-
assert.NoError(t, err)
942+
func TestValidateXML_UnwrapArrayElementMissingItem(t *testing.T) {
943+
// test unwrapArrayElement when wrapper map doesn't contain expected item (line 177)
944+
schema := &base.Schema{
945+
Type: []string{"array"},
946+
Items: &base.DynamicValue[*base.SchemaProxy, bool]{
947+
A: &base.SchemaProxy{},
948+
},
949+
XML: &base.XML{
950+
Wrapped: true,
951+
},
952+
}
956953

957-
schema := v3Doc.Model.Paths.PathItems.GetOrZero("/list").Get.Responses.Codes.GetOrZero("200").
958-
Content.GetOrZero("application/xml").Schema.Schema()
954+
// wrapper map contains wrong key - should return map as-is (line 177)
955+
wrapperMap := map[string]interface{}{"wrongKey": []interface{}{"one", "two"}}
956+
result := unwrapArrayElement(wrapperMap, schema)
957+
assert.Equal(t, wrapperMap, result)
958+
}
959959

960-
validator := NewSchemaValidator()
960+
func TestTransformXMLToSchemaJSON_EmptyString(t *testing.T) {
961+
// test empty string error path (line 68)
962+
schema := &base.Schema{}
963+
_, err := transformXMLToSchemaJSON("", schema)
964+
assert.Error(t, err)
965+
assert.Contains(t, err.Error(), "empty xml")
966+
}
961967

962-
// unwrapped array (direct values) - tests non-map value path
963-
validXML := `<List><values>one</values><values>two</values></List>`
964-
valid, validationErrors := validator.ValidateXMLString(schema, validXML)
965968

966-
// this tests the path where val is already an array, not a wrapper map
967-
assert.NotNil(t, validationErrors)
969+
func TestApplyXMLTransformations_NoXMLName(t *testing.T) {
970+
// test schema without xml.name - data stays wrapped
971+
schema := &base.Schema{
972+
Properties: nil,
973+
}
974+
data := map[string]interface{}{"Cat": map[string]interface{}{"nice": "true"}}
975+
result := applyXMLTransformations(data, schema)
976+
assert.Equal(t, data, result)
968977
}
969978

970979
func TestIsXMLContentType(t *testing.T) {

0 commit comments

Comments
 (0)