Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: create test for request.query.settings should be object #125 #126

Merged
merged 5 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/middlewares/openapi.request.validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,16 @@ export class RequestValidator {
if (parameter.content && parameter.content[TYPE_JSON]) {
parameterSchema = parameter.content[TYPE_JSON].schema;
parseJson.push({ name, reqField });
} else if (
// handle complex json types in schema
$in === 'query' &&
(parameterSchema.allOf ||
parameterSchema.oneOf ||
parameterSchema.anyOf ||
(parameterSchema.type === 'object' &&
parameterSchema.type !== 'array'))
) {
parseJson.push({ name, reqField });
}

if (!parameterSchema) {
Expand Down
52 changes: 52 additions & 0 deletions test/resources/serialized.objects.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@

openapi: '3.0.2'
info:
version: 1.0.0
title: requestBodies $ref
description: requestBodies $ref Test

servers:
- url: /v1/

paths:
/serialisable:
get:
summary: "Retrieve something"
parameters:
- in: query
style: form
name: settings
explode: true
schema:
allOf:
- type: object
properties:
onlyValidated:
type: boolean
default: true
onlySelected:
type: array
default: []
uniqueItems: true
items:
type: integer
minimum: 0
example: 42
responses:
'200':
description: "An array of items"
content:
application/json:
schema:
type: array
items:
$ref: "#/components/schemas/someItem"
uniqueItems: true

components:
schemas:
someItem:
type: object
properties:
id:
type: string
34 changes: 34 additions & 0 deletions test/serialized.objects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as path from 'path';
import * as express from 'express';
import * as request from 'supertest';
import * as packageJson from '../package.json';
import { createApp } from './common/app';

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

before(async () => {
// Set up the express app
const apiSpec = path.join('test', 'resources', 'serialized.objects.yaml');
app = await createApp({ apiSpec }, 3005, app =>
app.use(
`${app.basePath}`,
express
.Router()
.get(`/serialisable`, (req, res) => res.json({ id: 'test' })),
),
);
});

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

it('should deserialize object', async () =>
request(app)
.get(`${app.basePath}/serialisable`)
.query({
settings: '{"onlyValidated":true}',
})
.expect(200));
});