Skip to content

Commit

Permalink
fix: hash ending path resolution fails (Redocly#1408)
Browse files Browse the repository at this point in the history
  • Loading branch information
maininformer authored Feb 5, 2024
1 parent 8bff6a8 commit 4ab6e09
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 3 deletions.
6 changes: 6 additions & 0 deletions .changeset/soft-planets-watch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@redocly/openapi-core": patch
"@redocly/cli": patch
---

Fixed an issue where `$ref`s ending in `#` (instead of `#/`) would break the application.
10 changes: 10 additions & 0 deletions __tests__/bundle/reference-ending-in-hash/openapi.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
openapi: 3.0.3
paths:
/pets/{petId}:
get:
responses:
'200':
content:
application/json:
schema:
$ref: './schemas/Pet.json#'
3 changes: 3 additions & 0 deletions __tests__/bundle/reference-ending-in-hash/redocly.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
apis:
main:
root: ./openapi.yaml
1 change: 1 addition & 0 deletions __tests__/bundle/reference-ending-in-hash/schemas/Pet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{}
21 changes: 21 additions & 0 deletions __tests__/bundle/reference-ending-in-hash/snapshot.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`E2E bundle reference-ending-in-hash 1`] = `
openapi: 3.0.3
paths:
/pets/{petId}:
get:
responses:
'200':
content:
application/json:
schema:
$ref: '#/components/schemas/Pet'
components:
schemas:
Pet: {}
bundling ./openapi.yaml...
📦 Created a bundle for ./openapi.yaml at stdout <test>ms.
`;
6 changes: 3 additions & 3 deletions packages/core/src/ref-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,15 @@ export function escapePointer<T extends string | number>(fragment: T): T {
}

export function parseRef(ref: string): { uri: string | null; pointer: string[] } {
const [uri, pointer] = ref.split('#/');
const [uri, pointer = ''] = ref.split('#');
return {
uri: uri || null,
pointer: pointer ? pointer.split('/').map(unescapePointer).filter(isTruthy) : [],
pointer: parsePointer(pointer),
};
}

export function parsePointer(pointer: string) {
return pointer.substr(2).split('/').map(unescapePointer);
return pointer.split('/').map(unescapePointer).filter(isTruthy);
}

export function pointerBaseName(pointer: string) {
Expand Down

0 comments on commit 4ab6e09

Please sign in to comment.