Skip to content

Commit

Permalink
Merge pull request #37 from trebler/fix-path-common-parameters
Browse files Browse the repository at this point in the history
Add path-level parameters handling
  • Loading branch information
cdimascio committed Aug 19, 2019
2 parents 0bb25be + 00ab580 commit f80d459
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/openapi.spec.loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ export class OpenApiSpecLoader {
const bp = bpa.replace(/\/$/, '');
for (const [path, methods] of Object.entries(apiDoc.paths)) {
for (const [method, schema] of Object.entries(methods)) {
if (method === 'parameters') {
continue;
}
const schemaParameters = new Set();
(schema.parameters || []).forEach(parameter => schemaParameters.add(parameter));
((methods as any).parameters || []).forEach(parameter => schemaParameters.add(parameter));
schema.parameters = Array.from(schemaParameters);
const pathParams = new Set();
for (const param of schema.parameters || []) {
for (const param of schema.parameters) {
if (param.in === 'path') {
pathParams.add(param.name);
}
Expand Down
80 changes: 80 additions & 0 deletions test/path.level.parameters.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import * as path from 'path';
import * as express from 'express';
import { expect } from 'chai';
import * as request from 'supertest';
import { createApp } from './common/app';

const packageJson = require('../package.json');

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

before(async () => {
// Set up the express app
const apiSpec = path.join(
'test',
'resources',
'path.level.parameters.yaml',
);
app = await createApp({ apiSpec }, 3005);
basePath = app.basePath;

// Define new coercion routes
app.use(
`${basePath}`,
express
.Router()
.get(`/path_level_parameters`, (_req, res) => res.send())
);
});

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

it('should return 400 if pathLevel query parameter is not provided', async () =>
request(app)
.get(`${basePath}/path_level_parameters?operationLevel=123`)
.send()
.expect(400)
.then(r => {
expect(r.body.errors).to.be.an('array')
expect(r.body.errors).to.have.length(1);
const message = r.body.errors[0].message;
expect(message).to.equal('should have required property \'pathLevel\'');
}));

it('should return 400 if operationLevel query parameter is not provided', async () =>
request(app)
.get(`${basePath}/path_level_parameters?pathLevel=123`)
.send()
.expect(400)
.then(r => {
expect(r.body.errors).to.be.an('array')
expect(r.body.errors).to.have.length(1);
const message = r.body.errors[0].message;
expect(message).to.equal('should have required property \'operationLevel\'');
}));

it('should return 400 if neither operationLevel, nor pathLevel query parameters are provided', async () =>
request(app)
.get(`${basePath}/path_level_parameters`)
.send()
.expect(400)
.then(r => {
expect(r.body.errors).to.be.an('array')
expect(r.body.errors).to.have.length(2);
const messages = r.body.errors.map(err => err.message);
expect(messages).to.have.members([
'should have required property \'pathLevel\'',
'should have required property \'operationLevel\''
]);
}));

it('should return 200 if both pathLevel and operationLevel query parameter are provided', async () =>
request(app)
.get(`${basePath}/path_level_parameters?operationLevel=123&pathLevel=123`)
.send()
.expect(200));
});
36 changes: 36 additions & 0 deletions test/resources/path.level.parameters.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
openapi: '3.0.2'
info:
version: 1.0.0
title: Path Level Parameters
description: Path Level Parameters Test

servers:
- url: /v1/

paths:
/path_level_parameters:
parameters:
- $ref: '#components/parameters/pathLevelParameter'
get:
parameters:
- $ref: '#components/parameters/operationLevelParameter'
responses:
'200':
description: OK
'400':
description: Bad Request

components:
parameters:
pathLevelParameter:
name: pathLevel
in: query
required: true
schema:
type: string
operationLevelParameter:
name: operationLevel
in: query
required: true
schema:
type: string

0 comments on commit f80d459

Please sign in to comment.