| 1 |
--- |
| 2 |
title: openapi3.Unmarshal |
| 3 |
description: Unmarshals the given resource into an OpenAPI 3 Description. |
| 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 |
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). |
| 14 |
|
| 15 |
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. |
| 16 |
|
| 17 |
## Options |
| 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 |
### Remote resource |
| 28 |
|
| 29 |
To work with a remote resource: |
| 30 |
|
| 31 |
```go-html-template {copy=true} |
| 32 |
{{ $api := "" }} |
| 33 |
{{ $url := "https://petstore.swagger.io/v2/swagger.json" }} |
| 34 |
{{ $opts := dict |
| 35 |
"headers" (dict "Authorization" "Bearer abcd") |
| 36 |
}} |
| 37 |
{{ with try (resources.GetRemote $url $opts) }} |
| 38 |
{{ with .Err }} |
| 39 |
{{ errorf "%s" . }} |
| 40 |
{{ else with .Value }} |
| 41 |
{{ $api = openapi3.Unmarshal . (dict "getremote" $opts) }} |
| 42 |
{{ else }} |
| 43 |
{{ errorf "Unable to get remote resource %q" $url }} |
| 44 |
{{ end }} |
| 45 |
{{ end }} |
| 46 |
``` |
| 47 |
|
| 48 |
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. |
| 49 |
|
| 50 |
### Global resource |
| 51 |
|
| 52 |
To work with a global resource: |
| 53 |
|
| 54 |
```go-html-template {copy=true} |
| 55 |
{{ $api := "" }} |
| 56 |
{{ $opts := dict |
| 57 |
"method" "post" |
| 58 |
"key" now.UnixNano |
| 59 |
}} |
| 60 |
{{ with resources.Get "api/petstore.json" }} |
| 61 |
{{ $api = openapi3.Unmarshal . (dict "getremote" $opts) }} |
| 62 |
{{ end }} |
| 63 |
``` |
| 64 |
|
| 65 |
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`. |
| 66 |
|
| 67 |
## Inspection |
| 68 |
|
| 69 |
> [!NOTE] |
| 70 |
> 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. |
| 71 |
> |
| 72 |
> 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. |
| 73 |
> |
| 74 |
> See the [`kin-openapi` godoc for OpenAPI 3][] for full type definitions. |
| 75 |
|
| 76 |
To inspect the unmarshaled data structure: |
| 77 |
|
| 78 |
```go-html-template {copy=true} |
| 79 |
<pre>{{ debug.Dump $api }}</pre> |
| 80 |
``` |
| 81 |
|
| 82 |
To list the GET and POST operations for each of the API paths: |
| 83 |
|
| 84 |
```go-html-template {copy=true} |
| 85 |
{{ range $path, $details := $api.Paths.Map }} |
| 86 |
<p>{{ $path }}</p> |
| 87 |
<dl> |
| 88 |
{{ with $details.Get }} |
| 89 |
<dt>GET</dt> |
| 90 |
<dd>{{ .Summary }}</dd> |
| 91 |
{{ end }} |
| 92 |
{{ with $details.Post }} |
| 93 |
<dt>POST</dt> |
| 94 |
<dd>{{ .Summary }}</dd> |
| 95 |
{{ end }} |
| 96 |
</dl> |
| 97 |
{{ end }} |
| 98 |
``` |
| 99 |
|
| 100 |
Hugo renders this to: |
| 101 |
|
| 102 |
```html |
| 103 |
<p>/pets</p> |
| 104 |
<dl> |
| 105 |
<dt>GET</dt> |
| 106 |
<dd>List all pets</dd> |
| 107 |
<dt>POST</dt> |
| 108 |
<dd>Create a pet</dd> |
| 109 |
</dl> |
| 110 |
<p>/pets/{petId}</p> |
| 111 |
<dl> |
| 112 |
<dt>GET</dt> |
| 113 |
<dd>Info for a specific pet</dd> |
| 114 |
</dl> |
| 115 |
``` |
| 116 |
|
| 117 |
[OpenAPI Description]: https://swagger.io/specification/#openapi-description |
| 118 |
[OpenAPI Document]: https://swagger.io/specification/#openapi-document |
| 119 |
[`kin-openapi` godoc for OpenAPI 3]: https://pkg.go.dev/github.com/getkin/kin-openapi/openapi3 |
| 120 |
[`kin-openapi`]: https://github.com/getkin/kin-openapi |
| 121 |
[`resources.GetRemote`]: /functions/resources/getremote/#options |