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

fix: correctly handle default values of deepObject query params #557

Merged
merged 1 commit into from
Mar 13, 2021
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
30 changes: 27 additions & 3 deletions src/middlewares/parsers/req.parameter.mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ export class RequestParameterMutator {
this.parseJsonAndMutateRequest(req, parameter.in, name);
this.handleFormExplode(req, name, <SchemaObject>schema, parameter);
} else if (style === 'deepObject') {
this.handleDeepObject(req, queryString, name);
this.handleDeepObject(req, queryString, name, schema);
} else {
this.parseJsonAndMutateRequest(req, parameter.in, name);
}
Expand All @@ -103,9 +103,33 @@ export class RequestParameterMutator {
});
}

private handleDeepObject(req: Request, qs: string, name: string): void {
private handleDeepObject(req: Request, qs: string, name: string, schema: SchemaObject): void {
const getDefaultSchemaValue = () => {
let defaultValue;

if (schema.default !== undefined) {
defaultValue = schema.default
} else {
['allOf', 'oneOf', 'anyOf'].forEach((key) => {
if (schema[key]) {
schema[key].forEach((s) => {
if (s.$ref) {
const compiledSchema = this.ajv.getSchema(s.$ref);
// as any -> https://stackoverflow.com/a/23553128
defaultValue = defaultValue === undefined ? (compiledSchema.schema as any).default : defaultValue;
} else {
defaultValue = defaultValue === undefined ? s.default : defaultValue;
}
});
}
});
}

return defaultValue;
};

if (!req.query?.[name]) {
req.query[name] = {};
req.query[name] = getDefaultSchemaValue();
}
this.parseJsonAndMutateRequest(req, 'query', name);
// TODO handle url encoded?
Expand Down
62 changes: 62 additions & 0 deletions test/resources/serialized-deep-object.objects.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
openapi: "3.0.2"
info:
version: 1.0.0
title: Request Query Serialization
description: Request Query Serialization Test

servers:
- url: /v1/

paths:
/deep_object:
x-vendorExtension1: accounts
get:
x-vendorExtension2: accounts
summary: "retrieve a deep object"
operationId: getDeepObject
parameters:
- in: query
style: deepObject
name: settings
schema:
type: object
required:
- state
properties:
tag_ids:
type: array
items:
type: integer
minimum: 0
minItems: 1
state:
type: string
enum: ["default", "validated", "pending"]
default: "default"
description: "Filter the tags by their validity. The default value ('default') stands for no filtering."
greeting:
type: string
default: "hello"
responses:
"200":
description: the object

components:
schemas:
Deep:
type: object
properties:
tag_ids:
type: array
items:
type: integer
minimum: 0
minItems: 1
state:
type: string
enum: ["default", "validated", "pending"]
default: "default"
description: "Filter the tags by their validity. The default value ('default') stands for no filtering."
greeting:
type: string
default: "hello"
2 changes: 2 additions & 0 deletions test/resources/serialized.objects.defaults.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ components:
default: 25
type: integer
type: object
default: {}
Sorting:
properties:
field:
Expand All @@ -29,6 +30,7 @@ components:
- DESC
type: string
type: object
default: {}
info:
description: API
title: API
Expand Down
50 changes: 50 additions & 0 deletions test/serialized-deep-object.objects.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import * as path from 'path';
import * as express from 'express';
import * as request from 'supertest';
import * as packageJson from '../package.json';
import { expect } from 'chai';
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-deep-object.objects.yaml');
app = await createApp({ apiSpec }, 3005, (app) =>
app.use(
`${app.basePath}`,
express
.Router()
.get(`/deep_object`, (req, res) => res.json(req.query))
),
);
});

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

it('should explode deepObject query params', async () =>
request(app)
.get(`${app.basePath}/deep_object?settings[state]=default`)
.expect(200)
.then((r) => {
const expected = {
settings: {
greeting: 'hello',
state: 'default'
}
};
expect(r.body).to.deep.equals(expected);
}));

it('should explode deepObject query params (optional query param)', async () =>
request(app)
.get(`${app.basePath}/deep_object`)
.expect(200)
.then((r) => {
const expected = {};
expect(r.body).to.deep.equals(expected);
}));
});