-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: compute dependency graphs early & always use the deep one fix: prop names with special characters fix: schema names with special characters
- Loading branch information
Showing
19 changed files
with
450 additions
and
160 deletions.
There are no files selected for viewing
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
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,51 @@ | ||
import type { OpenAPIObject, SchemaObject } from "openapi3-ts"; | ||
import { get } from "pastable/server"; | ||
import { normalizeString } from "./utils"; | ||
|
||
const autocorrectRef = (ref: string) => (ref[1] === "/" ? ref : "#/" + ref.slice(1)); | ||
|
||
type RefInfo = { | ||
ref: string; | ||
name: string; | ||
normalized: string; | ||
}; | ||
|
||
export const makeSchemaResolver = (doc: OpenAPIObject) => { | ||
const nameByRef = new Map<string, string>(); | ||
const refByName = new Map<string, string>(); | ||
|
||
const byRef = new Map<string, RefInfo>(); | ||
const byNormalized = new Map<string, RefInfo>(); | ||
|
||
const getSchemaByRef = (ref: string) => { | ||
// #components -> #/components | ||
const correctRef = autocorrectRef(ref); | ||
const split = correctRef.split("/"); | ||
|
||
// "#/components/schemas/Something.jsonld" -> #/components/schemas | ||
const path = split.slice(1, split.length - 1).join("/")!; | ||
const map = get(doc, path.replace("#/", "").replace("#", "").replaceAll("/", ".")) ?? ({} as any); | ||
|
||
// "#/components/schemas/Something.jsonld" -> "Something.jsonld" | ||
const name = split[split.length - 1]!; | ||
const normalized = normalizeString(name); | ||
|
||
nameByRef.set(correctRef, normalized); | ||
refByName.set(normalized, correctRef); | ||
|
||
const infos = { ref: correctRef, name, normalized }; | ||
byRef.set(infos.ref, infos); | ||
byNormalized.set(infos.normalized, infos); | ||
|
||
// doc.components.schemas["Something.jsonld"] | ||
return map[name] as SchemaObject; | ||
}; | ||
|
||
return { | ||
getSchemaByRef, | ||
resolveRef: (ref: string) => byRef.get(autocorrectRef(ref))!, | ||
resolveSchemaName: (normalized: string) => byNormalized.get(normalized)!, | ||
}; | ||
}; | ||
|
||
export type DocumentResolver = ReturnType<typeof makeSchemaResolver>; |
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
Oops, something went wrong.