Skip to content

Commit

Permalink
Handle spaces in file path for capture (#2652)
Browse files Browse the repository at this point in the history
  • Loading branch information
niclim authored Jan 9, 2024
1 parent 48adf9b commit 8001d02
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 8 deletions.
14 changes: 8 additions & 6 deletions projects/openapi-io/src/parser/sourcemap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,19 @@ export class JsonSchemaSourcemap {
}

logPointer(pathRelativeToFile: string, pathRelativeToRoot: string) {
const relativePathDecoded =
jsonPointerHelpers.unescapeUriSafePointer(pathRelativeToFile);
const rootKey = jsonPointerHelpers.unescapeUriSafePointer(
pathRelativeToRoot.substring(1)
);

const thisFile = this.files.find((i) =>
pathRelativeToFile.startsWith(i.path)
relativePathDecoded.startsWith(i.path)
);

if (thisFile) {
const rootKey = jsonPointerHelpers.unescapeUriSafePointer(
pathRelativeToRoot.substring(1)
);

const jsonPointer = jsonPointerHelpers.unescapeUriSafePointer(
pathRelativeToFile.split(thisFile.path)[1].substring(1) || '/'
relativePathDecoded.split(thisFile.path)[1].substring(1) || '/'
);

if (rootKey === jsonPointer) return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,6 @@ export function sourcemapReader(sourcemap: SerializedSourcemap) {
} else {
cursor.pathInCurrentFile.push(component);
}

// console.log(cursor);
});

const file = sourcemap.files.find((i) => i.index === cursor.currentFile)!;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1726,6 +1726,71 @@ exports[`capture with requests update behavior handles update in other file 3`]
"
`;

exports[`capture with requests update behavior handles update in other file with spaces 1`] = `
"Generating traffic to send to server
GET /books
✓ 200 response
[200 response body] 'name' has been added (/properties/books/items/properties/name)
[200 response body] 'author_id' has been added (/properties/books/items/properties/author_id)
[200 response body] 'status' has been added (/properties/books/items/properties/status)
[200 response body] 'price' has been added (/properties/books/items/properties/price)
[200 response body] 'created_at' has been added (/properties/books/items/properties/created_at)
[200 response body] 'updated_at' has been added (/properties/books/items/properties/updated_at)
"
`;

exports[`capture with requests update behavior handles update in other file with spaces 2`] = `
"openapi: 3.0.3
info:
title: a spec
description: The API
version: 0.1.0
paths:
/books:
get:
responses:
"200":
description: 200 response
content:
application/json:
schema:
$ref: ./with space/books.yml#/GetBooks200ResponseBody
"
`;

exports[`capture with requests update behavior handles update in other file with spaces 3`] = `
"GetBooks200ResponseBody:
type: object
properties:
books:
type: array
items:
type: object
properties:
id:
type: string
name:
type: string
author_id:
type: string
status:
type: string
price:
type: number
created_at:
type: string
updated_at:
type: string
required:
- name
- author_id
- status
- price
- created_at
- updated_at
"
`;

exports[`capture with requests update behavior respects x-optic-path-ignore 1`] = `
"Generating traffic to send to server
GET /authors
Expand Down
24 changes: 24 additions & 0 deletions projects/optic/src/__tests__/integration/capture.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,30 @@ describe('capture with requests', () => {
)
).toMatchSnapshot();
});

test('handles update in other file with spaces', async () => {
const workspace = await setupWorkspace('capture/with-server');
await setPortInFile(workspace, 'optic.yml');

const { combined, code } = await runOptic(
workspace,
'capture openapi-with-external-ref-spaces.yml --update'
);
expect(normalizeWorkspace(workspace, combined)).toMatchSnapshot();
expect(code).toBe(0);
expect(
await fs.readFile(
path.join(workspace, 'openapi-with-external-ref-spaces.yml'),
'utf-8'
)
).toMatchSnapshot();
expect(
await fs.readFile(
path.join(workspace, './with space/books.yml'),
'utf-8'
)
).toMatchSnapshot();
});
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
openapi: 3.0.3
info:
title: a spec
description: The API
version: 0.1.0
paths:
/books:
get:
responses:
"200":
description: 200 response
content:
application/json:
schema:
$ref: ./with space/books.yml#/GetBooks200ResponseBody
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,16 @@ capture:
send:
- path: /books
method: GET
openapi-with-external-ref-spaces.yml:
server:
command: node server.js
url: http://localhost:%PORT
ready_endpoint: /healthcheck
ready_timeout: 5000
requests:
send:
- path: /books
method: GET
openapi-with-server-prefix.yml:
server:
command: node server.js
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
GetBooks200ResponseBody:
type: object
properties:
books:
type: array
items:
type: object
properties:
id:
type: string

0 comments on commit 8001d02

Please sign in to comment.