|
| 1 | +openapi: 3.0.0 |
| 2 | +info: |
| 3 | + title: x-codeSamples Variations API |
| 4 | + version: 1.0.0 |
| 5 | + description: | |
| 6 | + Exercises every supported shape of the `x-codeSamples` vendor extension |
| 7 | + so each path can be inspected in isolation in the demo. |
| 8 | +
|
| 9 | + Tracks: |
| 10 | + - https://github.com/PaloAltoNetworks/docusaurus-openapi-docs/issues/1204 |
| 11 | + - https://github.com/PaloAltoNetworks/docusaurus-openapi-docs/issues/1036 |
| 12 | +
|
| 13 | +servers: |
| 14 | + - url: https://api.example.com/v1 |
| 15 | + description: Example API server |
| 16 | + |
| 17 | +tags: |
| 18 | + - name: codeSamples |
| 19 | + description: x-codeSamples tests |
| 20 | + |
| 21 | +paths: |
| 22 | + /no-samples: |
| 23 | + get: |
| 24 | + tags: |
| 25 | + - codeSamples |
| 26 | + summary: Control — no x-codeSamples |
| 27 | + description: | |
| 28 | + Sanity check. Only the Postman-generated language tabs should render; |
| 29 | + no inner sample tabs. |
| 30 | + responses: |
| 31 | + "200": |
| 32 | + description: OK |
| 33 | + |
| 34 | + /single-sample-no-label: |
| 35 | + get: |
| 36 | + tags: |
| 37 | + - codeSamples |
| 38 | + summary: Single sample, no label |
| 39 | + description: | |
| 40 | + One C# sample with no `label`. Inner tab id falls back to indexed |
| 41 | + form (`C#-0`) and the visible tab label is the bare language name. |
| 42 | + responses: |
| 43 | + "200": |
| 44 | + description: OK |
| 45 | + x-codeSamples: |
| 46 | + - lang: "C#" |
| 47 | + source: | |
| 48 | + using System.Net.Http; |
| 49 | + var client = new HttpClient(); |
| 50 | + var response = await client.GetAsync("https://api.example.com/v1/single-sample-no-label"); |
| 51 | +
|
| 52 | + /single-sample-with-label: |
| 53 | + get: |
| 54 | + tags: |
| 55 | + - codeSamples |
| 56 | + summary: Single sample, with label |
| 57 | + description: | |
| 58 | + One PHP sample with `label: Custom`. Inner tab id is `PHP-Custom` |
| 59 | + and the tab displays "Custom". |
| 60 | + responses: |
| 61 | + "200": |
| 62 | + description: OK |
| 63 | + x-codeSamples: |
| 64 | + - lang: PHP |
| 65 | + label: Custom |
| 66 | + source: | |
| 67 | + <?php |
| 68 | + $client = new \GuzzleHttp\Client(); |
| 69 | + $response = $client->get('https://api.example.com/v1/single-sample-with-label'); |
| 70 | +
|
| 71 | + /multiple-samples-with-labels: |
| 72 | + post: |
| 73 | + tags: |
| 74 | + - codeSamples |
| 75 | + summary: Multiple samples per language with distinct labels (#1204) |
| 76 | + description: | |
| 77 | + Three Python samples covering three auth flows. Before the fix this |
| 78 | + crashed with `Duplicate values "Python, Python" found in <Tabs>`. |
| 79 | + After the fix each renders as its own inner tab labeled by `label`. |
| 80 | + responses: |
| 81 | + "200": |
| 82 | + description: OK |
| 83 | + requestBody: |
| 84 | + content: |
| 85 | + application/json: |
| 86 | + schema: |
| 87 | + type: object |
| 88 | + properties: |
| 89 | + name: |
| 90 | + type: string |
| 91 | + x-codeSamples: |
| 92 | + - lang: Python |
| 93 | + label: KeyPair Auth |
| 94 | + source: | |
| 95 | + import requests |
| 96 | + from cryptography.hazmat.primitives.serialization import load_pem_private_key |
| 97 | +
|
| 98 | + with open("private_key.pem", "rb") as f: |
| 99 | + key = load_pem_private_key(f.read(), password=None) |
| 100 | + token = sign_jwt(key) |
| 101 | + requests.post( |
| 102 | + "https://api.example.com/v1/multiple-samples-with-labels", |
| 103 | + headers={"Authorization": f"Bearer {token}"}, |
| 104 | + json={"name": "rex"}, |
| 105 | + ) |
| 106 | + - lang: Python |
| 107 | + label: Basic Auth |
| 108 | + source: | |
| 109 | + import requests |
| 110 | + from requests.auth import HTTPBasicAuth |
| 111 | +
|
| 112 | + requests.post( |
| 113 | + "https://api.example.com/v1/multiple-samples-with-labels", |
| 114 | + auth=HTTPBasicAuth("user", "password"), |
| 115 | + json={"name": "rex"}, |
| 116 | + ) |
| 117 | + - lang: Python |
| 118 | + label: OAuth |
| 119 | + source: | |
| 120 | + from requests_oauthlib import OAuth2Session |
| 121 | +
|
| 122 | + oauth = OAuth2Session(client_id, token=token) |
| 123 | + oauth.post( |
| 124 | + "https://api.example.com/v1/multiple-samples-with-labels", |
| 125 | + json={"name": "rex"}, |
| 126 | + ) |
| 127 | +
|
| 128 | + /multiple-samples-no-labels: |
| 129 | + post: |
| 130 | + tags: |
| 131 | + - codeSamples |
| 132 | + summary: Multiple samples per language, no labels |
| 133 | + description: | |
| 134 | + Two PowerShell samples without `label`. Inner tab ids fall back to |
| 135 | + indexed form (`PowerShell-0`, `PowerShell-1`) so the page renders |
| 136 | + without crashing — but both tabs display the bare language name, |
| 137 | + which makes them visually identical. Adding `label` is recommended. |
| 138 | + responses: |
| 139 | + "200": |
| 140 | + description: OK |
| 141 | + x-codeSamples: |
| 142 | + - lang: PowerShell |
| 143 | + source: | |
| 144 | + $body = @{ name = "rex" } | ConvertTo-Json |
| 145 | + Invoke-RestMethod -Uri https://api.example.com/v1/multiple-samples-no-labels -Method Post -Body $body -ContentType application/json |
| 146 | + - lang: PowerShell |
| 147 | + source: | |
| 148 | + $headers = @{ Authorization = "Bearer $token" } |
| 149 | + $body = @{ name = "rex" } | ConvertTo-Json |
| 150 | + Invoke-RestMethod -Uri https://api.example.com/v1/multiple-samples-no-labels -Method Post -Headers $headers -Body $body -ContentType application/json |
| 151 | +
|
| 152 | + /duplicate-label-collision: |
| 153 | + post: |
| 154 | + tags: |
| 155 | + - codeSamples |
| 156 | + summary: Duplicate lang+label (defensive collision suffix) |
| 157 | + description: | |
| 158 | + Two Java samples sharing both `lang: Java` and `label: Auth`. This |
| 159 | + is an author bug, but the page still renders — a numeric suffix is |
| 160 | + appended internally to keep the inner tab ids unique. The visible |
| 161 | + labels remain identical and the author should fix the spec. |
| 162 | + responses: |
| 163 | + "200": |
| 164 | + description: OK |
| 165 | + x-codeSamples: |
| 166 | + - lang: Java |
| 167 | + label: Auth |
| 168 | + source: | |
| 169 | + // OkHttp + bearer token |
| 170 | + Request request = new Request.Builder() |
| 171 | + .url("https://api.example.com/v1/duplicate-label-collision") |
| 172 | + .header("Authorization", "Bearer " + token) |
| 173 | + .post(RequestBody.create("{}", JSON)) |
| 174 | + .build(); |
| 175 | + - lang: Java |
| 176 | + label: Auth |
| 177 | + source: | |
| 178 | + // HttpClient (JDK 11+) with basic auth |
| 179 | + HttpRequest request = HttpRequest.newBuilder() |
| 180 | + .uri(URI.create("https://api.example.com/v1/duplicate-label-collision")) |
| 181 | + .header("Authorization", "Basic " + encoded) |
| 182 | + .POST(BodyPublishers.ofString("{}")) |
| 183 | + .build(); |
| 184 | +
|
| 185 | + /mixed-languages: |
| 186 | + post: |
| 187 | + tags: |
| 188 | + - codeSamples |
| 189 | + summary: Mixed languages, some with multiple samples |
| 190 | + description: | |
| 191 | + Realistic mix: two Python entries (with labels), one Ruby (no |
| 192 | + label), one Go (with label). Exercises the case where switching |
| 193 | + between outer language tabs must not leak `selectedSample` from |
| 194 | + another language. |
| 195 | + responses: |
| 196 | + "200": |
| 197 | + description: OK |
| 198 | + x-codeSamples: |
| 199 | + - lang: Python |
| 200 | + label: requests |
| 201 | + source: | |
| 202 | + import requests |
| 203 | + requests.post("https://api.example.com/v1/mixed-languages") |
| 204 | + - lang: Python |
| 205 | + label: httpx |
| 206 | + source: | |
| 207 | + import httpx |
| 208 | + httpx.post("https://api.example.com/v1/mixed-languages") |
| 209 | + - lang: Ruby |
| 210 | + source: | |
| 211 | + require "net/http" |
| 212 | + Net::HTTP.post(URI("https://api.example.com/v1/mixed-languages"), "") |
| 213 | + - lang: Go |
| 214 | + label: net/http |
| 215 | + source: | |
| 216 | + package main |
| 217 | +
|
| 218 | + import ( |
| 219 | + "net/http" |
| 220 | + "strings" |
| 221 | + ) |
| 222 | +
|
| 223 | + func main() { |
| 224 | + http.Post("https://api.example.com/v1/mixed-languages", "application/json", strings.NewReader("")) |
| 225 | + } |
| 226 | +
|
| 227 | + /unmatched-language: |
| 228 | + get: |
| 229 | + tags: |
| 230 | + - codeSamples |
| 231 | + summary: Sample for a language not in languageTabs |
| 232 | + description: | |
| 233 | + A Dart sample is provided, but `Dart` is typically not in the |
| 234 | + default `languageTabs`. The sample is silently dropped (existing |
| 235 | + behavior — unchanged by this fix). Only the Postman-generated tabs |
| 236 | + for configured languages should appear. |
| 237 | + responses: |
| 238 | + "200": |
| 239 | + description: OK |
| 240 | + x-codeSamples: |
| 241 | + - lang: Dart |
| 242 | + label: http |
| 243 | + source: | |
| 244 | + import 'package:http/http.dart' as http; |
| 245 | + await http.get(Uri.parse('https://api.example.com/v1/unmatched-language')); |
0 commit comments