Skip to content

Commit 6b6060c

Browse files
Alan-ChaErikWittern
authored andcommitted
Fix #272
Signed-off-by: Alan Cha <[email protected]>
1 parent a820369 commit 6b6060c

File tree

6 files changed

+65
-22
lines changed

6 files changed

+65
-22
lines changed

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

Lines changed: 3 additions & 2 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/src/schema_builder.ts

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ function createOrReuseOt({
186186
if (def.ot && typeof def.ot !== 'undefined') {
187187
translationLog(
188188
`Reuse object type '${def.otName}'` +
189-
(typeof operation === 'object'
190-
? ` (for operation '${operation.operationId}')`
191-
: '')
189+
(typeof operation === 'object'
190+
? ` (for operation '${operation.operationId}')`
191+
: '')
192192
)
193193
return def.ot as (
194194
| GraphQLObjectType
@@ -201,9 +201,9 @@ function createOrReuseOt({
201201
if (def.iot && typeof def.iot !== 'undefined') {
202202
translationLog(
203203
`Reuse input object type '${def.iotName}'` +
204-
(typeof operation === 'object'
205-
? ` (for operation '${operation.operationId}')`
206-
: '')
204+
(typeof operation === 'object'
205+
? ` (for operation '${operation.operationId}')`
206+
: '')
207207
)
208208
return def.iot as GraphQLInputObjectType
209209
}
@@ -223,7 +223,8 @@ function createOrReuseOt({
223223
* Instead, store response in an arbitray JSON type.
224224
*/
225225
if (
226-
typeof def.schema.properties === 'undefined' &&
226+
(typeof def.schema.properties === 'undefined' ||
227+
Object.keys(def.schema.properties).length === 0) && // Empty object
227228
typeof def.schema.allOf === 'undefined' // allOf can provide all the properties
228229
// TODO: Add oneOf and anyOf
229230
) {
@@ -232,7 +233,7 @@ function createOrReuseOt({
232233
message:
233234
`The operation ` +
234235
`'${operation.operationString}' contains ` +
235-
`a object schema ${JSON.stringify(def)} with no properties. ` +
236+
`an object schema ${JSON.stringify(schema)} with no properties. ` +
236237
`GraphQL objects must have well-defined properties so a one to ` +
237238
`one conversion cannot be achieved.`,
238239
data,
@@ -245,9 +246,9 @@ function createOrReuseOt({
245246
if (!isInputObjectType) {
246247
translationLog(
247248
`Create object type '${def.otName}'` +
248-
(typeof operation === 'object'
249-
? ` (for operation '${operation.operationId}')`
250-
: '')
249+
(typeof operation === 'object'
250+
? ` (for operation '${operation.operationId}')`
251+
: '')
251252
)
252253

253254
def.ot = new GraphQLObjectType({
@@ -271,9 +272,9 @@ function createOrReuseOt({
271272
} else {
272273
translationLog(
273274
`Create input object type '${def.iotName}'` +
274-
(typeof operation === 'object'
275-
? ` (for operation '${operation.operationId}')`
276-
: '')
275+
(typeof operation === 'object'
276+
? ` (for operation '${operation.operationId}')`
277+
: '')
277278
)
278279

279280
def.iot = new GraphQLInputObjectType({
@@ -475,7 +476,6 @@ function createFields({
475476
: (objectType as GraphQLOutputType),
476477

477478
description: schema.description
478-
479479
}
480480
}
481481
}
@@ -563,7 +563,7 @@ function createFields({
563563
const resObjectType = linkedOp.responseDefinition.ot
564564

565565
let description = link.description
566-
566+
567567
if (data.options.equivalentToMessages && description) {
568568
description += `\n\nEquivalent to ${linkedOp.operationString}`
569569
}

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

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ test('Get descriptions', () => {
6464
{
6565
description: null
6666
},
67+
{
68+
description: null
69+
},
6770
{
6871
description: 'The model of the car.'
6972
},
@@ -806,14 +809,22 @@ test('Request data is correctly de-sanitized to be sent', () => {
806809
})
807810
})
808811

809-
// Testing additionalProperties field in schemas
810812
test('Fields with arbitrary JSON (e.g., maps) can be returned', () => {
813+
// Testing additionalProperties field in schemas
811814
const query = `{
812815
cars {
813816
tags
814817
}
815818
}`
816-
return graphql(createdSchema, query, null, {}).then(result => {
819+
820+
// Testing empty properties field
821+
const query2 = `{
822+
cars {
823+
features
824+
}
825+
}`
826+
827+
const promise = graphql(createdSchema, query, null, {}).then(result => {
817828
expect(result).toEqual({
818829
data: {
819830
cars: [
@@ -840,6 +851,31 @@ test('Fields with arbitrary JSON (e.g., maps) can be returned', () => {
840851
}
841852
})
842853
})
854+
855+
const promise2 = graphql(createdSchema, query2, null, {}).then(result => {
856+
expect(result).toEqual({
857+
data: {
858+
cars: [
859+
{
860+
features: {
861+
color: 'banana yellow to be specific'
862+
}
863+
},
864+
{
865+
features: null
866+
},
867+
{
868+
features: null
869+
},
870+
{
871+
features: null
872+
}
873+
]
874+
}
875+
})
876+
})
877+
878+
return Promise.all([promise, promise2])
843879
})
844880

845881
test('Capitalized enum values can be returned', () => {

packages/openapi-to-graphql/test/example_api_server.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,10 @@ function startServer(PORT) {
9898
model: 'Retro Rides',
9999
color: 'yellow',
100100
kind: 'SEDAN',
101-
rating: 100
101+
rating: 100,
102+
features: {
103+
color: 'banana yellow to be specific'
104+
}
102105
},
103106
will: {
104107
model: 'Speedzone Speedster',

packages/openapi-to-graphql/test/fixtures/example_oas.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,6 +1134,9 @@
11341134
"type": "string",
11351135
"description": "The color of the car."
11361136
},
1137+
"features": {
1138+
"properties": {}
1139+
},
11371140
"tags": {
11381141
"$ref": "#/components/schemas/tags"
11391142
},

0 commit comments

Comments
 (0)