Skip to content

Commit ddd80db

Browse files
authored
fix(resolver): add relative reference support for baseDoc option (#2905)
This change is specific to OpenAPI 3.1.0 resolver strategy. Refs swagger-api/swagger-ui#8525
1 parent 138e982 commit ddd80db

File tree

7 files changed

+339
-7
lines changed

7 files changed

+339
-7
lines changed

src/resolver/strategies/openapi-3-1-apidom/resolve.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ const resolveOpenAPI31Strategy = async (options) => {
4242
} = options;
4343
try {
4444
// determining BaseURI
45-
const defaultBaseURI = 'https://smartbear.com/';
46-
const retrievalURI = optionsUtil.retrievalURI(options) ?? url.cwd();
47-
const baseURI = url.isHttpUrl(retrievalURI) ? retrievalURI : defaultBaseURI;
45+
const cwd = url.isHttpUrl(url.cwd()) ? url.cwd() : 'https://smartbear.com/';
46+
const retrievalURI = optionsUtil.retrievalURI(options);
47+
const baseURI = url.resolve(cwd, retrievalURI);
4848

4949
// prepare spec for dereferencing
5050
const openApiElement = OpenApi3_1Element.refract(spec);

src/resolver/utils/options.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ export const retrievalURI = (options) => {
55

66
// @TODO Swagger-UI uses baseDoc instead of url, this is to allow both
77
// need to fix and pick one.
8-
return baseDoc || url;
8+
return baseDoc || url || '';
99
};
1010

1111
export const httpClient = (options) => {

test/resolver/strategies/openapi-3-1-apidom/__snapshots__/index.js.snap

Lines changed: 317 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -990,6 +990,315 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via URL s
990990
}
991991
`;
992992

993+
exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec option and baseDoc option is provided and baseDoc option is a relative reference should resolve the definition with the resolved baseDoc 1`] = `
994+
{
995+
"errors": [],
996+
"spec": {
997+
"$$normalized": true,
998+
"components": {
999+
"schemas": {
1000+
"Error": {
1001+
"properties": {
1002+
"code": {
1003+
"format": "int32",
1004+
"type": "integer",
1005+
},
1006+
"message": {
1007+
"type": "string",
1008+
},
1009+
},
1010+
"required": [
1011+
"code",
1012+
"message",
1013+
],
1014+
"type": "object",
1015+
},
1016+
"Pet": {
1017+
"properties": {
1018+
"id": {
1019+
"format": "int64",
1020+
"type": "integer",
1021+
},
1022+
"name": {
1023+
"type": "string",
1024+
},
1025+
"tag": {
1026+
"type": "string",
1027+
},
1028+
},
1029+
"required": [
1030+
"id",
1031+
"name",
1032+
],
1033+
"type": "object",
1034+
},
1035+
"Pets": {
1036+
"items": {
1037+
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pet",
1038+
"properties": {
1039+
"id": {
1040+
"format": "int64",
1041+
"type": "integer",
1042+
},
1043+
"name": {
1044+
"type": "string",
1045+
},
1046+
"tag": {
1047+
"type": "string",
1048+
},
1049+
},
1050+
"required": [
1051+
"id",
1052+
"name",
1053+
],
1054+
"type": "object",
1055+
},
1056+
"maxItems": 100,
1057+
"type": "array",
1058+
},
1059+
},
1060+
},
1061+
"info": {
1062+
"license": {
1063+
"name": "MIT",
1064+
},
1065+
"title": "Swagger Petstore",
1066+
"version": "1.0.0",
1067+
},
1068+
"openapi": "3.1.0",
1069+
"paths": {
1070+
"/pets": {
1071+
"get": {
1072+
"operationId": "listPets",
1073+
"parameters": [
1074+
{
1075+
"description": "How many items to return at one time (max 100)",
1076+
"in": "query",
1077+
"name": "limit",
1078+
"required": false,
1079+
"schema": {
1080+
"format": "int32",
1081+
"maximum": 100,
1082+
"type": "integer",
1083+
},
1084+
},
1085+
],
1086+
"responses": {
1087+
"200": {
1088+
"content": {
1089+
"application/json": {
1090+
"schema": {
1091+
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pets",
1092+
"items": {
1093+
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pet",
1094+
"properties": {
1095+
"id": {
1096+
"format": "int64",
1097+
"type": "integer",
1098+
},
1099+
"name": {
1100+
"type": "string",
1101+
},
1102+
"tag": {
1103+
"type": "string",
1104+
},
1105+
},
1106+
"required": [
1107+
"id",
1108+
"name",
1109+
],
1110+
"type": "object",
1111+
},
1112+
"maxItems": 100,
1113+
"type": "array",
1114+
},
1115+
},
1116+
},
1117+
"description": "A paged array of pets",
1118+
"headers": {
1119+
"x-next": {
1120+
"description": "A link to the next page of responses",
1121+
"schema": {
1122+
"type": "string",
1123+
},
1124+
},
1125+
},
1126+
},
1127+
"default": {
1128+
"content": {
1129+
"application/json": {
1130+
"schema": {
1131+
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Error",
1132+
"properties": {
1133+
"code": {
1134+
"format": "int32",
1135+
"type": "integer",
1136+
},
1137+
"message": {
1138+
"type": "string",
1139+
},
1140+
},
1141+
"required": [
1142+
"code",
1143+
"message",
1144+
],
1145+
"type": "object",
1146+
},
1147+
},
1148+
},
1149+
"description": "unexpected error",
1150+
},
1151+
},
1152+
"servers": [
1153+
{
1154+
"url": "http://petstore.swagger.io/v1",
1155+
},
1156+
],
1157+
"summary": "List all pets",
1158+
"tags": [
1159+
"pets",
1160+
],
1161+
},
1162+
"post": {
1163+
"operationId": "createPets",
1164+
"responses": {
1165+
"201": {
1166+
"description": "Null response",
1167+
},
1168+
"default": {
1169+
"content": {
1170+
"application/json": {
1171+
"schema": {
1172+
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Error",
1173+
"properties": {
1174+
"code": {
1175+
"format": "int32",
1176+
"type": "integer",
1177+
},
1178+
"message": {
1179+
"type": "string",
1180+
},
1181+
},
1182+
"required": [
1183+
"code",
1184+
"message",
1185+
],
1186+
"type": "object",
1187+
},
1188+
},
1189+
},
1190+
"description": "unexpected error",
1191+
},
1192+
},
1193+
"servers": [
1194+
{
1195+
"url": "http://petstore.swagger.io/v1",
1196+
},
1197+
],
1198+
"summary": "Create a pet",
1199+
"tags": [
1200+
"pets",
1201+
],
1202+
},
1203+
"servers": [
1204+
{
1205+
"url": "http://petstore.swagger.io/v1",
1206+
},
1207+
],
1208+
},
1209+
"/pets/{petId}": {
1210+
"get": {
1211+
"operationId": "showPetById",
1212+
"parameters": [
1213+
{
1214+
"description": "The id of the pet to retrieve",
1215+
"in": "path",
1216+
"name": "petId",
1217+
"required": true,
1218+
"schema": {
1219+
"type": "string",
1220+
},
1221+
},
1222+
],
1223+
"responses": {
1224+
"200": {
1225+
"content": {
1226+
"application/json": {
1227+
"schema": {
1228+
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Pet",
1229+
"properties": {
1230+
"id": {
1231+
"format": "int64",
1232+
"type": "integer",
1233+
},
1234+
"name": {
1235+
"type": "string",
1236+
},
1237+
"tag": {
1238+
"type": "string",
1239+
},
1240+
},
1241+
"required": [
1242+
"id",
1243+
"name",
1244+
],
1245+
"type": "object",
1246+
},
1247+
},
1248+
},
1249+
"description": "Expected response to a valid request",
1250+
},
1251+
"default": {
1252+
"content": {
1253+
"application/json": {
1254+
"schema": {
1255+
"$$ref": "https://smartbear.com/petstore.json#/components/schemas/Error",
1256+
"properties": {
1257+
"code": {
1258+
"format": "int32",
1259+
"type": "integer",
1260+
},
1261+
"message": {
1262+
"type": "string",
1263+
},
1264+
},
1265+
"required": [
1266+
"code",
1267+
"message",
1268+
],
1269+
"type": "object",
1270+
},
1271+
},
1272+
},
1273+
"description": "unexpected error",
1274+
},
1275+
},
1276+
"servers": [
1277+
{
1278+
"url": "http://petstore.swagger.io/v1",
1279+
},
1280+
],
1281+
"summary": "Info for a specific pet",
1282+
"tags": [
1283+
"pets",
1284+
],
1285+
},
1286+
"servers": [
1287+
{
1288+
"url": "http://petstore.swagger.io/v1",
1289+
},
1290+
],
1291+
},
1292+
},
1293+
"servers": [
1294+
{
1295+
"url": "http://petstore.swagger.io/v1",
1296+
},
1297+
],
1298+
},
1299+
}
1300+
`;
1301+
9931302
exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec option and baseDoc option is provided should resolve 1`] = `
9941303
{
9951304
"errors": [],
@@ -1410,7 +1719,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
14101719
}
14111720
`;
14121721

1413-
exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec option and neither baseDoc nor url option is provided should resolve 1`] = `
1722+
exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec option and neither baseDoc nor url option is provided should resolve using implicit baseURI=https://smartbear.com/ 1`] = `
14141723
{
14151724
"errors": [],
14161725
"spec": {
@@ -1454,6 +1763,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
14541763
},
14551764
"Pets": {
14561765
"items": {
1766+
"$$ref": "https://smartbear.com/#/components/schemas/Pet",
14571767
"properties": {
14581768
"id": {
14591769
"format": "int64",
@@ -1507,7 +1817,9 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
15071817
"content": {
15081818
"application/json": {
15091819
"schema": {
1820+
"$$ref": "https://smartbear.com/#/components/schemas/Pets",
15101821
"items": {
1822+
"$$ref": "https://smartbear.com/#/components/schemas/Pet",
15111823
"properties": {
15121824
"id": {
15131825
"format": "int64",
@@ -1545,6 +1857,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
15451857
"content": {
15461858
"application/json": {
15471859
"schema": {
1860+
"$$ref": "https://smartbear.com/#/components/schemas/Error",
15481861
"properties": {
15491862
"code": {
15501863
"format": "int32",
@@ -1585,6 +1898,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
15851898
"content": {
15861899
"application/json": {
15871900
"schema": {
1901+
"$$ref": "https://smartbear.com/#/components/schemas/Error",
15881902
"properties": {
15891903
"code": {
15901904
"format": "int32",
@@ -1640,6 +1954,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
16401954
"content": {
16411955
"application/json": {
16421956
"schema": {
1957+
"$$ref": "https://smartbear.com/#/components/schemas/Pet",
16431958
"properties": {
16441959
"id": {
16451960
"format": "int64",
@@ -1666,6 +1981,7 @@ exports[`resolve OpenAPI 3.1.0 strategy given OpenAPI 3.1.0 definition via spec
16661981
"content": {
16671982
"application/json": {
16681983
"schema": {
1984+
"$$ref": "https://smartbear.com/#/components/schemas/Error",
16691985
"properties": {
16701986
"code": {
16711987
"format": "int32",

0 commit comments

Comments
 (0)