Skip to content

Commit de0a5f8

Browse files
Alan-ChaErikWittern
authored andcommitted
Store object with no properties in arbitray JSON type
Signed-off-by: Alan Cha <[email protected]>
1 parent 2a258bd commit de0a5f8

File tree

10 files changed

+44
-104
lines changed

10 files changed

+44
-104
lines changed

packages/openapi-to-graphql/lib/resolver_builder.js

Lines changed: 0 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/resolver_builder.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/schema_builder.js

Lines changed: 2 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/schema_builder.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/utils.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/lib/utils.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/openapi-to-graphql/src/resolver_builder.ts

Lines changed: 0 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -409,11 +409,6 @@ export function getResolver({
409409

410410
resolveData.responseHeaders = response.headers
411411

412-
responseBody = stringifyObjectsWithNoProperties(
413-
responseBody,
414-
operation.responseDefinition
415-
)
416-
417412
// Deal with the fact that the server might send unsanitized data
418413
let saneData = Oas3Tools.sanitizeObjKeys(responseBody)
419414

@@ -904,41 +899,3 @@ function graphQLErrorWithExtensions(
904899
): GraphQLError {
905900
return new GraphQLError(message, null, null, null, null, null, extensions)
906901
}
907-
908-
/**
909-
* Ideally, all objects should be defined with properties. However, if they are
910-
* not, then we can at least stringify the data.
911-
*
912-
* This function recursively ensures that objects have properties, if not
913-
* stringify the data to match the mitigation.
914-
*/
915-
function stringifyObjectsWithNoProperties(
916-
data: any,
917-
dataDef: DataDefinition
918-
): any {
919-
if (typeof dataDef !== 'undefined') {
920-
if (dataDef.type === 'array') {
921-
data = data.map(element => {
922-
return stringifyObjectsWithNoProperties(
923-
element,
924-
dataDef.subDefinitions as DataDefinition
925-
)
926-
})
927-
} else if (dataDef.type === 'object') {
928-
if (typeof dataDef.schema.properties !== 'undefined') {
929-
Object.keys(data).forEach(propertyName => {
930-
data[propertyName] = stringifyObjectsWithNoProperties(
931-
data[propertyName],
932-
dataDef.subDefinitions[propertyName]
933-
)
934-
})
935-
936-
// Schema does not have properties defined, therefore stringify
937-
} else {
938-
return JSON.stringify(data)
939-
}
940-
}
941-
}
942-
943-
return data
944-
}

packages/openapi-to-graphql/src/schema_builder.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,7 @@ function createOrReuseOt({
207207
* cannot create a GraphQL Object Type for it because in GraphQL, all Object
208208
* Type properties must be named.
209209
*
210-
* Instead, stringify the response.
211-
*
212-
* NOTE: there is a similar check in the resolver_builder.ts so that the
213-
* response data is properly stringified.
214-
*
215-
* See stringifyObjectsWithNoProperties() function
210+
* Instead, store response in an arbitray JSON type.
216211
*/
217212
if (typeof def.schema.properties === 'undefined') {
218213
handleWarning({
@@ -226,7 +221,7 @@ function createOrReuseOt({
226221
data,
227222
log: translationLog
228223
})
229-
return GraphQLString
224+
return GraphQLJSON
230225
}
231226

232227
const description =

packages/openapi-to-graphql/src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ export const mitigations = {
2323
UNRESOLVABLE_REFERENCE: `The schema will not be resolved.`,
2424
UNSUPPORTED_HTTP_SECURITY_SCHEME: `Ignore security scheme.`,
2525
NON_APPLICATION_JSON_SCHEMA: `Ignore schema`,
26-
OBJECT_MISSING_PROPERTIES: `The (sub-)object will be stringified. The property will return a string in the interface.`,
26+
OBJECT_MISSING_PROPERTIES: `The (sub-)object will be stored in an arbitray JSON type.`,
2727

2828
// Links
2929
UNRESOLVABLE_LINK: `Ignore link.`,

packages/openapi-to-graphql/test/example_api.test.ts

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1259,7 +1259,7 @@ test('Content property in parameter object', () => {
12591259
})
12601260
})
12611261

1262-
test('Stringify objects without defined properties', () => {
1262+
test('Handle objects without defined properties with arbitrary GraphQL JSON type', () => {
12631263
const query = `{
12641264
trashcan(username:"arlene") {
12651265
brand,
@@ -1272,28 +1272,50 @@ test('Stringify objects without defined properties', () => {
12721272
return graphql(createdSchema, query).then(result => {
12731273
expect(result).toEqual({
12741274
data: {
1275-
trashcan: {
1276-
brand: '"Garbage Emporium"',
1277-
contents: [
1278-
'{"type":"apple","message":"Half-eaten"}',
1279-
'{"type":"sock","message":"Lost one"}'
1275+
"trashcan": {
1276+
"brand": "Garbage Emporium",
1277+
"contents": [
1278+
{
1279+
"type": "apple",
1280+
"message": "Half-eaten"
1281+
},
1282+
{
1283+
"type": "sock",
1284+
"message": "Lost one"
1285+
}
12801286
]
12811287
},
1282-
trashcans: [
1288+
"trashcans": [
12831289
{
1284-
contents: [
1285-
'{"type":"apple","message":"Half-eaten"}',
1286-
'{"type":"sock","message":"Lost one"}'
1290+
"contents": [
1291+
{
1292+
"type": "apple",
1293+
"message": "Half-eaten"
1294+
},
1295+
{
1296+
"type": "sock",
1297+
"message": "Lost one"
1298+
}
12871299
]
12881300
},
12891301
{
1290-
contents: ['{"type":"sock","message":"Lost one"}']
1302+
"contents": [
1303+
{
1304+
"type": "sock",
1305+
"message": "Lost one"
1306+
}
1307+
]
12911308
},
12921309
{
1293-
contents: []
1310+
"contents": []
12941311
},
12951312
{
1296-
contents: ['{"type":"tissue","message":"Used"}']
1313+
"contents": [
1314+
{
1315+
"type": "tissue",
1316+
"message": "Used"
1317+
}
1318+
]
12971319
}
12981320
]
12991321
}

0 commit comments

Comments
 (0)