Skip to content

Commit

Permalink
Merge pull request #27 from paulRbr/fix-accented-filesystem-paths
Browse files Browse the repository at this point in the history
fix: resolve accented filesystem path correctly in $RefParser lib
  • Loading branch information
paulRbr authored May 26, 2021
2 parents c29f816 + 7448033 commit fe8c7f5
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 17 deletions.
16 changes: 16 additions & 0 deletions examples/valid/__gitlab-é__.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
openapi: 3.0.0
info:
description: |
An OpenAPI definition for the GitLab REST API.
version: v4
title: GitLab API

paths:
# ...


# ACCESS REQUESTS (PROJECTS)
/v4/projects/{id}/access_requests:
$ref: 'v4/access_requests.yaml#/accessRequestsProjects'

# ...
12 changes: 12 additions & 0 deletions examples/valid/v4/access_requests.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
accessRequestsProjects:
get:
description: Lists access requests for a project
parameters:
- name: id
in: path
responses:
'200':
description: Successful operation
content:
'application/json':
$ref: 'models/ProjectAccessResponse.yaml'
11 changes: 11 additions & 0 deletions examples/valid/v4/models/ProjectAccessResponse.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
schema:
title: ProjectAccessResponse
type: object
properties:
id:
type: integer
usename:
type: string
example:
- "id": 1
"username": "raymond_smith"
26 changes: 9 additions & 17 deletions src/definition.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,18 @@ class API {
}

resolveContent($refs: $RefParser.$Refs): [string, APIDefinition] {
const paths = $refs.paths();
let mainReference;
let absPath = paths.shift();
const values = $refs.values();
let mainReference: JSONSchemaWithRaw = { parsed: {}, raw: '' };

while (typeof absPath !== 'undefined') {
for (const [absPath, reference] of Object.entries(values)) {
if (absPath === this.location || absPath === path.resolve(this.location)) {
mainReference = absPath;
// $refs.values is not properly typed so we need to force it
// with the resulting type of our custom defined parser
mainReference = reference as JSONSchemaWithRaw;
} else {
// $refs.get is not properly typed so we need to force it
// $refs.values is not properly typed so we need to force it
// with the resulting type of our custom defined parser
const { raw } = $refs.get(absPath) as JSONSchemaWithRaw;
const { raw } = reference as JSONSchemaWithRaw;

if (!raw) {
throw new UnsupportedFormat('Reference ${absPath} is empty');
Expand All @@ -135,18 +136,9 @@ class API {
content: raw,
});
}
absPath = paths.shift();
}

if (typeof mainReference === 'undefined') {
throw new UnsupportedFormat(
"JSON Schema $ref parser couldn't parse the main definition",
);
}

// $refs.get is not properly typed so we need to force it
// with the resulting type of our custom defined parser
const { raw, parsed } = $refs.get(mainReference) as JSONSchemaWithRaw;
const { raw, parsed } = mainReference;

if (!parsed || !(parsed instanceof Object) || !('info' in parsed)) {
throw new UnsupportedFormat(
Expand Down
7 changes: 7 additions & 0 deletions test/unit/definition.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ describe('API definition class', () => {
});
});

describe('with a file path containing special characters', () => {
test.it('parses successfully', async () => {
const api = await API.loadAPI('./examples/valid/__gitlab-é__.yml');
expect(api.version).to.equal('3.0.0');
});
});

describe('with an http file containing relative URL refs', () => {
test
.nock('http://example.org', (api) =>
Expand Down

0 comments on commit fe8c7f5

Please sign in to comment.