-
-
Notifications
You must be signed in to change notification settings - Fork 366
Adjust names of security schemes to match prefixed name #3861
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
base: main
Are you sure you want to change the base?
Changes from all commits
73a8153
91ab2b0
526eeba
e7cf98b
7339164
7579098
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -42,7 +42,15 @@ export function getResolvedInput({ | |
| resolvedInput.path = url.fromFileSystemPath(resolvedInput.path); | ||
| resolvedInput.type = 'file'; | ||
| } else if (!resolvedInput.path && pathOrUrlOrSchema && typeof pathOrUrlOrSchema === 'object') { | ||
| if ('$id' in pathOrUrlOrSchema && pathOrUrlOrSchema.$id) { | ||
| if ( | ||
| ('openapi' in pathOrUrlOrSchema && pathOrUrlOrSchema.openapi) || | ||
| ('swagger' in pathOrUrlOrSchema && pathOrUrlOrSchema.swagger) | ||
| ) { | ||
| resolvedInput.schema = pathOrUrlOrSchema; | ||
| resolvedInput.type = 'json'; | ||
| if ('$id' in pathOrUrlOrSchema && pathOrUrlOrSchema.$id) | ||
| resolvedInput.path = pathOrUrlOrSchema.$id as string; | ||
| } else if ('$id' in pathOrUrlOrSchema && pathOrUrlOrSchema.$id) { | ||
| // when schema id has defined an URL should use that hostname to request the references, | ||
| // instead of using the current page URL | ||
| const { hostname, protocol } = new URL(pathOrUrlOrSchema.$id as string); | ||
|
|
@@ -385,7 +393,8 @@ export class $RefParser { | |
| const baseName = (p: string) => { | ||
| try { | ||
| const withoutHash = p.split('#')[0]!; | ||
| const parts = withoutHash.split('/'); | ||
| const withoutTrailingSlash = withoutHash.replace(/\/+$/, ''); | ||
| const parts = withoutTrailingSlash.split('/'); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @StratusFearMe21 why is this change needed? Reverting it doesn't fail tests
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oops, I didn't mean to keep that in this PR, that's my mistake. It fixes another bug where if the
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want to open a separate pull request for that? |
||
| const filename = parts[parts.length - 1] || 'schema'; | ||
| const dot = filename.lastIndexOf('.'); | ||
| const raw = dot > 0 ? filename.substring(0, dot) : filename; | ||
|
|
@@ -461,6 +470,14 @@ export class $RefParser { | |
| } | ||
| } else if (k === 'tags' && Array.isArray(v) && v.every((x) => typeof x === 'string')) { | ||
| out[k] = v.map((t) => tagMap.get(t) || t); | ||
| } else if (k === 'security' && Array.isArray(v)) { | ||
| out[k] = v.map((s) => { | ||
| const securityScheme: Record<string, any> = {}; | ||
| for (const [key, value] of Object.entries(s)) { | ||
| securityScheme[`${opIdPrefix}_${key}`] = value; | ||
| } | ||
| return securityScheme; | ||
| }); | ||
| } else if (k === 'operationId' && typeof v === 'string') { | ||
| out[k] = unique(usedOpIds, `${opIdPrefix}_${v}`); | ||
| } else { | ||
|
|
||
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.
@StratusFearMe21 why did you need to add this piece? I see the test fails without it, but I'm concerned about tying internals to OpenAPI/Swagger like this
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.
That piece is so that the regression test can set
resolvedInput.pathon each input schema without the type of theresolvedInputbeing set to'url'. This is so that when the test callsbundleManyon the input, the prefixes on the outputsecuritySchemasis predictable.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.
I'm not sure I understand, is this just to satisfy the test suite? Or is this needed for a real world usage?
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.
I put it there to satisfy the test suite, but if you were using this package in the real world, passing multiple JSON objects to
bundleManywould be broken without this change since both schemas would resolve to the samepath. Eitherurl.cwd()or'schema'.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.
Might be better to handle that as a separate issue. I'm also concerned about this package, it feels like it's doing too much too poorly because I'm sure there are other similar edge cases we just didn't run into yet
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.
Why
$idon a schema object is essential tobundleManyLine 45 is in
getResolvedInput, which is called byparseMany(line 250) — the first step ofbundleMany. The critical logic is at lines 44-59:When a JSON object (not a file path or URL) is passed as an input,
resolvedInput.pathstarts as''(line 30). Without a$id, the path stays empty, and line 62-64 kicks in:This means a schema without
$idfalls through entirely —typestays'url', no path is set, and it gets resolved againsturl.cwd()(the current working directory).But when the schema does have
$id(and is an OpenAPI/Swagger document), lines 51-52 setresolvedInput.pathto the$idvalue andtypeto'json'. This is critical forbundleManybecause:parseMany(line 295) stores this path inthis.schemaManySources[i]:mergeMany(line 489-490) uses that source path to compute the prefix used for namespacing all components:That prefix is then used to rename all components (schemas, parameters, securitySchemes, etc.) to avoid collisions between the multiple inputs:
It's also used to rewrite
$refpointers (line 512), prefix operation IDs (line 479), prefix tag names (line 522), and resolve relative external references (line 463).Without
$id, an inline schema object would getpath = '', which would makebaseNamereturn a generic name (or fall back to the CWD-derived path), causing unpredictable or colliding prefixes when multiple JSON objects are bundled together. The$idprovides a stable, unique identity for each inline schema, enabling the entire namespacing and reference-rewriting machinery ofbundleManyto work correctly.')