openapi3.Unmarshal
使用 openapi3.Unmarshal
函數處理 global、page 或 remote 資源。
例如,處理遠端的 OpenAPI 定義:
{{ $url := "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.json" }}
{{ $api := "" }}
{{ with resources.GetRemote $url }}
{{ with .Err }}
{{ errorf "%s" . }}
{{ else }}
{{ $api = . | openapi3.Unmarshal }}
{{ end }}
{{ else }}
{{ errorf "無法取得遠端資源 %q" $url }}
{{ 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>列出所有寵物</dd>
<dt>POST</dt>
<dd>創建一隻寵物</dd>
</dl>
<p>/pets/{petId}</p>
<dl>
<dt>GET</dt>
<dd>查詢特定寵物資訊</dd>
</dl>