Skip to content
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): pointer in overrides are applied too broadly #2511

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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