-
-
Notifications
You must be signed in to change notification settings - Fork 420
[feature request] #447 partial support external urls in paths #1434
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
Open
js2me
wants to merge
6
commits into
main
Choose a base branch
from
feature_refs_in_paths
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a56ded2
feat: add ability to use in paths
js2me c670ca3
feat: partial support external refs in paths (#447)
js2me 016589d
chore: fix build after new changes
js2me 780c0c8
chore: fix build after new changes
js2me 90e0267
feat: multiple swagger schemas using $ref in paths
js2me 0a81ae6
Merge remote-tracking branch 'origin/main' into feature_refs_in_paths
js2me 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "swagger-typescript-api": minor | ||
| --- | ||
|
|
||
| partial support external paths by ref (#447) |
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,19 @@ | ||
| { | ||
| "editor.defaultFormatter": "biomejs.biome", | ||
| "[typescript]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| }, | ||
| "[typescriptreact]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| }, | ||
| "[javascript]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| }, | ||
| "[javascriptreact]": { | ||
| "editor.defaultFormatter": "biomejs.biome" | ||
| }, | ||
| "editor.codeActionsOnSave": { | ||
| "source.fixAll.biome": "explicit", | ||
| "source.organizeImports.biome": "explicit" | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| import type { resolve } from "@apidevtools/swagger-parser"; | ||
| import consola from "consola"; | ||
| import type { OpenAPI } from "openapi-types"; | ||
| import type { AnyObject, Maybe, Primitive } from "yummies/utils/types"; | ||
| import type { CodeGenConfig } from "./configuration.js"; | ||
|
|
||
| export interface RefDetails { | ||
| ref: string; | ||
| isLocal: boolean; | ||
| externalUrlOrPath: Maybe<string>; | ||
| externalOpenapiFileName?: string; | ||
| } | ||
|
|
||
| export class ResolvedSwaggerSchema { | ||
| private parsedRefsCache = new Map<string, RefDetails>(); | ||
|
|
||
| constructor( | ||
| private config: CodeGenConfig, | ||
| public usageSchema: OpenAPI.Document, | ||
| public originalSchema: OpenAPI.Document, | ||
| private resolvers: Awaited<ReturnType<typeof resolve>>[], | ||
| ) { | ||
| this.usageSchema = usageSchema; | ||
| this.originalSchema = originalSchema; | ||
| } | ||
|
|
||
| getRefDetails(ref: string): RefDetails { | ||
| if (!this.parsedRefsCache.has(ref)) { | ||
| const isLocal = ref.startsWith("#"); | ||
|
|
||
| if (isLocal) { | ||
| this.parsedRefsCache.set(ref, { | ||
| ref, | ||
| isLocal, | ||
| externalUrlOrPath: null, | ||
| }); | ||
| } else { | ||
| const externalUrlOrPath = ref.split("#")[0]!; | ||
| let externalOpenapiFileName = externalUrlOrPath.split("/").at(-1) || ""; | ||
|
|
||
| if ( | ||
| externalOpenapiFileName.endsWith(".json") || | ||
| externalOpenapiFileName.endsWith(".yaml") | ||
| ) { | ||
| externalOpenapiFileName = externalOpenapiFileName.slice(0, -5); | ||
| } else if (externalOpenapiFileName.endsWith(".yml")) { | ||
| externalOpenapiFileName = externalOpenapiFileName.slice(0, -4); | ||
| } | ||
|
|
||
| this.parsedRefsCache.set(ref, { | ||
| ref, | ||
| isLocal, | ||
| externalUrlOrPath, | ||
| externalOpenapiFileName, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return this.parsedRefsCache.get(ref)!; | ||
| } | ||
|
|
||
| isLocalRef(ref: string): boolean { | ||
| return this.getRefDetails(ref).isLocal; | ||
| } | ||
|
|
||
| getRef(ref: Maybe<string>): Maybe<AnyObject | Primitive> { | ||
| if (!ref) { | ||
| return null; | ||
| } | ||
|
|
||
| const resolvedByOrigRef = this.tryToResolveRef(ref); | ||
|
|
||
| if (resolvedByOrigRef) { | ||
| return resolvedByOrigRef; | ||
| } | ||
|
|
||
| // const ref.match(/\#[a-z]/) | ||
| if (/#[a-z]/.test(ref)) { | ||
| const fixedRef = ref.replace(/#[a-z]/, (match) => { | ||
| const [hashtag, char] = match.split(""); | ||
| return `${hashtag}/${char}`; | ||
| }); | ||
|
|
||
| return this.tryToResolveRef(fixedRef); | ||
| } | ||
|
|
||
| // this.tryToResolveRef(`@usage${ref}`) ?? | ||
| // this.tryToResolveRef(`@original${ref}`) | ||
| } | ||
|
|
||
| private tryToResolveRef(ref: Maybe<string>) { | ||
| if (!this.resolvers || !ref) { | ||
| return null; | ||
| } | ||
|
|
||
| for (const resolver of this.resolvers) { | ||
| try { | ||
| const resolvedAsIs = resolver.get(ref); | ||
| return resolvedAsIs; | ||
| } catch (e) { | ||
| consola.debug(e); | ||
| } | ||
| } | ||
|
|
||
| return null; | ||
| } | ||
| } | ||
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Bug: JSON Pointer Normalization Fails for Valid Paths
The
getRefmethod's logic for normalizing local references is flawed. The regex/#[a-z]/is too restrictive, missing valid JSON pointer paths that don't start with a lowercase letter. When it does match, the replacement logic incorrectly truncates the path (e.g.,#<char>becomes#/char), leading to invalid references and preventing proper schema resolution.