Skip to content

Commit

Permalink
(fix) cdimascio#415 Cannot read property 'push' of undefined bug
Browse files Browse the repository at this point in the history
  • Loading branch information
ex1st committed Oct 26, 2020
1 parent 2ee83f2 commit 993bfb3
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 7 deletions.
11 changes: 6 additions & 5 deletions src/middlewares/parsers/request.schema.preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,17 @@ export class RequestSchemaPreprocessor {

if (parameters.length === 0) return;

const v = pathItem[pathItemKey];
let v = pathItem[pathItemKey];
if (v === parameters) return;
const ref = v?.parameters?.$ref;

const operationParameters = <
Array<OpenAPIV3.ParameterObject | OpenAPIV3.ReferenceObject>
>(ref ? this.ajv.getSchema(ref)?.schema : v.parameters);
const op = ref && this.ajv.getSchema(ref)?.schema;
if (op)
v = op;
v.parameters = v.parameters || [];

for (const param of parameters) {
operationParameters.push(param);
v.parameters.push(param);
}
}

Expand Down
12 changes: 10 additions & 2 deletions test/path.params.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('path params', () => {
},
3005,
(app) => {
app.get(`${app.basePath}/users/:id?`, (req, res) => {
app.get([`${app.basePath}/users/:id?`, `${app.basePath}/users_alt/:id?`], (req, res) => {
res.json({
id: req.params.id,
});
Expand Down Expand Up @@ -51,14 +51,22 @@ describe('path params', () => {
app.server.close();
});

it('should url decode path parameters', async () =>
it('should url decode path parameters (type level)', async () =>
request(app)
.get(`${app.basePath}/users/c%20dimascio`)
.expect(200)
.then((r) => {
expect(r.body.id).to.equal('c dimascio');
}));

it('should url decode path parameters (path level)', async () =>
request(app)
.get(`${app.basePath}/users_alt/c%20dimascio`)
.expect(200)
.then((r) => {
expect(r.body.id).to.equal('c dimascio');
}));

it('should handle path parameter with style=simple', async () =>
request(app)
.get(`${app.basePath}/multi_users/aa,bb,cc`)
Expand Down
15 changes: 15 additions & 0 deletions test/resources/path.params.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,21 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/User"
/users_alt/{id}:
parameters:
- name: id
in: path
required: true
schema:
type: string
get:
responses:
200:
description: ""
content:
application/json:
schema:
$ref: "#/components/schemas/User"
/multi_users/{ids}:
get:
summary: Deletes Features given a number of uuids.
Expand Down

0 comments on commit 993bfb3

Please sign in to comment.