Skip to content

Commit a5304b4

Browse files
authored
Merge branch 'main' into cx-3611-render-authorization-without-security
2 parents cb98654 + 1f3cf1d commit a5304b4

10 files changed

Lines changed: 613 additions & 143 deletions

File tree

.changeset/early-pens-dream.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
'oas': minor
3+
'@readme/oas-to-har': minor
4+
'@readme/oas-examples': patch
5+
---
6+
7+
Add operation-aware server helpers that resolve OpenAPI servers from operation, path-item, and root-level definitions.
8+
9+
Update HAR generation to construct request URLs from operation-aware servers.
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
{
2+
"openapi": "3.0.3",
3+
"info": {
4+
"title": "Path Level server declarations",
5+
"description": "https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#serverVariableObject",
6+
"version": "1.0.0"
7+
},
8+
"servers": [
9+
{
10+
"url": "https://{name}.example.com:{port}/{basePath}",
11+
"variables": {
12+
"name": {
13+
"default": "demo"
14+
},
15+
"port": {
16+
"default": "443"
17+
},
18+
"basePath": {
19+
"default": "v2"
20+
}
21+
}
22+
},
23+
{
24+
"url": "http://{name}.local/{basePath}",
25+
"variables": {
26+
"name": {
27+
"default": "demo"
28+
},
29+
"basePath": {
30+
"default": "v1"
31+
}
32+
}
33+
},
34+
{
35+
"url": "https://{name}.readme.io:{port}/{basePath}",
36+
"variables": {
37+
"name": {
38+
"default": "demo"
39+
},
40+
"port": {
41+
"default": "3000",
42+
"enum": ["3000", "5000"]
43+
},
44+
"basePath": {
45+
"description": "path description",
46+
"default": "v1"
47+
}
48+
}
49+
}
50+
],
51+
"paths": {
52+
"/relative-path-server": {
53+
"servers": [{ "url": "/v2" }],
54+
"get": {
55+
"summary": "Relative path-level server",
56+
"tags": ["Path"],
57+
"responses": {
58+
"200": {
59+
"description": "OK"
60+
}
61+
}
62+
}
63+
},
64+
"/relative-operation-server": {
65+
"get": {
66+
"summary": "Relative operation-level server",
67+
"tags": ["Operation"],
68+
"servers": [{ "url": "/v3" }],
69+
"responses": {
70+
"200": {
71+
"description": "OK"
72+
}
73+
}
74+
}
75+
},
76+
"/operation-server-variables": {
77+
"get": {
78+
"summary": "Operation-level server variables",
79+
"tags": ["Operation"],
80+
"servers": [
81+
{
82+
"url": "https://operation.example.com/{version}",
83+
"variables": {
84+
"version": {
85+
"default": "v3"
86+
}
87+
}
88+
}
89+
],
90+
"responses": {
91+
"200": {
92+
"description": "OK"
93+
}
94+
}
95+
}
96+
},
97+
"/path-item-ref-server": {
98+
"$ref": "#/paths/~1path-item-server-source"
99+
},
100+
"/path-item-server-source": {
101+
"servers": [{ "url": "https://path-item-ref.example.com" }],
102+
"get": {
103+
"summary": "Path item ref server source",
104+
"tags": ["Path"],
105+
"responses": {
106+
"200": {
107+
"description": "OK"
108+
}
109+
}
110+
}
111+
},
112+
"/empty-operation-servers": {
113+
"servers": [{ "url": "https://empty-operation-path.example.com" }],
114+
"get": {
115+
"summary": "Empty operation-level servers",
116+
"tags": ["Operation"],
117+
"servers": [],
118+
"responses": {
119+
"200": {
120+
"description": "OK"
121+
}
122+
}
123+
}
124+
},
125+
"/empty-path-item-servers": {
126+
"servers": [],
127+
"get": {
128+
"summary": "Empty path-level servers",
129+
"tags": ["Path"],
130+
"responses": {
131+
"200": {
132+
"description": "OK"
133+
}
134+
}
135+
}
136+
}
137+
}
138+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
openapi: 3.0.3
3+
info:
4+
title: Path Level server declarations
5+
description: https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.3.md#serverVariableObject
6+
version: 1.0.0
7+
servers:
8+
- url: https://{name}.example.com:{port}/{basePath}
9+
variables:
10+
name:
11+
default: demo
12+
port:
13+
default: '443'
14+
basePath:
15+
default: v2
16+
- url: http://{name}.local/{basePath}
17+
variables:
18+
name:
19+
default: demo
20+
basePath:
21+
default: v1
22+
- url: https://{name}.readme.io:{port}/{basePath}
23+
variables:
24+
name:
25+
default: demo
26+
port:
27+
default: '3000'
28+
enum:
29+
- '3000'
30+
- '5000'
31+
basePath:
32+
description: path description
33+
default: v1
34+
paths:
35+
'/relative-path-server':
36+
servers:
37+
- url: '/v2'
38+
get:
39+
summary: Relative path-level server
40+
tags:
41+
- Path
42+
responses:
43+
'200':
44+
description: OK
45+
'/relative-operation-server':
46+
get:
47+
summary: Relative operation-level server
48+
tags:
49+
- Operation
50+
servers:
51+
- url: '/v3'
52+
responses:
53+
'200':
54+
description: OK
55+
'/operation-server-variables':
56+
get:
57+
summary: Operation-level server variables
58+
tags:
59+
- Operation
60+
servers:
61+
- url: https://operation.example.com/{version}
62+
variables:
63+
version:
64+
default: v3
65+
responses:
66+
'200':
67+
description: OK
68+
'/path-item-ref-server':
69+
'$ref': '#/paths/~1path-item-server-source'
70+
'/path-item-server-source':
71+
servers:
72+
- url: https://path-item-ref.example.com
73+
get:
74+
summary: Path item ref server source
75+
tags:
76+
- Path
77+
responses:
78+
'200':
79+
description: OK
80+
'/empty-operation-servers':
81+
servers:
82+
- url: https://empty-operation-path.example.com
83+
get:
84+
summary: Empty operation-level servers
85+
tags:
86+
- Operation
87+
servers: []
88+
responses:
89+
'200':
90+
description: OK
91+
'/empty-path-item-servers':
92+
servers: []
93+
get:
94+
summary: Empty path-level servers
95+
tags:
96+
- Path
97+
responses:
98+
'200':
99+
description: OK

packages/oas-to-har/src/index.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { AuthForHAR, DataForHAR, oasToHarOptions } from './lib/types.js';
22
import type { PostData, PostDataParams, Request } from 'har-format';
3+
import type Oas from 'oas';
34
import type { Extensions } from 'oas/extensions';
45
import type {
56
HttpMethods,
67
JSONSchema,
78
MediaTypeObject,
8-
OASDocument,
99
OperationObject,
1010
ParameterObject,
1111
SchemaObject,
@@ -14,7 +14,6 @@ import type {
1414
} from 'oas/types';
1515

1616
import { parse as parseDataUrl } from '@readme/data-urls';
17-
import Oas from 'oas';
1817
import { HEADERS, PROXY_ENABLED } from 'oas/extensions';
1918
import { Operation } from 'oas/operation';
2019
import { isRef } from 'oas/types';
@@ -274,9 +273,8 @@ export default function oasToHar(
274273
*
275274
* It's weird. This is easier.
276275
*/
277-
const currentOas = Oas.init(oas as unknown as OASDocument);
278276
operation = new Operation(
279-
currentOas,
277+
oas,
280278
operationSchema?.path || '',
281279
operationSchema?.method || ('' as HttpMethods),
282280
(operationSchema as unknown as OperationObject) || { path: '', method: '' },
@@ -296,13 +294,13 @@ export default function oasToHar(
296294
if (!formData.server) {
297295
formData.server = {
298296
selected: 0,
299-
variables: oas.defaultVariables(0),
297+
variables: operation.defaultVariables(0),
300298
};
301299
}
302300

303301
// If the incoming `server.variables` is missing variables let's pad it out with defaults.
304302
formData.server.variables = {
305-
...oas.defaultVariables(formData.server.selected),
303+
...operation.defaultVariables(formData.server.selected),
306304
...(formData.server.variables ? formData.server.variables : {}),
307305
};
308306

@@ -315,7 +313,7 @@ export default function oasToHar(
315313
postData: {},
316314
bodySize: 0,
317315
method: operation.method.toUpperCase(),
318-
url: `${oas.url(formData.server.selected, formData.server.variables as ServerVariable)}${operation.path}`.replace(
316+
url: `${operation.url(formData.server.selected, formData.server.variables as ServerVariable)}${operation.path}`.replace(
319317
/\s/g,
320318
'%20',
321319
),

packages/oas-to-har/test/__datasets__/server-variables.json

Lines changed: 0 additions & 46 deletions
This file was deleted.

0 commit comments

Comments
 (0)