-
Notifications
You must be signed in to change notification settings - Fork 22
feat(apidom-reference): resolve references async v3 #5051
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
653fa7e
feat(apidom-reference): resolve references async v3
lukaszzazulak 21a27ed
feat(apidom-ls): resolve references async v3
lukaszzazulak c4d8016
feat(apidom-reference): resolve references async v3
lukaszzazulak 2f93244
feat(apidom-reference): resolve references async v3
lukaszzazulak d60587b
feat(apidom-reference): resolve references async v3
lukaszzazulak 82a2afd
feat(apidom-reference): resolve references async v3
lukaszzazulak 617db94
feat(apidom-reference): resolve references async v3
lukaszzazulak 6cb0a5a
feat(apidom-reference): resolve references async v3
lukaszzazulak a9b92da
Merge branch 'main' into feat/async-api3-resolve-references
lukaszzazulak File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
140 changes: 140 additions & 0 deletions
140
packages/apidom-reference/src/dereference/strategies/asyncapi-3/index.ts
This file contains hidden or 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,140 @@ | ||
| import { createNamespace, visit, Element, cloneDeep } from '@swagger-api/apidom-core'; | ||
| import asyncApi3Namespace, { | ||
| getNodeType, | ||
| isAsyncApi3Element, | ||
| keyMap, | ||
| mediaTypes, | ||
| } from '@swagger-api/apidom-ns-asyncapi-3'; | ||
|
|
||
| import DereferenceStrategy, { DereferenceStrategyOptions } from '../DereferenceStrategy.ts'; | ||
| import File from '../../../File.ts'; | ||
| import Reference from '../../../Reference.ts'; | ||
| import ReferenceSet from '../../../ReferenceSet.ts'; | ||
| import AsyncAPI3DereferenceVisitor from './visitor.ts'; | ||
| import type { ReferenceOptions } from '../../../options/index.ts'; | ||
|
|
||
| export type { | ||
| default as DereferenceStrategy, | ||
| DereferenceStrategyOptions, | ||
| } from '../DereferenceStrategy.ts'; | ||
| export type { default as File, FileOptions } from '../../../File.ts'; | ||
| export type { default as Reference, ReferenceOptions } from '../../../Reference.ts'; | ||
| export type { default as ReferenceSet, ReferenceSetOptions } from '../../../ReferenceSet.ts'; | ||
| export type { AsyncAPI3DereferenceVisitorOptions, mutationReplacer } from './visitor.ts'; | ||
| export type { | ||
| ReferenceOptions as ApiDOMReferenceOptions, | ||
| ReferenceBundleOptions as ApiDOMReferenceBundleOptions, | ||
| ReferenceDereferenceOptions as ApiDOMReferenceDereferenceOptions, | ||
| ReferenceParseOptions as ApiDOMReferenceParseOptions, | ||
| ReferenceResolveOptions as ApiDOMReferenceResolveOptions, | ||
| } from '../../../options/index.ts'; | ||
| export type { default as Parser, ParserOptions } from '../../../parse/parsers/Parser.ts'; | ||
| export type { default as Resolver, ResolverOptions } from '../../../resolve/resolvers/Resolver.ts'; | ||
| export type { | ||
| default as ResolveStrategy, | ||
| ResolveStrategyOptions, | ||
| } from '../../../resolve/strategies/ResolveStrategy.ts'; | ||
| export type { | ||
| default as BundleStrategy, | ||
| BundleStrategyOptions, | ||
| } from '../../../bundle/strategies/BundleStrategy.ts'; | ||
| export type { AncestorLineage } from '../../util.ts'; | ||
|
|
||
| // @ts-ignore | ||
| const visitAsync = visit[Symbol.for('nodejs.util.promisify.custom')]; | ||
|
|
||
| /** | ||
| * @public | ||
| */ | ||
| export interface AsyncAPI3DeferenceStrategyOptions | ||
| extends Omit<DereferenceStrategyOptions, 'name'> {} | ||
|
|
||
| /** | ||
| * @public | ||
| */ | ||
| class AsyncAPI3DereferenceStrategy extends DereferenceStrategy { | ||
| constructor(options?: AsyncAPI3DeferenceStrategyOptions) { | ||
| super({ ...(options ?? {}), name: 'asyncapi-3' }); | ||
| } | ||
|
|
||
| canDereference(file: File): boolean { | ||
| // assert by media type | ||
| if (file.mediaType !== 'text/plain') { | ||
| return mediaTypes.includes(file.mediaType); | ||
| } | ||
|
|
||
| // assert by inspecting ApiDOM | ||
| return isAsyncApi3Element(file.parseResult?.api); | ||
| } | ||
|
|
||
| async dereference(file: File, options: ReferenceOptions): Promise<Element> { | ||
| const namespace = createNamespace(asyncApi3Namespace); | ||
| const immutableRefSet = options.dereference.refSet ?? new ReferenceSet(); | ||
| const mutableRefSet = new ReferenceSet(); | ||
| let refSet = immutableRefSet; | ||
| let reference: Reference; | ||
|
|
||
| if (!immutableRefSet.has(file.uri)) { | ||
| reference = new Reference({ uri: file.uri, value: file.parseResult! }); | ||
| immutableRefSet.add(reference); | ||
| } else { | ||
| // pre-computed refSet was provided as configuration option | ||
| reference = immutableRefSet.find((ref) => ref.uri === file.uri)!; | ||
| } | ||
|
|
||
| /** | ||
| * Clone refSet due the dereferencing process being mutable. | ||
| * We don't want to mutate the original refSet and the references. | ||
| */ | ||
| if (options.dereference.immutable) { | ||
| immutableRefSet.refs | ||
| .map( | ||
| (ref) => | ||
| new Reference({ | ||
| ...ref, | ||
| value: cloneDeep(ref.value), | ||
| }), | ||
| ) | ||
| .forEach((ref) => mutableRefSet.add(ref)); | ||
| reference = mutableRefSet.find((ref) => ref.uri === file.uri)!; | ||
| refSet = mutableRefSet; | ||
| } | ||
|
|
||
| const visitor = new AsyncAPI3DereferenceVisitor({ reference, namespace, options }); | ||
| const dereferencedElement = await visitAsync(refSet.rootRef!.value, visitor, { | ||
| keyMap, | ||
| nodeTypeGetter: getNodeType, | ||
| }); | ||
|
|
||
| /** | ||
| * If immutable option is set, replay refs from the refSet. | ||
| */ | ||
| if (options.dereference.immutable) { | ||
| mutableRefSet.refs | ||
| .filter((ref) => ref.uri.startsWith('immutable://')) | ||
| .map( | ||
| (ref) => | ||
| new Reference({ | ||
| ...ref, | ||
| uri: ref.uri.replace(/^immutable:\/\//, ''), | ||
| }), | ||
| ) | ||
| .forEach((ref) => immutableRefSet.add(ref)); | ||
| } | ||
|
|
||
| /** | ||
| * Release all memory if this refSet was not provided as a configuration option. | ||
| * If provided as configuration option, then provider is responsible for cleanup. | ||
| */ | ||
| if (options.dereference.refSet === null) { | ||
| immutableRefSet.clean(); | ||
| } | ||
|
|
||
| mutableRefSet.clean(); | ||
|
|
||
| return dereferencedElement; | ||
| } | ||
| } | ||
|
|
||
| export { AsyncAPI3DereferenceVisitor }; | ||
| export default AsyncAPI3DereferenceStrategy; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.