Skip to content

Commit

Permalink
feat: spike on url encoding of json object in query param
Browse files Browse the repository at this point in the history
  • Loading branch information
deepakrkris committed Nov 18, 2019
1 parent fe83941 commit 7f6f73d
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion examples/todo/src/__tests__/acceptance/todo.acceptance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ describe('TodoApplication', () => {

await client
.get('/todos')
.query({filter: {where: {isComplete: false}}})
.query({filter: encodeURIComponent(JSON.stringify({where: {isComplete: false}})) })
.expect(200, [toJSON(todoInProgress)]);
});

Expand Down
2 changes: 1 addition & 1 deletion examples/todo/src/controllers/todo.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class TodoController {
},
})
async findTodos(
@param.query.object('filter', getFilterSchemaFor(Todo))
@param.query.jsonObject('filter', getFilterSchemaFor(Todo))
filter?: Filter<Todo>,
): Promise<Todo[]> {
return this.todoRepo.find(filter);
Expand Down
31 changes: 31 additions & 0 deletions packages/openapi-v3/src/decorators/parameter.decorator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,8 +218,39 @@ export namespace param {
...spec,
});
},

/**
* Define a parameter accepting a url encoded string of a JSON object,
*
* parameter is created with explode: false
*
* @param name - Parameter name
* @param schema - Optional OpenAPI Schema describing the JSON object.
*/
jsonObject: function(
name: string,
schema: SchemaObject | ReferenceObject = {
type: 'object',
additionalProperties: true,
},
spec?: Partial<ParameterObject>,
) {
schema = {
type: 'object',
...schema,
};
return param({
name,
in: 'query',
style: 'deepObject',
explode: false,
schema,
...spec,
});
},
};


/**
* Header parameter decorator
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/rest/src/coercion/coerce-parameter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,12 @@ function parseJsonIfNeeded(
return undefined;
}

// if query param is deepObject but explode is false
// then a url encoded string is expected
if (spec.in === 'query' && spec.style === 'deepObject' && !spec.explode) {
data = decodeURIComponent(data);
}

try {
const result = parseJson(data);
debug('Parsed parameter %s as %j', spec.name, result);
Expand Down

0 comments on commit 7f6f73d

Please sign in to comment.