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

Allow excepting keys from sanitization #454

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/openapi-to-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ const DEFAULT_OPTIONS: InternalOptions<any, any, any> = {
genericPayloadArgName: false,
simpleNames: false,
simpleEnumValues: false,
nonSanitizableObjectKeys: [],
singularNames: false,
createSubscriptionsFromCallbacks: false,

Expand Down Expand Up @@ -188,6 +189,7 @@ export function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
genericPayloadArgName,
simpleNames,
simpleEnumValues,
nonSanitizableObjectKeys,
singularNames,
createSubscriptionsFromCallbacks,

Expand Down Expand Up @@ -230,6 +232,7 @@ export function translateOpenAPIToGraphQL<TSource, TContext, TArgs>(
genericPayloadArgName,
simpleNames,
simpleEnumValues,
nonSanitizableObjectKeys,
singularNames,
createSubscriptionsFromCallbacks,

Expand Down
8 changes: 7 additions & 1 deletion packages/openapi-to-graphql/src/oas_3_tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,8 @@ function buildUrl(server: ServerObject): string {
*/
export function sanitizeObjectKeys(
obj: any, // obj does not necessarily need to be an object
caseStyle: CaseStyle = CaseStyle.camelCase
caseStyle: CaseStyle = CaseStyle.camelCase,
nonSanitizableObjectKeys?: string[]
): any {
const cleanKeys = (obj: any): any => {
// Case: no (response) data
Expand All @@ -477,6 +478,11 @@ export function sanitizeObjectKeys(
const res: object = {}

for (const key in obj) {
// Escape hatch if the caller really does not want to have these keys sanitized
if (nonSanitizableObjectKeys && nonSanitizableObjectKeys.includes(key)) {
res[key] = obj[key]
continue
}
const saneKey = sanitize(key, caseStyle)

if (Object.prototype.hasOwnProperty.call(obj, key)) {
Expand Down
3 changes: 2 additions & 1 deletion packages/openapi-to-graphql/src/resolver_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -832,7 +832,8 @@ export function getResolver<TSource, TContext, TArgs>({
responseBody,
!data.options.simpleNames
? Oas3Tools.CaseStyle.camelCase
: Oas3Tools.CaseStyle.simple
: Oas3Tools.CaseStyle.simple,
data.options.nonSanitizableObjectKeys
)

// Pass on _openAPIToGraphQL to subsequent resolvers
Expand Down
9 changes: 9 additions & 0 deletions packages/openapi-to-graphql/src/types/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,15 @@ export type InternalOptions<TSource, TContext, TArgs> = {
*/
simpleEnumValues: boolean

/**
* By default, field names are sanitized to conform with GraphQL conventions,
* i.e. fields should only contain alphanumeric characters.
*
* This option will prevent OpenAPI-to-GraphQL from sanitizing objects falling
* under the provided keys.
*/
nonSanitizableObjectKeys?: string[]

/**
* Experimental feature that will try to create more meaningful names from
* the operation path than the response object by leveraging common
Expand Down
28 changes: 28 additions & 0 deletions packages/openapi-to-graphql/test/example_api6.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -396,3 +396,31 @@ test('Handle no response schema', () => {
})
})
})

/**
* Get /testDynamicallyKeyedObject has an object with a key we don't want
* to desanitize.
*/
test('Properties present', () => {
const query = `{
dynamicallyKeyedObject{
name
dynamic
}
}`

const options: Options<any, any, any> = {
nonSanitizableObjectKeys: ["dynamic"]
}

return openAPIToGraphQL
.createGraphQLSchema(oas, options)
.then(({ schema, report }) => {
return graphql(schema, query).then((result) => {
expect(result.data.dynamicallyKeyedObject).toEqual({
name: 'Mr Dynamic',
dynamic: { 'ぁ': '56', '1234567': 'legit' },
})
})
})
})
6 changes: 6 additions & 0 deletions packages/openapi-to-graphql/test/example_api6_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ function startServer(PORT) {
res.send({"nesting1": {"nesting2": 5} })
})

app.get('/api/testDynamicallyKeyedObject', (req, res) => {
res.send({
name: 'Mr Dynamic',
dynamic: { 'ぁ': '56', '1234567': 'legit' },
})
})

return new Promise((resolve) => {
server = app.listen(PORT, () => {
Expand Down
30 changes: 29 additions & 1 deletion packages/openapi-to-graphql/test/fixtures/example_oas6.json
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,24 @@
"operationId": "returnNumber",
"parameters": {
"number": "$response.body#/nesting1/nesting2"
}
}
}
}
}
}
}
},
"/testDynamicallyKeyedObject": {
"get": {
"description": "Test object with dynamic keyes that shouldn't be sanitized.",
"responses": {
"200": {
"description": "A successful response.",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/dynamicallyKeyedObject"
}
}
}
}
Expand Down Expand Up @@ -465,6 +482,17 @@
"$ref": "#/components/schemas/russianDoll"
}
}
},
"dynamicallyKeyedObject": {
"type": "object",
"properties": {
"name": {
"type": "string"
},
"dynamic": {
"type": "object"
}
}
}
},
"links": {
Expand Down
20 changes: 20 additions & 0 deletions packages/openapi-to-graphql/test/oas_3_tools.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ test('Sanitize object keys when given an array', () => {
])
})

test('Sanitize object keys with exceptions', () => {
const obj = [
{
properties: {
5353535353: 'test',
'££$£$': 'fine'
}
}
]
const clean = Oas3Tools.sanitizeObjectKeys(obj, Oas3Tools.CaseStyle.camelCase, ["properties"])
expect(clean).toEqual([
{
properties: {
5353535353: 'test',
'££$£$': 'fine'
}
}
])
})

const mapping = {
productId: 'product-id',
productName: 'product-name',
Expand Down