Skip to content

Commit

Permalink
fix: cannot create property parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
cdimascio committed Nov 5, 2020
1 parent 52cfe01 commit 43dcfda
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/middlewares/parsers/request.schema.preprocessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ type SchemaObject = OpenAPIV3.SchemaObject;
type ReferenceObject = OpenAPIV3.ReferenceObject;
type Schema = ReferenceObject | SchemaObject;

const httpMethods = new Set([
'get',
'put',
'post',
'delete',
'options',
'head',
'patch',
'trace',
]);
export class RequestSchemaPreprocessor {
private ajv: Ajv;
private apiDoc: OpenAPIV3.Document;
Expand All @@ -24,8 +34,10 @@ export class RequestSchemaPreprocessor {
? <OpenAPIV3.PathItemObject>this.ajv.getSchema(piOrRef.$ref).schema
: piOrRef;
for (const pathItemKey of Object.keys(pathItem)) {
this.preprocessRequestBody(pathItemKey, pathItem);
this.preprocessPathLevelParameters(pathItemKey, pathItem);
if (httpMethods.has(pathItemKey)) {
this.preprocessRequestBody(pathItemKey, pathItem);
this.preprocessPathLevelParameters(pathItemKey, pathItem);
}
}
});
}
Expand Down Expand Up @@ -62,8 +74,7 @@ export class RequestSchemaPreprocessor {
const ref = v?.parameters?.$ref;

const op = ref && this.ajv.getSchema(ref)?.schema;
if (op)
v = op;
if (op) v = op;
v.parameters = v.parameters || [];

for (const param of parameters) {
Expand Down
69 changes: 69 additions & 0 deletions test/440.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// expressOpenapiValidator.middleware({
// apiSpec: apiSpec,
// validateRequests: true,
// validateResponses: true,
// });

import * as express from 'express';
import * as request from 'supertest';
import { createApp } from './common/app';
import * as packageJson from '../package.json';
import { OpenAPIV3 } from '../src/framework/types';

describe(packageJson.name, () => {
let app = null;

before(async () => {
// Set up the express app
const apiSpec: OpenAPIV3.Document = {
openapi: '3.0.0',
info: { title: 'Api test', version: '1.0.0' },
servers: [{ url: '/api' }],
paths: {
'/test/{id}': {
description: 'Description',
parameters: [
{
name: 'id',
in: 'path',
required: true,
schema: { type: 'string' },
},
],
get: {
responses: {
'200': {
description: 'response',
content: {
'application/json': {
schema: {
type: 'object',
properties: {
id: { type: 'string' },
label: { type: 'string' },
},
},
},
},
},
},
},
},
},
};
app = await createApp({ apiSpec }, 3005, (app) =>
app.use(
express
.Router()
.post(`/test/abc123`, (req, res) => res.status(200).json(req.body)),
),
);
});

after(() => {
app.server.close();
});

it('create campaign should return 200', async () =>
request(app).post(`/test/abc123`).send({ id: 'abc123' }).expect(200));
});

0 comments on commit 43dcfda

Please sign in to comment.