{"id":207782,"date":"2025-07-08T10:05:52","date_gmt":"2025-07-08T02:05:52","guid":{"rendered":"https:\/\/server.hk\/cnblog\/207782\/"},"modified":"2025-07-08T10:05:52","modified_gmt":"2025-07-08T02:05:52","slug":"%e4%bb%8e-go-%e7%bb%93%e6%9e%84%e7%94%9f%e6%88%90-openapi-xml-%e6%a8%a1%e5%9e%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/207782\/","title":{"rendered":"\u4ece Go \u7ed3\u6784\u751f\u6210 OpenAPI XML \u6a21\u578b"},"content":{"rendered":"<p><b><\/b> <\/p>\n<p>\u5f53\u524d\u4f4d\u7f6e\uff1a <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>&gt;<\/span> <span>\u4ece Go \u7ed3\u6784\u751f\u6210 OpenAPI XML \u6a21\u578b<\/span><\/p>\n<p><span>\u6765\u6e90\uff1astackoverflow<\/span><br \/>\n<span>2024-04-25 16:09:34<\/span><br \/>\n<span><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u5728IT\u884c\u4e1a\u8fd9\u4e2a\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u5f88\u5feb\u7684\u884c\u4e1a\uff0c\u53ea\u6709\u4e0d\u505c\u6b62\u7684\u5b66\u4e60\uff0c\u624d\u4e0d\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u5982\u679c\u4f60\u662fGolang\u5b66\u4e60\u8005\uff0c\u90a3\u4e48\u672c\u6587\u300a\u4ece Go \u7ed3\u6784\u751f\u6210 OpenAPI XML \u6a21\u578b\u300b\u5c31\u5f88\u9002\u5408\u4f60\uff01\u672c\u7bc7\u5185\u5bb9\u4e3b\u8981\u5305\u62ec##content_title##\uff0c\u5e0c\u671b\u5bf9\u5927\u5bb6\u7684\u77e5\u8bc6\u79ef\u7d2f\u6709\u6240\u5e2e\u52a9\uff0c\u52a9\u529b\u5b9e\u6218\u5f00\u53d1\uff01<\/p>\n<p> \u95ee\u9898\u5185\u5bb9<br \/>\n <\/p>\n<p>\u6709\u6ca1\u6709\u529e\u6cd5\u4f7f\u7528golang\u7ed3\u6784\u751f\u6210xml\u7684openapi\u89c4\u8303\u6587\u6863\uff1f\u6211\u7684\u7ed3\u6784\u4f7f\u7528\u7f16\u7801\/xml \u6ce8\u91ca\u8fdb\u884c\u6ce8\u91ca\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n<pre>type Error struct {\n    Text      string `xml:\",chardata\"`\n    Type      string `xml:\"Type,attr\"`\n    Code      string `xml:\"Code,attr\"`\n    ShortText string `xml:\"ShortText,attr\"`\n}\n<\/pre>\n<p>\u6211\u60f3\u81ea\u52a8\u751f\u6210\u4e00\u4e2a openapi \u6a21\u578b\uff0c\u5b83\u5c0a\u91cd\u8fd9\u4e9b\u6ce8\u91ca<\/p>\n<p> <\/p>\n<h2>\u6b63\u786e\u7b54\u6848<\/h2>\n<p> <\/p>\n<p>\u60a8\u53ef\u4ee5\u4f7f\u7528\u6807\u51c6\u5e93\u4e2d\u7684 <code>reflect<\/code> \u5305\u6765\u68c0\u67e5\u7ed3\u6784\u53ca\u5176\u6807\u7b7e\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528 <code>gopkg.in\/yaml.v3<\/code> \u5305\u6765\u751f\u6210 open api \u683c\u5f0f\u3002\u60a8\u53ea\u9700\u7f16\u5199\u5c06\u60a8\u7684\u7c7b\u578b\u8f6c\u6362\u4e3a\u53e6\u4e00\u79cd\u7c7b\u578b\u7684\u903b\u8f91\uff0c\u5373\u4e0e open api \u6587\u6863\u7684\u7ed3\u6784\u76f8\u5339\u914d\u7684\u7c7b\u578b\u3002<\/p>\n<p>\u4ee5\u4e0b\u662f\u5982\u4f55\u89e3\u51b3\u6b64\u95ee\u9898\u7684\u793a\u4f8b\uff0c\u8bf7\u6ce8\u610f\uff0c\u5b83\u4e0d\u5b8c\u6574\uff0c\u56e0\u6b64\u4e0d\u5e94\u6309\u539f\u6837\u4f7f\u7528\uff1a<\/p>\n<pre>\/\/ declare a structure that matches the openapi's yaml document\ntype datatype struct {\n    type  string               `yaml:\"type,omitempty\"`\n    props map[string]*datatype `yaml:\"properties,omitempty\"`\n    xml   *xml                 `yaml:\"xml,omitempty\"`\n}\ntype xml struct {\n    name string `yaml:\"name,omitempty\"`\n    attr bool   `yaml:\"attribute,omitempty\"`\n}\n<\/pre>\n<pre>\/\/ write a marshal func that converts a given type to the structure declared above\n\/\/ - this example converts only plain structs\nfunc marshalopenapi(v interface{}) (interface{}, error) {\n    rt := reflect.typeof(v)\n    if rt.kind() == reflect.struct {\n        obj := datatype{type: \"object\"}\n\n        for i := 0; i &lt; rt.numfield(); i++ {\n            f := rt.field(i)\n            tag := strings.split(f.tag.get(\"xml\"), \",\")\n            if len(tag) == 0 || len(tag[0]) == 0 { \/\/ no name?\n                continue\n            }\n            if obj.props == nil {\n                obj.props = make(map[string]*datatype)\n            }\n\n            name := tag[0]\n            obj.props[name] = &amp;datatype{\n                type: gotypetoopenapitype(f.type),\n            }\n            if len(tag) &gt; 1 &amp;&amp; tag[1] == \"attr\" {\n                obj.props[name].xml = &amp;xml{attr: true}\n            }\n        }\n\n        return obj, nil\n    }\n    return nil, fmt.errorf(\"unsupported type\")\n}\n<\/pre>\n<pre>\/\/ have all your types implement the yaml.marshaler interface by\n\/\/ delegating to the marshal func implemented above\nfunc (e error) marshalyaml() (interface{}, error) {\n    return marshalopenapi(e)\n}\n<\/pre>\n<pre>\/\/ marshal the types\nyaml.Marshal(map[string]map[string]interface{}{\n    \"schemas\": {\n        \"error\": Error{},\n        \/\/ ...\n    },\n})\n<\/pre>\n<p>\u62e8\u6253 \u8bd5\u8bd5\u3002<\/p>\n<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300a\u4ece Go \u7ed3\u6784\u751f\u6210 OpenAPI XML \u6a21\u578b\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03Golang\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5f53\u524d\u4f4d\u7f6e\uff1a &gt; &gt; &#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-207782","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207782","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=207782"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/207782\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=207782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=207782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=207782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}