openapi3.Unmarshal
语法
openapi3.Unmarshal RESOURCE
返回
openapi3.OpenAPIDocument
将 openapi3.Unmarshal
函数与全局、页面或远程资源一起使用。
例如,要使用远程 OpenAPI 定义
{{ $url := "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.json" }}
{{ $api := "" }}
{{ with try (resources.GetRemote $url) }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else with .Value }}
{{ $api = . | openapi3.Unmarshal }}
{{ else }}
{{ errorf "Unable to get remote resource %q" $url }}
{{ end }}
{{ end }}
检查数据结构
<pre>{{ debug.Dump $api }}</pre>
列出每个 API 路径的 GET 和 POST 操作
{{ range $path, $details := $api.Paths }}
<p>{{ $path }}</p>
<dl>
{{ with $details.Get }}
<dt>GET</dt>
<dd>{{ .Summary }}</dd>
{{ end }}
{{ with $details.Post }}
<dt>POST</dt>
<dd>{{ .Summary }}</dd>
{{ end }}
</dl>
{{ end }}
Hugo 将此渲染为
<p>/pets</p>
<dl>
<dt>GET</dt>
<dd>List all pets</dd>
<dt>POST</dt>
<dd>Create a pet</dd>
</dl>
<p>/pets/{petId}</p>
<dl>
<dt>GET</dt>
<dd>Info for a specific pet</dd>
</dl>