pickExample returns a copy of the example for a response body, preferring the one named default.
(media *openapi3.MediaType)
| 95 | // pickExample returns a copy of the example for a response body, preferring |
| 96 | // the one named default. |
| 97 | func pickExample(media *openapi3.MediaType) (any, bool) { |
| 98 | value := media.Example |
| 99 | if value == nil && len(media.Examples) > 0 { |
| 100 | name := "default" |
| 101 | if media.Examples[name] == nil { |
| 102 | name = slices.Sorted(maps.Keys(media.Examples))[0] |
| 103 | } |
| 104 | if ex := media.Examples[name]; ex != nil && ex.Value != nil { |
| 105 | value = ex.Value.Value |
| 106 | } |
| 107 | } |
| 108 | if value == nil { |
| 109 | return nil, false |
| 110 | } |
| 111 | // The filler changes values in place, and the example belongs to the |
| 112 | // loaded description. |
| 113 | return cloneJSON(value), true |
| 114 | } |
| 115 | |
| 116 | // restFiller makes a JSON value match a REST schema. It adds required |
| 117 | // properties that are missing and replaces values of the wrong type, and |