Skip to content

Commit

Permalink
fix: backport v5 operationId accessor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
erunion committed Aug 2, 2022
1 parent 5c34902 commit 1b78b39
Show file tree
Hide file tree
Showing 7 changed files with 948 additions and 542 deletions.
691 changes: 425 additions & 266 deletions package-lock.json

Large diffs are not rendered by default.

24 changes: 24 additions & 0 deletions packages/api/__tests__/__fixtures__/operation-ids.oas.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"openapi": "3.0.0",
"info": {
"version": "1.0.0",
"title": "Single Path"
},
"servers": [
{
"url": "https://httpbin.org"
}
],
"paths": {
"/anything": {
"get": {
"operationId": "get-pet",
"responses": {
"200": {
"description": "OK"
}
}
}
}
}
}
34 changes: 32 additions & 2 deletions packages/api/__tests__/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const examplesDir = path.join(__dirname, 'examples');

let petstoreSdk;
let readmeSdk;
let operationIdsSDK;
const petstoreServerUrl = 'http://petstore.swagger.io/api';

beforeEach(async () => {
Expand All @@ -26,6 +27,10 @@ beforeEach(async () => {
require.resolve('@readme/oas-examples/3.0/json/readme.json'),
'utf8'
),
[`${[examplesDir]}/operation-ids.json`]: await realFs.readFile(
require.resolve('./__fixtures__/operation-ids.oas.json'),
'utf8'
),
[`${[examplesDir]}/uspto.json`]: await realFs.readFile(
require.resolve('@readme/oas-examples/3.0/json/uspto.json'),
'utf8'
Expand All @@ -39,6 +44,10 @@ beforeEach(async () => {
const readme = path.join(examplesDir, 'readme.json');
await new Cache(readme).saveFile();
readmeSdk = api(readme);

const operationIds = path.join(examplesDir, 'operation-ids.json');
await new Cache(readme).saveFile();
operationIdsSDK = api(operationIds);
});

afterEach(() => {
Expand Down Expand Up @@ -79,8 +88,11 @@ describe('#preloading', () => {
'head',
'patch',
'trace',
'listDataSets',
'list-data-sets',
'listSearchableFields',
'list-searchable-fields',
'performSearch',
'perform-search',
]);

Expand Down Expand Up @@ -113,8 +125,26 @@ describe('#accessors', () => {
}).not.toThrow();
});

it('should work with operationIds that have contain spaces', () => {
expect(typeof petstoreSdk['find pet by id']).toBe('function');
it('should work with operationIds that have contain spaces', async () => {
const mock = nock(petstoreServerUrl).get('/pets/1234').twice().reply(200, 'it worked!');

await expect(petstoreSdk['find pet by id']({ id: 1234 })).resolves.toBe('it worked!');

// Because we don't want people using ugly operationIDs like the above we transform them into
// JS-friendly method accessors also.
await expect(petstoreSdk.findPetById({ id: 1234 })).resolves.toBe('it worked!');
mock.done();
});

it('should work with operationIds that contain hyphens', async () => {
const mock = nock('https://httpbin.org').get('/anything').twice().reply(200, 'it worked!');

await expect(operationIdsSDK['get-pet']()).resolves.toBe('it worked!');

// Because we don't want people using ugly operationIDs like the above we transform them into
// JS-friendly method accessors also.
await expect(operationIdsSDK.getPet()).resolves.toBe('it worked!');
mock.done();
});

it('should work for other methods', () => {
Expand Down
Loading

0 comments on commit 1b78b39

Please sign in to comment.