Debian Codegraph @

docs/content/en/functions/openapi3/Unmarshal.md

raw
1 ---
2 title: openapi3.Unmarshal
3 description: Returns an OpenAPI 3 Description unmarshaled from the given resource.
4 categories: []
5 keywords: []
6 params:
7 functions_and_methods:
8 aliases: []
9 returnType: openapi3.OpenAPIDocument
10 signatures: ['openapi3.Unmarshal RESOURCE [OPTIONS]']
11 ---
12
13 ## Usage
14
15 The resource passed to the `openapi3.Unmarshal` function must be an [OpenAPI Document][], typically in JSON or YAML format. This resource can be a [global resource](g) or a [remote resource](g).
16
17 This function automatically resolves and includes all external references, both local and remote, and returns a complete [OpenAPI Description][] that fully describes the surface of an API and its semantics.
18
19 The `openapi3.Unmarshal` function accepts an options map.
20
21 `getremote`
22 : {{< new-in 0.153.0 />}}
23 : (`map`) This is a map of the options for the [`resources.GetRemote`][] function, useful when an OpenAPI Document includes remote external references.
24
25 ## Examples
26
27 The following examples demonstrate how to unmarshal remote and global resources, and how to inspect the result.
28
29 ### Remote resource
30
31 To work with a remote resource:
32
33 ```go-html-template {copy=true}
34 {{ $api := "" }}
35 {{ $url := "https://petstore.swagger.io/v2/swagger.json" }}
36 {{ $opts := dict
37 "headers" (dict "Authorization" "Bearer abcd")
38 }}
39 {{ with try (resources.GetRemote $url $opts) }}
40 {{ with .Err }}
41 {{ errorf "%s" . }}
42 {{ else with .Value }}
43 {{ $api = openapi3.Unmarshal . (dict "getremote" $opts) }}
44 {{ else }}
45 {{ errorf "Unable to get remote resource %q" $url }}
46 {{ end }}
47 {{ end }}
48 ```
49
50 In the example above, the same HTTP Authorization header is used for both the initial remote request made by the `resources.GetRemote` function and for subsequent requests by the `openapi.Unmarshal` function as it retrieve remote external references.
51
52 ### Global resource
53
54 To work with a global resource:
55
56 ```go-html-template {copy=true}
57 {{ $api := "" }}
58 {{ $opts := dict
59 "method" "post"
60 "key" now.UnixNano
61 }}
62 {{ with resources.Get "api/petstore.json" }}
63 {{ $api = openapi3.Unmarshal . (dict "getremote" $opts) }}
64 {{ end }}
65 ```
66
67 For global resources, local external reference paths starting with `/` are resolved relative to the `assets` directory. All other local paths are resolved relative to the entry point. In the example above, local paths are resolved relative to `assets/api/petstore.json`.
68
69 ### Inspection
70
71 > [!NOTE]
72 > The unmarshaled data structure is created with [`kin-openapi`][]. Many fields are structs or pointers (not maps), and therefore require accessors or other methods for indexing and iteration.
73 >
74 > For example, `Paths` is a pointer rather than a map; to iterate over the API paths, you must use the `.Paths.Map` accessor as shown in the example below.
75 >
76 > See the [`kin-openapi` godoc for OpenAPI 3][] for full type definitions.
77
78 To inspect the unmarshaled data structure:
79
80 ```go-html-template {copy=true}
81 <pre>{{ debug.Dump $api }}</pre>
82 ```
83
84 To list the GET and POST operations for each of the API paths:
85
86 ```go-html-template {copy=true}
87 {{ range $path, $details := $api.Paths.Map }}
88 <p>{{ $path }}</p>
89 <dl>
90 {{ with $details.Get }}
91 <dt>GET</dt>
92 <dd>{{ .Summary }}</dd>
93 {{ end }}
94 {{ with $details.Post }}
95 <dt>POST</dt>
96 <dd>{{ .Summary }}</dd>
97 {{ end }}
98 </dl>
99 {{ end }}
100 ```
101
102 Hugo renders this to:
103
104 ```html
105 <p>/pets</p>
106 <dl>
107 <dt>GET</dt>
108 <dd>List all pets</dd>
109 <dt>POST</dt>
110 <dd>Create a pet</dd>
111 </dl>
112 <p>/pets/{petId}</p>
113 <dl>
114 <dt>GET</dt>
115 <dd>Info for a specific pet</dd>
116 </dl>
117 ```
118
119 [OpenAPI Description]: https://swagger.io/specification/#openapi-description
120 [OpenAPI Document]: https://swagger.io/specification/#openapi-document
121 [`kin-openapi` godoc for OpenAPI 3]: https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3
122 [`kin-openapi`]: https://github.com/getkin/kin-openapi
123 [`resources.GetRemote`]: /functions/resources/getremote/#options