From 28f8b65ce3504abed49f38727b921c01ab0cf496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jakub=20Ro=C5=BCek?= Date: Mon, 30 Sep 2019 14:11:16 +0200 Subject: [PATCH] fix(resolving): decode path to produce proper ranges --- .../__fixtures__/draft-nested-ref.oas2.json | 5 ++ .../__tests__/__fixtures__/refs/paths.json | 9 ++++ src/cli/services/__tests__/linter.test.ts | 48 +++++++++++++++++++ src/resolved.ts | 4 +- 4 files changed, 64 insertions(+), 2 deletions(-) create mode 100644 src/cli/services/__tests__/__fixtures__/refs/paths.json diff --git a/src/cli/services/__tests__/__fixtures__/draft-nested-ref.oas2.json b/src/cli/services/__tests__/__fixtures__/draft-nested-ref.oas2.json index 441e923e9d..da6d298de8 100644 --- a/src/cli/services/__tests__/__fixtures__/draft-nested-ref.oas2.json +++ b/src/cli/services/__tests__/__fixtures__/draft-nested-ref.oas2.json @@ -3,5 +3,10 @@ "host": "d", "info": { "$ref": "./refs/info-ref.json" + }, + "paths": { + "/test": { + "$ref": "./refs/paths.json#/paths/~1test" + } } } diff --git a/src/cli/services/__tests__/__fixtures__/refs/paths.json b/src/cli/services/__tests__/__fixtures__/refs/paths.json new file mode 100644 index 0000000000..fceaaf76d5 --- /dev/null +++ b/src/cli/services/__tests__/__fixtures__/refs/paths.json @@ -0,0 +1,9 @@ +{ + "paths": { + "/test": { + "get": { + "response": "bar" + } + } + } +} diff --git a/src/cli/services/__tests__/linter.test.ts b/src/cli/services/__tests__/linter.test.ts index 1b518bb94b..b7602bd1e2 100644 --- a/src/cli/services/__tests__/linter.test.ts +++ b/src/cli/services/__tests__/linter.test.ts @@ -380,6 +380,54 @@ describe('Linter service', () => { }, source: expect.stringContaining('__tests__/__fixtures__/refs/contact.json'), }), + expect.objectContaining({ + code: 'operation-description', + message: 'Operation `description` must be present and non-empty string.', + path: ['paths', '/test', 'get'], + range: { + end: { + character: 7, + line: 5, + }, + start: { + character: 13, + line: 3, + }, + }, + source: expect.stringContaining('__tests__/__fixtures__/refs/paths.json'), + }), + expect.objectContaining({ + code: 'operation-operationId', + message: 'Operation should have an `operationId`.', + path: ['paths', '/test', 'get'], + range: { + end: { + character: 7, + line: 5, + }, + start: { + character: 13, + line: 3, + }, + }, + source: expect.stringContaining('__tests__/__fixtures__/refs/paths.json'), + }), + expect.objectContaining({ + code: 'operation-tags', + message: 'Operation should have non-empty `tags` array.', + path: ['paths', '/test', 'get'], + range: { + end: { + character: 7, + line: 5, + }, + start: { + character: 13, + line: 3, + }, + }, + source: expect.stringContaining('__tests__/__fixtures__/refs/paths.json'), + }), ]); }); }); diff --git a/src/resolved.ts b/src/resolved.ts index 2579abe5b8..202f5b3ef6 100644 --- a/src/resolved.ts +++ b/src/resolved.ts @@ -1,4 +1,4 @@ -import { pointerToPath } from '@stoplight/json'; +import { decodePointerFragment, pointerToPath } from '@stoplight/json'; import { IResolveError } from '@stoplight/json-ref-resolver/types'; import { Dictionary, ILocation, IRange, JsonPath, Segment } from '@stoplight/types'; import { get } from 'lodash'; @@ -72,7 +72,7 @@ export class Resolved { if (target && target[REF_METADATA]) { return { - path: [...get(target, [REF_METADATA, 'root'], []), ...newPath], + path: [...get(target, [REF_METADATA, 'root'], []).map(decodePointerFragment), ...newPath], doc: get(this.parsedMap.parsed, get(target, [REF_METADATA, 'ref']), this.spec), }; }