Skip to content

Commit

Permalink
All tests are running now
Browse files Browse the repository at this point in the history
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.

Signed-off-by: Ayrton Sparling <ayrton@sparling.us>
  • Loading branch information
FallingSnow committed Jul 1, 2021
1 parent 799c2b5 commit 4e914f9
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 75 deletions.
2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/index.js.map

Large diffs are not rendered by default.

35 changes: 21 additions & 14 deletions packages/openapi-to-graphql/lib/oas_3_tools.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/oas_3_tools.js.map

Large diffs are not rendered by default.

79 changes: 51 additions & 28 deletions packages/openapi-to-graphql/lib/schema_builder.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/openapi-to-graphql/lib/schema_builder.js.map

Large diffs are not rendered by default.

79 changes: 51 additions & 28 deletions packages/openapi-to-graphql/src/schema_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,8 +1344,16 @@ export function getArgs<TSource, TContext, TArgs>({
* For example, name reference, file path, web-hosted OAS link, etc.
*/
function getLinkLocationType(linkLocation: string): string {
// TODO: currently we only support the title as a link location
return 'title'
if (
linkLocation.startsWith('http://') ||
linkLocation.startsWith('https://') ||
linkLocation.startsWith('//')
) {
// TODO: Should probably use new (require('url').URL)(linkLocation) to check link validity
return 'url'
}

return 'file'
}

/**
Expand All @@ -1358,48 +1366,37 @@ function getOasFromLinkLocation<TSource, TContext, TArgs>(
data: PreprocessingData<TSource, TContext, TArgs>
): Oas3 {
// May be an external reference
switch (getLinkLocationType(linkLocation)) {
case 'title':
// Get the possible
const possibleOass = data.oass.filter((oas) => {
return oas.info.title === linkLocation
const locationType = getLinkLocationType(linkLocation)
let possibleOass
switch (locationType) {
// 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.
case 'file':
// Reduce path of the reference to just the filename
let filename = linkLocation.substring(linkLocation.lastIndexOf('/') + 1)

// Find all OAS documents with that filename
possibleOass = data.oass.filter((oas) => {
return oas.info['x-filename'] === filename
})

// Check if there are an ambiguous OASs
if (possibleOass.length === 1) {
// No ambiguity
return possibleOass[0]
} else if (possibleOass.length > 1) {
// Some ambiguity
handleWarning({
mitigationType: MitigationTypes.AMBIGUOUS_LINK,
message:
`The operationRef '${link.operationRef}' references an ` +
`OAS '${linkLocation}' but multiple OASs share the same title`,
data,
log: translationLog
})
} else {
// No OAS had the expected title
if (possibleOass.length === 0) {
handleWarning({
mitigationType: MitigationTypes.UNRESOLVABLE_LINK,
message:
`The operationRef '${link.operationRef}' references an ` +
`OAS '${linkLocation}' but no such OAS was provided`,
`OAS '${linkLocation}' but no OAS with a info.x-filename matching '${filename}' was found`,
data,
log: translationLog
})
return undefined
}

break

// // TODO
// case 'url':
// break

// // TODO
// case 'file':
// break

// TODO: should title be default?
// In cases of names like api.io
default:
Expand All @@ -1413,4 +1410,30 @@ function getOasFromLinkLocation<TSource, TContext, TArgs>(
log: translationLog
})
}

// Check if there are an ambiguous OASs
if (possibleOass.length === 1) {
// No ambiguity
return possibleOass[0]
} else if (possibleOass.length > 1) {
// Some ambiguity
handleWarning({
mitigationType: MitigationTypes.AMBIGUOUS_LINK,
message:
`The operationRef '${link.operationRef}' references an ` +
`OAS '${linkLocation}' but multiple OASs were matched`,
data,
log: translationLog
})
} else {
// No OAS matches were found
handleWarning({
mitigationType: MitigationTypes.UNRESOLVABLE_LINK,
message:
`The operationRef '${link.operationRef}' references an ` +
`OAS '${linkLocation}' but no matching OAS was provided`,
data,
log: translationLog
})
}
}
3 changes: 2 additions & 1 deletion packages/openapi-to-graphql/test/fixtures/example_oas.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"x-filename": "example_oas.json"
},
"externalDocs": {
"url": "http://example.com/docs",
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-to-graphql/test/fixtures/example_oas3.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
"license": {
"name": "Apache 2.0",
"url": "http://www.apache.org/licenses/LICENSE-2.0.html"
}
},
"x-filename": "example_oas3.json"
},
"externalDocs": {
"url": "http://example.com/docs",
Expand Down

0 comments on commit 4e914f9

Please sign in to comment.