Unmarshal.md - hugo - [fork] hugo port for 9front
(HTM) git clone https://git.drkhsh.at/hugo.git
(DIR) Log
(DIR) Files
(DIR) Refs
(DIR) Submodules
(DIR) README
(DIR) LICENSE
---
Unmarshal.md (2233B)
---
1 ---
2 title: openapi3.Unmarshal
3 description: Unmarshals the given resource into an OpenAPI 3 document.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: openapi3.OpenAPIDocument
10 signatures: ['openapi3.Unmarshal RESOURCE']
11 ---
12
13 Use the `openapi3.Unmarshal` function with [global resources](g), [page resources](g), or [remote resources](g).
14
15 [OpenAPI]: https://www.openapis.org/
16
17 For example, to work with a remote [OpenAPI] definition:
18
19 ```go-html-template
20 {{ $url := "https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore.json" }}
21 {{ $api := "" }}
22 {{ with try (resources.GetRemote $url) }}
23 {{ with .Err }}
24 {{ errorf "%s" . }}
25 {{ else with .Value }}
26 {{ $api = . | openapi3.Unmarshal }}
27 {{ else }}
28 {{ errorf "Unable to get remote resource %q" $url }}
29 {{ end }}
30 {{ end }}
31 ```
32
33 To inspect the data structure:
34
35 ```go-html-template
36 <pre>{{ debug.Dump $api }}</pre>
37 ```
38
39 To list the GET and POST operations for each of the API paths:
40
41 ```go-html-template
42 {{ range $path, $details := $api.Paths.Map }}
43 <p>{{ $path }}</p>
44 <dl>
45 {{ with $details.Get }}
46 <dt>GET</dt>
47 <dd>{{ .Summary }}</dd>
48 {{ end }}
49 {{ with $details.Post }}
50 <dt>POST</dt>
51 <dd>{{ .Summary }}</dd>
52 {{ end }}
53 </dl>
54 {{ end }}
55 ```
56
57 > [!warning]
58 > The unmarshaled data structure is created with [`kin-openapi`](https://github.com/getkin/kin-openapi). Many fields are structs or pointers (not maps), and therefore require accessors or other methods for indexing and iteration.
59 > For example, prior to [`kin-openapi` v0.122.0](https://github.com/getkin/kin-openapi#v01220) / [Hugo v0.121.0](https://github.com/gohugoio/hugo/releases/tag/v0.121.0), `Paths` was a map (so `.Paths` was iterable) and it is now a pointer (and requires the `.Paths.Map` accessor, as in the example above).
60 > See the [`kin-openapi` godoc for OpenAPI 3](https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3) for full type definitions.
61
62 Hugo renders this to:
63
64 ```html
65 <p>/pets</p>
66 <dl>
67 <dt>GET</dt>
68 <dd>List all pets</dd>
69 <dt>POST</dt>
70 <dd>Create a pet</dd>
71 </dl>
72 <p>/pets/{petId}</p>
73 <dl>
74 <dt>GET</dt>
75 <dd>Info for a specific pet</dd>
76 </dl>
77 ```