Skip to content

Commit

Permalink
fix(core): pointer in overrides are applied too broadly (#2511)
Browse files Browse the repository at this point in the history
  • Loading branch information
P0lip authored Jul 18, 2023
1 parent 91bdc88 commit 69403c1
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
4 changes: 3 additions & 1 deletion docs/guides/4d-overrides.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,15 @@ JSON Pointers have a different syntax than JSON Paths used in the `given` compon
In JSON Pointers, path components are prefixed with a `/` and then concatenated to form the pointer.

Since `/` has a special meaning in JSON pointer, it must be encoded as `~1` when it appears in a component, and `~` must be encoded as `~0`.
JSON Pointer must be percent-encoded for use within a URI as specified by the [spec](https://datatracker.ietf.org/doc/html/rfc6901#section-6)

You can test JSON Pointer expressions in the [JSON Query online evaluator](https://www.jsonquerytool.com/) by choosing "JSONPointer" as the Transform.
Bear in mind the tool above expects plain JSON Pointer, thus you need to decode any previously percent-encoded characters.

```yaml
overrides:
- files:
- "legacy/**/*.oas.json#/paths/~1Pets~1{petId}/get/parameters/0"
- "legacy/**/*.oas.json#/paths/~1Pets~1%7BpetId%7D/get/parameters/0"
rules:
some-inherited-rule: "off"
```
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/__tests__/linter.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,9 @@ responses:: !!foo
bar: {
type: 'number',
},
bars: {
type: 'number',
},
}),
Parsers.Json,
documentUri,
Expand All @@ -1583,6 +1586,11 @@ responses:: !!foo
path: ['bar', 'type'],
severity: DiagnosticSeverity.Hint,
}),
expect.objectContaining({
code: 'valid-type',
path: ['bars', 'type'],
severity: DiagnosticSeverity.Error,
}),
]);
});
});
Expand Down
5 changes: 4 additions & 1 deletion packages/core/src/ruleset/rule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,10 @@ export class Rule implements IRule {

for (const relevantOverride of relevantOverrides) {
for (const [overridePath, overrideSeverity] of relevantOverride.entries()) {
if (overridePath.length >= closestPointer.length && pointer.startsWith(overridePath)) {
if (
overridePath.length >= closestPointer.length &&
(pointer === overridePath || pointer.startsWith(`${overridePath}/`))
) {
closestPointer = overridePath;
severity = overrideSeverity;
}
Expand Down

0 comments on commit 69403c1

Please sign in to comment.