From 055848fcf6561b9083ca44bef0638e3171da96f0 Mon Sep 17 00:00:00 2001 From: Toan Nguyen Date: Sun, 24 Nov 2024 01:54:12 +0700 Subject: [PATCH] improve xml encode and decode --- connector/internal/xml_decode.go | 5 +-- connector/internal/xml_encode.go | 3 +- connector/internal/xml_encode_test.go | 5 +-- .../openapi/testdata/petstore3/expected.json | 36 +++++++++++++++++++ .../openapi/testdata/petstore3/schema.json | 18 ++++++++++ .../openapi/testdata/petstore3/source.json | 14 ++++++++ 6 files changed, 75 insertions(+), 6 deletions(-) diff --git a/connector/internal/xml_decode.go b/connector/internal/xml_decode.go index f46c615..156d8b8 100644 --- a/connector/internal/xml_decode.go +++ b/connector/internal/xml_decode.go @@ -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 { @@ -169,6 +169,7 @@ func (c *XMLDecoder) evalNamedField(block *xmlBlock, t *schema.NamedType, fieldP if err != nil { return nil, err } + result[key] = attrValue break diff --git a/connector/internal/xml_encode.go b/connector/internal/xml_encode.go index 6bb3e27..14dc64e 100644 --- a/connector/internal/xml_encode.go +++ b/connector/internal/xml_encode.go @@ -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, }) } diff --git a/connector/internal/xml_encode_test.go b/connector/internal/xml_encode_test.go index e8dcbc9..1f161f2 100644 --- a/connector/internal/xml_encode_test.go +++ b/connector/internal/xml_encode_test.go @@ -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", @@ -65,7 +66,7 @@ func TestCreateXMLForm(t *testing.T) { }, }, Expected: ` -This is a pretty cool request!This is a pretty cool project!This is a pretty cool package!`, +This is a pretty cool request!This is a pretty cool project!This is a pretty cool package!`, }, { Name: "putBookXml", diff --git a/ndc-http-schema/openapi/testdata/petstore3/expected.json b/ndc-http-schema/openapi/testdata/petstore3/expected.json index 5b23bac..2fe8af3 100644 --- a/ndc-http-schema/openapi/testdata/petstore3/expected.json +++ b/ndc-http-schema/openapi/testdata/petstore3/expected.json @@ -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", @@ -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", diff --git a/ndc-http-schema/openapi/testdata/petstore3/schema.json b/ndc-http-schema/openapi/testdata/petstore3/schema.json index 7c0b825..dd827d7 100644 --- a/ndc-http-schema/openapi/testdata/petstore3/schema.json +++ b/ndc-http-schema/openapi/testdata/petstore3/schema.json @@ -3657,6 +3657,15 @@ } } }, + "comment_count": { + "type": { + "type": "nullable", + "underlying_type": { + "name": "Int32", + "type": "named" + } + } + }, "package": { "type": { "type": "nullable", @@ -3783,6 +3792,15 @@ } } }, + "comment_count": { + "type": { + "type": "nullable", + "underlying_type": { + "name": "Int32", + "type": "named" + } + } + }, "package": { "type": { "type": "nullable", diff --git a/ndc-http-schema/openapi/testdata/petstore3/source.json b/ndc-http-schema/openapi/testdata/petstore3/source.json index 79b4ce5..9fcf315 100644 --- a/ndc-http-schema/openapi/testdata/petstore3/source.json +++ b/ndc-http-schema/openapi/testdata/petstore3/source.json @@ -3313,6 +3313,13 @@ "schema": { "type": "object", "properties": { + "comment_count": { + "type": "integer", + "xml": { + "attribute": true, + "name": "comment" + } + }, "comment": { "type": "array", "items": { @@ -3402,6 +3409,13 @@ "schema": { "type": "object", "properties": { + "comment_count": { + "type": "integer", + "xml": { + "attribute": true, + "name": "comment" + } + }, "comment": { "type": "array", "items": {