Skip to content

Commit

Permalink
Adds query string serialization optimization
Browse files Browse the repository at this point in the history
Respects the `explode` attribute. When defined the actual serialization includes a duplication of the parameter. Otherwise arrays will be serialized in a comma-separated way.

Signed-off-by: André König <[email protected]>
  • Loading branch information
akoenig authored and Alan-Cha committed May 12, 2020
1 parent 0c12ebf commit 7c2573c
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 2 deletions.
14 changes: 13 additions & 1 deletion packages/openapi-to-graphql/src/oas_3_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,19 @@ export function instantiatePathAndGetQuery(

// Query parameters
case 'query':
query[param.name] = args[sanitizedParamName]
//
// Spec-compliant query string serialization:
// http://spec.openapis.org/oas/v3.0.3#style-examples
//
// Whenever the query string value is an array, we check if it
// should be `exploded`. In this case, we don't serialize anything.
// Otherwise, the array will be joined in comma-separated fashion.
//
//
const arg = args[sanitizedParamName]
const shouldBeCommaSeparated = Array.isArray(arg) && !param.explode

query[param.name] = shouldBeCommaSeparated ? arg.join(",") : arg
break

// Header parameters
Expand Down
9 changes: 8 additions & 1 deletion packages/openapi-to-graphql/src/resolver_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -577,7 +577,14 @@ export function getResolver({
)

return new Promise((resolve, reject) => {
NodeRequest(options, (err, response, body) => {
NodeRequest({...options,
//
// Use `native` querystring library to avoid `foo[0]=bar&foo[1]=baz`
// which is not spec compliant. See https://github.com/request/request#requestoptions-callback
// for further information.
//
useQuerystring: true,
}, (err, response, body) => {
if (err) {
httpLog(err)
reject(err)
Expand Down
6 changes: 6 additions & 0 deletions packages/openapi-to-graphql/test/example_api2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ test('Querying the two operations', () => {
user {
name
}
getRobots(types: ["Droid", "Bot"]) {
name
}
}`
return graphql(createdSchema, query).then(result => {
expect(result).toEqual({
Expand All @@ -84,6 +87,9 @@ test('Querying the two operations', () => {
},
user: {
name: 'William B Ropp'
},
getRobots: {
name: "Nkiru Gwendoline"
}
}
})
Expand Down
7 changes: 7 additions & 0 deletions packages/openapi-to-graphql/test/example_api2_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,13 @@ function startServer(PORT) {
})
})

app.get('/api/robots', (req, res) => {
console.log(req.method, req.path)
res.send({
name: "Nkiru Gwendoline"
})
})

return new Promise(resolve => {
server = app.listen(PORT, () => {
console.log(`Example API accessible on port ${PORT}`)
Expand Down
40 changes: 40 additions & 0 deletions packages/openapi-to-graphql/test/fixtures/example_oas2.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,36 @@
}
}
}
},
"/robots": {
"get": {
"description": "",
"parameters": [
{
"in": "query",
"name": "types",
"required": true,
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}
],
"responses": {
"200": {
"description": "",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/robot"
}
}
}
}
}
}
}
},
"components": {
Expand All @@ -86,6 +116,16 @@
"description": "The legal name of a user"
}
}
},
"robot": {
"type": "object",
"description": "A robot represents a not so natural person",
"properties": {
"name": {
"type": "string",
"description": "The legal name of a robot"
}
}
}
}
},
Expand Down

0 comments on commit 7c2573c

Please sign in to comment.