mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-14 20:37:55 -05:00
1.6 KiB
1.6 KiB
title | description | categories | keywords | action | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
openapi3.Unmarshal | Unmarshals the given resource into an OpenAPI 3 document. |
|
Use the openapi3.Unmarshal
function with global, page, or remote resources.
For example, to work with a remote OpenAPI defintion:
{{ $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 "Unable to get remote resource %q" $url }}
{{ end }}
To inspect the data structure:
<pre>{{ debug.Dump $api }}</pre>
To list the GET and POST operations for each of the API paths:
{{ 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 renders this to:
<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>