Skip to content

Commit

Permalink
improve xml encode and decode
Browse files Browse the repository at this point in the history
  • Loading branch information
hgiasac committed Nov 23, 2024
1 parent a5aba1d commit 055848f
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 6 deletions.
5 changes: 3 additions & 2 deletions connector/internal/xml_decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ func (c *XMLDecoder) evalNamedField(block *xmlBlock, t *schema.NamedType, fieldP

for _, attr := range block.Start.Attr {
for key, objectField := range objectType.Fields {
if objectField.HTTP == nil {
if objectField.HTTP == nil || objectField.HTTP.XML == nil || !objectField.HTTP.XML.Attribute {
continue
}

xmlKey := key
if objectField.HTTP.XML != nil && objectField.HTTP.XML.Name != "" {
if objectField.HTTP.XML.Name != "" {
xmlKey = objectField.HTTP.XML.Name
}
if attr.Name.Local != xmlKey {
Expand All @@ -169,6 +169,7 @@ func (c *XMLDecoder) evalNamedField(block *xmlBlock, t *schema.NamedType, fieldP
if err != nil {
return nil, err
}

result[key] = attrValue

break
Expand Down
3 changes: 1 addition & 2 deletions connector/internal/xml_encode.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,8 @@ func (c *XMLEncoder) evalAttributes(objectType rest.ObjectType, keys []string, v
continue
}

xmlName := getTypeSchemaXMLName(objectField.HTTP, key)
attrs = append(attrs, xml.Attr{
Name: xml.Name{Local: xmlName},
Name: xml.Name{Local: getTypeSchemaXMLName(objectField.HTTP, key)},
Value: *str,
})
}
Expand Down
5 changes: 3 additions & 2 deletions connector/internal/xml_encode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ func TestCreateXMLForm(t *testing.T) {
{
Name: "putCommentXml",
Body: map[string]any{
"user": "Iggy",
"user": "Iggy",
"comment_count": int64(6),
"comment": []any{
map[string]any{
"who": "Iggy",
Expand All @@ -65,7 +66,7 @@ func TestCreateXMLForm(t *testing.T) {
},
},
Expected: `<?xml version="1.0" encoding="UTF-8"?>
<comments user="Iggy"><comment bsrequest="115" id="1" when="2021-10-15 13:28:22 UTC" who="Iggy">This is a pretty cool request!</comment><comment id="2" project="home:Admin" when="2021-10-15 13:49:39 UTC" who="Iggy">This is a pretty cool project!</comment><comment id="3" package="0ad" project="home:Admin" when="2021-10-15 13:54:38 UTC" who="Iggy">This is a pretty cool package!</comment></comments>`,
<comments comment="6" user="Iggy"><comment bsrequest="115" id="1" when="2021-10-15 13:28:22 UTC" who="Iggy">This is a pretty cool request!</comment><comment id="2" project="home:Admin" when="2021-10-15 13:49:39 UTC" who="Iggy">This is a pretty cool project!</comment><comment id="3" package="0ad" project="home:Admin" when="2021-10-15 13:54:38 UTC" who="Iggy">This is a pretty cool package!</comment></comments>`,
},
{
Name: "putBookXml",
Expand Down
36 changes: 36 additions & 0 deletions ndc-http-schema/openapi/testdata/petstore3/expected.json
Original file line number Diff line number Diff line change
Expand Up @@ -5930,6 +5930,24 @@
}
}
},
"comment_count": {
"type": {
"type": "nullable",
"underlying_type": {
"name": "Int32",
"type": "named"
}
},
"http": {
"type": [
"integer"
],
"xml": {
"name": "comment",
"attribute": true
}
}
},
"package": {
"type": {
"type": "nullable",
Expand Down Expand Up @@ -6165,6 +6183,24 @@
}
}
},
"comment_count": {
"type": {
"type": "nullable",
"underlying_type": {
"name": "Int32",
"type": "named"
}
},
"http": {
"type": [
"integer"
],
"xml": {
"name": "comment",
"attribute": true
}
}
},
"package": {
"type": {
"type": "nullable",
Expand Down
18 changes: 18 additions & 0 deletions ndc-http-schema/openapi/testdata/petstore3/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -3657,6 +3657,15 @@
}
}
},
"comment_count": {
"type": {
"type": "nullable",
"underlying_type": {
"name": "Int32",
"type": "named"
}
}
},
"package": {
"type": {
"type": "nullable",
Expand Down Expand Up @@ -3783,6 +3792,15 @@
}
}
},
"comment_count": {
"type": {
"type": "nullable",
"underlying_type": {
"name": "Int32",
"type": "named"
}
}
},
"package": {
"type": {
"type": "nullable",
Expand Down
14 changes: 14 additions & 0 deletions ndc-http-schema/openapi/testdata/petstore3/source.json
Original file line number Diff line number Diff line change
Expand Up @@ -3313,6 +3313,13 @@
"schema": {
"type": "object",
"properties": {
"comment_count": {
"type": "integer",
"xml": {
"attribute": true,
"name": "comment"
}
},
"comment": {
"type": "array",
"items": {
Expand Down Expand Up @@ -3402,6 +3409,13 @@
"schema": {
"type": "object",
"properties": {
"comment_count": {
"type": "integer",
"xml": {
"attribute": true,
"name": "comment"
}
},
"comment": {
"type": "array",
"items": {
Expand Down

0 comments on commit 055848f

Please sign in to comment.