-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(reference): add OpenAPI 2.0 dereference strategy (#3435)
Refs #3102
- Loading branch information
Showing
66 changed files
with
2,019 additions
and
20 deletions.
There are no files selected for viewing
11 changes: 8 additions & 3 deletions
11
packages/apidom-ns-json-schema-draft-4/src/refractor/predicates.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,12 @@ | ||
import { isObjectElement, Element } from '@swagger-api/apidom-core'; | ||
import { isObjectElement, ObjectElement } from '@swagger-api/apidom-core'; | ||
|
||
export interface JSONReferenceLikeElement extends ObjectElement { | ||
hasKey: (value: '$ref') => true; | ||
} | ||
|
||
// eslint-disable-next-line import/prefer-default-export | ||
export const isJSONReferenceLikeElement = <T extends Element>(element: T): boolean => { | ||
// @ts-ignore | ||
export const isJSONReferenceLikeElement = ( | ||
element: unknown, | ||
): element is JSONReferenceLikeElement => { | ||
return isObjectElement(element) && element.hasKey('$ref'); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
packages/apidom-reference/src/dereference/strategies/openapi-2/index.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import stampit from 'stampit'; | ||
import { defaultTo, propEq } from 'ramda'; | ||
import { createNamespace, visit, Element } from '@swagger-api/apidom-core'; | ||
import openApi2Namespace, { | ||
getNodeType, | ||
isSwaggerElement, | ||
keyMap, | ||
mediaTypes, | ||
} from '@swagger-api/apidom-ns-openapi-2'; | ||
|
||
import DereferenceStrategy from '../DereferenceStrategy'; | ||
import { | ||
DereferenceStrategy as IDereferenceStrategy, | ||
File as IFile, | ||
ReferenceOptions as IReferenceOptions, | ||
} from '../../../types'; | ||
import Reference from '../../../Reference'; | ||
import ReferenceSet from '../../../ReferenceSet'; | ||
import OpenApi2DereferenceVisitor from './visitor'; | ||
|
||
// @ts-ignore | ||
const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')]; | ||
|
||
const OpenApi2DereferenceStrategy: stampit.Stamp<IDereferenceStrategy> = stampit( | ||
DereferenceStrategy, | ||
{ | ||
init() { | ||
this.name = 'openapi-2'; | ||
}, | ||
methods: { | ||
canDereference(file: IFile): boolean { | ||
// assert by media type | ||
if (file.mediaType !== 'text/plain') { | ||
return mediaTypes.includes(file.mediaType); | ||
} | ||
|
||
// assert by inspecting ApiDOM | ||
return isSwaggerElement(file.parseResult?.api); | ||
}, | ||
|
||
async dereference(file: IFile, options: IReferenceOptions): Promise<Element> { | ||
const namespace = createNamespace(openApi2Namespace); | ||
const refSet = defaultTo(ReferenceSet(), options.dereference.refSet); | ||
let reference; | ||
|
||
if (!refSet.has(file.uri)) { | ||
reference = Reference({ uri: file.uri, value: file.parseResult }); | ||
refSet.add(reference); | ||
} else { | ||
// pre-computed refSet was provided as configuration option | ||
reference = refSet.find(propEq(file.uri, 'uri')); | ||
} | ||
|
||
const visitor = OpenApi2DereferenceVisitor({ reference, namespace, options }); | ||
const dereferencedElement = await visitAsync(refSet.rootRef.value, visitor, { | ||
keyMap, | ||
nodeTypeGetter: getNodeType, | ||
}); | ||
|
||
/** | ||
* Release all memory if this refSet was not provided as an configuration option. | ||
* If provided as configuration option, then provider is responsible for cleanup. | ||
*/ | ||
if (options.dereference.refSet === null) { | ||
refSet.clean(); | ||
} | ||
|
||
return dereferencedElement; | ||
}, | ||
}, | ||
}, | ||
); | ||
|
||
export default OpenApi2DereferenceStrategy; |
Oops, something went wrong.