Skip to content

Commit 30229f8

Browse files
committed
All tests are running now
Implement a partial getLinkLocationType: Technically 'title' type does not exist, it should be file. operationRef follows the same guidelines as $ref. See https://swagger.io/docs/specification/using-ref/ The only types that **really** exist are local, remote, and url.
1 parent fe4ed65 commit 30229f8

File tree

8 files changed

+130
-75
lines changed

8 files changed

+130
-75
lines changed

packages/openapi-to-graphql/lib/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/oas_3_tools.js

+21-14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/oas_3_tools.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/schema_builder.js

+51-28
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/schema_builder.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/src/schema_builder.ts

+51-28
Original file line numberDiff line numberDiff line change
@@ -1344,8 +1344,16 @@ export function getArgs<TSource, TContext, TArgs>({
13441344
* For example, name reference, file path, web-hosted OAS link, etc.
13451345
*/
13461346
function getLinkLocationType(linkLocation: string): string {
1347-
// TODO: currently we only support the title as a link location
1348-
return 'title'
1347+
if (
1348+
linkLocation.startsWith('http://') ||
1349+
linkLocation.startsWith('https://') ||
1350+
linkLocation.startsWith('//')
1351+
) {
1352+
// TODO: Should probably use new (require('url').URL)(linkLocation) to check link validity
1353+
return 'url'
1354+
}
1355+
1356+
return 'file'
13491357
}
13501358

13511359
/**
@@ -1358,48 +1366,37 @@ function getOasFromLinkLocation<TSource, TContext, TArgs>(
13581366
data: PreprocessingData<TSource, TContext, TArgs>
13591367
): Oas3 {
13601368
// May be an external reference
1361-
switch (getLinkLocationType(linkLocation)) {
1362-
case 'title':
1363-
// Get the possible
1364-
const possibleOass = data.oass.filter((oas) => {
1365-
return oas.info.title === linkLocation
1369+
const locationType = getLinkLocationType(linkLocation)
1370+
let possibleOass
1371+
switch (locationType) {
1372+
// FIXME: This is really the wrong way to do this. We are throwing away all path information and hoping we can just use the filename.
1373+
case 'file':
1374+
// Reduce path of the reference to just the filename
1375+
let filename = linkLocation.substring(linkLocation.lastIndexOf('/') + 1)
1376+
1377+
// Find all OAS documents with that filename
1378+
possibleOass = data.oass.filter((oas) => {
1379+
return oas.info['x-filename'] === filename
13661380
})
13671381

1368-
// Check if there are an ambiguous OASs
1369-
if (possibleOass.length === 1) {
1370-
// No ambiguity
1371-
return possibleOass[0]
1372-
} else if (possibleOass.length > 1) {
1373-
// Some ambiguity
1374-
handleWarning({
1375-
mitigationType: MitigationTypes.AMBIGUOUS_LINK,
1376-
message:
1377-
`The operationRef '${link.operationRef}' references an ` +
1378-
`OAS '${linkLocation}' but multiple OASs share the same title`,
1379-
data,
1380-
log: translationLog
1381-
})
1382-
} else {
1383-
// No OAS had the expected title
1382+
if (possibleOass.length === 0) {
13841383
handleWarning({
13851384
mitigationType: MitigationTypes.UNRESOLVABLE_LINK,
13861385
message:
13871386
`The operationRef '${link.operationRef}' references an ` +
1388-
`OAS '${linkLocation}' but no such OAS was provided`,
1387+
`OAS '${linkLocation}' but no OAS with a info.x-filename matching '${filename}' was found`,
13891388
data,
13901389
log: translationLog
13911390
})
1391+
return undefined
13921392
}
1393+
13931394
break
13941395

13951396
// // TODO
13961397
// case 'url':
13971398
// break
13981399

1399-
// // TODO
1400-
// case 'file':
1401-
// break
1402-
14031400
// TODO: should title be default?
14041401
// In cases of names like api.io
14051402
default:
@@ -1413,4 +1410,30 @@ function getOasFromLinkLocation<TSource, TContext, TArgs>(
14131410
log: translationLog
14141411
})
14151412
}
1413+
1414+
// Check if there are an ambiguous OASs
1415+
if (possibleOass.length === 1) {
1416+
// No ambiguity
1417+
return possibleOass[0]
1418+
} else if (possibleOass.length > 1) {
1419+
// Some ambiguity
1420+
handleWarning({
1421+
mitigationType: MitigationTypes.AMBIGUOUS_LINK,
1422+
message:
1423+
`The operationRef '${link.operationRef}' references an ` +
1424+
`OAS '${linkLocation}' but multiple OASs were matched`,
1425+
data,
1426+
log: translationLog
1427+
})
1428+
} else {
1429+
// No OAS matches were found
1430+
handleWarning({
1431+
mitigationType: MitigationTypes.UNRESOLVABLE_LINK,
1432+
message:
1433+
`The operationRef '${link.operationRef}' references an ` +
1434+
`OAS '${linkLocation}' but no matching OAS was provided`,
1435+
data,
1436+
log: translationLog
1437+
})
1438+
}
14161439
}

packages/openapi-to-graphql/test/fixtures/example_oas.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"license": {
1313
"name": "Apache 2.0",
1414
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
15-
}
15+
},
16+
"x-filename": "example_oas.json"
1617
},
1718
"externalDocs": {
1819
"url": "http://example.com/docs",

packages/openapi-to-graphql/test/fixtures/example_oas3.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
"license": {
1313
"name": "Apache 2.0",
1414
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
15-
}
15+
},
16+
"x-filename": "example_oas3.json"
1617
},
1718
"externalDocs": {
1819
"url": "http://example.com/docs",

0 commit comments

Comments
 (0)