Skip to content

Commit

Permalink
Fix path params (#94)
Browse files Browse the repository at this point in the history
* oas3-chow-chow never sets validRequest.path.params

* added changeset

Co-authored-by: Chris Olson <[email protected]>
  • Loading branch information
crizo23 and Chris Olson authored Sep 26, 2021
1 parent f0fa68b commit 7b113c2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/rude-ducks-tickle.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"koa-oas3": patch
---

Fix setting of ctx.oas.request.path
18 changes: 17 additions & 1 deletion __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ describe('Koa Oas3 with ChowOptions', () => {
file: path.resolve('./__tests__/fixtures/pet-store.json'),
endpoint: '/openapi',
uiEndpoint: '/openapi.html',
validatePaths: ['/pets'],
validatePaths: ['/'],
validationOptions: { requestBodyAjvOptions: { allErrors: true } } as ChowOptions
});
})
Expand Down Expand Up @@ -407,4 +407,20 @@ describe('Koa Oas3 with ChowOptions', () => {
return expect(mw(ctx, next)).rejects.toThrow();
})

test('ctx.oas.request.params populated with path param if validation passed', async () => {
const pathParam = 345;

const ctx = createContext({
url: `/pets/${pathParam}`,
headers: {
'accept': 'application/json'
},
method: 'GET'
});
const next = jest.fn();
await mw(ctx, next);
console.log(`ctx.oas: ${JSON.stringify(ctx.oas)}`)
expect(ctx.oas!.request.params.petId).toBe(pathParam);
});

})
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export async function oas(cfg: Partial<Config>): Promise<koa.Middleware> {
ctx.oas = {
request: {
query: validRequest.query,
params: validRequest.path && validRequest.path.params,
params: validRequest.path,
header: validRequest.header,
},
operationId: validRequest.operationId
Expand Down

0 comments on commit 7b113c2

Please sign in to comment.