-
-
Notifications
You must be signed in to change notification settings - Fork 240
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
fix(core): fix 'resolved vs unresolved' json path mapping #2202
Conversation
@P0lip Hi Jakub, could you please give this PR a look when you get a chance? This fixes a few issues that we've been dealing with in the IBM openapi validator project (https://github.com/IBM/openapi-validator). Thanks! |
I think this represents an enormous correction in how paths are reported from the core Spectral tool. Would love to see this in a release soon! |
apologies for the delay folks, I was on vacation. |
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.
Looks great, huge thanks for this one.
Since you've been waiting so long for the review, I didn't want my review to block it, thus I went ahead and added a few minor changes, mostly to use some utils we already have elsewhere in our libs.
commit a83422bae9b3dcf83e4604eb7c109bda0621872a
Author: Jakub Rożek <[email protected]>
Date: Mon Jul 18 09:01:12 2022 +0200
refactor(core): use stoplight/json utils
diff --git a/packages/core/src/documentInventory.ts b/packages/core/src/documentInventory.ts
index cdb68015..f449e981 100644
--- a/packages/core/src/documentInventory.ts
+++ b/packages/core/src/documentInventory.ts
@@ -1,4 +1,4 @@
-import { extractSourceFromRef, isLocalRef } from '@stoplight/json';
+import { decodePointerFragment, encodePointerFragment, extractSourceFromRef, isLocalRef } from '@stoplight/json';
import { extname, resolve } from '@stoplight/path';
import { Dictionary, IParserResult, JsonPath } from '@stoplight/types';
import { isObjectLike } from 'lodash';
@@ -113,16 +113,17 @@ export class DocumentInventory implements IDocumentInventory {
let resolvedDoc = this.document;
// Add '#' on the beginning of "path" to simplify the logic below.
- const adjustedPath: JsonPath = [...'#', ...path];
+ const adjustedPath: string[] = ['#', ...path.map(String)];
// Walk through the segments of 'path' one at a time, looking for
// json path locations containing a $ref.
let refMapKey = '';
for (const segment of adjustedPath) {
- if (refMapKey.length) {
- refMapKey = refMapKey.concat('/');
+ if (refMapKey.length > 0) {
+ refMapKey += '/';
}
- refMapKey = refMapKey.concat(segment.toString().replace(/\//g, '~1'));
+
+ refMapKey += encodePointerFragment(segment);
// If our current refMapKey value is in fact a key in refMap,
// then we'll "reverse-resolve" it by replacing refMapKey with
@@ -149,7 +150,7 @@ export class DocumentInventory implements IDocumentInventory {
// Update "resolvedDoc" to reflect the new "source" value and make sure we found an actual document.
const newResolvedDoc = source === this.document.source ? this.document : this.referencedDocuments[source];
- if (newResolvedDoc === null || newResolvedDoc === undefined) {
+ if (newResolvedDoc === null || newResolvedDoc === void 0) {
const item: DocumentInventoryItem = {
document: resolvedDoc,
path: getClosestJsonPath(resolvedDoc.data, path),
@@ -157,6 +158,7 @@ export class DocumentInventory implements IDocumentInventory {
};
return item;
}
+
resolvedDoc = newResolvedDoc;
// Update "refMap" to reflect the new "source" value.
@@ -174,25 +176,17 @@ export class DocumentInventory implements IDocumentInventory {
missingPropertyPath: [...closestPath, ...missingPropertyPath],
};
return item;
- } catch (e) {
- // console.warn(`Caught exception! e=${e}`);
+ } catch {
return null;
}
}
protected convertRefMapKeyToPath(refPath: string): JsonPath {
- const jsonPath: JsonPath = [];
-
if (refPath.startsWith('#/')) {
refPath = refPath.slice(2);
}
- const pathSegments: string[] = refPath.split('/');
- for (const pathSegment of pathSegments) {
- jsonPath.push(pathSegment.replace('~1', '/'));
- }
-
- return jsonPath;
+ return refPath.split('/').map(decodePointerFragment);
}
protected parseResolveResult: Resolver['parseResolveResult'] = resolveOpts => {
I tried to push that commit, but it appears I'm getting (permission denied)
, so I'd appreciate it if you could apply the above git patch.
Okay @P0lip I pushed the commit - let me know if there are any other issues! |
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.
Thank you all!
# [@stoplight/spectral-cli-v6.4.2](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-cli-v6.4.1...@stoplight/spectral-cli-v6.4.2) (2022-07-21) ### Bug Fixes * **core:** fix 'resolved vs unresolved' json path mapping ([#2202](#2202)) ([157ec59](157ec59))
🎉 This PR is included in version @stoplight/spectral-cli-v6.4.2 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
# [@stoplight/spectral-core-v1.12.4](https://github.com/stoplightio/spectral/compare/@stoplight/spectral-core-v1.12.3...@stoplight/spectral-core-v1.12.4) (2022-07-21) ### Bug Fixes * **core:** fix 'resolved vs unresolved' json path mapping ([#2202](#2202)) ([157ec59](157ec59))
🎉 This PR is included in version @stoplight/spectral-core-v1.12.4 🎉 The release is available on npm package (@latest dist-tag) Your semantic-release bot 📦🚀 |
This PR improves the DocumentInventory.findAssociatedItemForPath() function
by slightly changing the approach used to map a path from the resolved document
back to the corresponding path within the original unresolved document.
The result is a more accurate mapping due to the fact that the updated code
now more closely mimics the steps performed by the resolver when processing
the $refs, but just in reverse.
Fixes #2059
Fixes #2043
Checklist
Does this PR introduce a breaking change?