Skip to content

Commit

Permalink
add deepObject test
Browse files Browse the repository at this point in the history
  • Loading branch information
Carmine DiMascio committed Dec 29, 2019
1 parent b300058 commit 3a7892a
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 10 deletions.
13 changes: 11 additions & 2 deletions src/middlewares/parsers/req.parameter.mutator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ type Schema = ReferenceObject | SchemaObject;
type Parameter = ReferenceObject | ParameterObject;

/**
* A class top parse incoing parameters and populate a list of request fields e.g. id and field types e.g. query
* whose value must later be parsed as a JSON object, JSON Exploded Object, JSON Array, or JSON Exploded Array
* A class top parse and mutate the incoming request parameters according to the openapi spec.
* the request is mutated to accomodate various styles and types e.g. form, explode, deepObject, etc
*/
export class RequestParameterMutator {
private _apiDocs: OpenAPIV3.Document;
Expand Down Expand Up @@ -61,13 +61,17 @@ export class RequestParameterMutator {
const { name, schema } = normalizeParameter(parameter);
const { type } = <SchemaObject>schema;
const { style, explode } = parameter;
const i = req.originalUrl.indexOf('?');
const queryString = req.originalUrl.substr(i + 1);

if (parameter.content) {
this.handleContent(req, name, parameter);
} else if (parameter.in === 'query' && this.isObjectOrXOf(schema)) {
this.parseJsonAndMutateRequest(req, parameter.in, name);
if (style === 'form' && explode) {
this.handleFormExplode(req, name, <SchemaObject>schema, parameter);
} else if (parameter.in === 'query' && style === 'deepObject') {
this.handleDeepObject(req, queryString, name);
}
} else if (type === 'array' && !explode) {
const delimiter = ARRAY_DELIMITER[parameter.style];
Expand All @@ -81,6 +85,11 @@ export class RequestParameterMutator {
});
}

private handleDeepObject(req: Request, qs: string, name: string) {
// nothing to do
// TODO handle url encoded?
}

private handleContent(
req: Request,
name: string,
Expand Down
36 changes: 31 additions & 5 deletions test/resources/serialized.objects.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

openapi: '3.0.2'
openapi: "3.0.2"
info:
version: 1.0.0
title: Request Query Serialization
Expand All @@ -9,6 +8,33 @@ servers:
- url: /v1/

paths:
/deep_object:
get:
summary: "retrieve a deep object"
operationId: getDeepObject
parameters:
- in: query
style: deepObject
explode: true
name: settings
schema:
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."
responses:
"200":
description: the object

/tags:
get:
summary: "Retrieve all tags"
Expand All @@ -33,7 +59,7 @@ paths:
default: "default"
description: "Filter the tags by their validity. The default value ('default') stands for no filtering."
responses:
'200':
"200":
description: "An array of tag"
/serialisable:
get:
Expand Down Expand Up @@ -64,7 +90,7 @@ paths:
description: Should not be serialized
schema:
description: Value passed to Javascript's `new Date()`.
example: '2019-06-24T12:34:56.789Z'
example: "2019-06-24T12:34:56.789Z"
anyOf:
- type: integer
description: Unix milliseconds
Expand All @@ -89,7 +115,7 @@ paths:
required:
- foo
responses:
'200':
"200":
description: parsed & validated query params
content:
application/json:
Expand Down
16 changes: 15 additions & 1 deletion test/serialized.objects.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ describe(packageJson.name, () => {
express
.Router()
.get(`/serialisable`, (req, res) => res.json(req.query))
.get(`/tags`, (req, res) => res.json(req.query)),
.get(`/tags`, (req, res) => res.json(req.query))
.get(`/deep_object`, (req, res) => res.json(req.query)),
),
);
});
Expand Down Expand Up @@ -104,4 +105,17 @@ describe(packageJson.name, () => {
.to.have.property('tag_ids')
.that.deep.equals([1]);
}));

it('should explode deepObject query params', async () =>
request(app)
.get(`${app.basePath}/deep_object?settings[state]=validated`)
.expect(200)
.then(r => {
const expected = {
settings: {
state: 'validated',
},
};
expect(r.body).to.deep.equals(expected);
}));
});
5 changes: 3 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
"resolveJsonModule": true,
"typeRoots": ["./node_modules/@types", "./typings"]
},
"exclude": ["node_modules"],
"exclude": ["node_modules", "a_reference"],
"include": [
"typings/**/*.d.ts",
"src/**/*.ts",
"src/framework/modded.express.mung.ts"
"src/framework/modded.express.mung.ts",
"a_reference/query.parser.ts"
]
}

0 comments on commit 3a7892a

Please sign in to comment.