Skip to content

Commit 6555074

Browse files
committed
Add test for composite oneOf allOf link
Signed-off-by: Alan Cha <[email protected]>
1 parent 530dee6 commit 6555074

File tree

5 files changed

+184
-11
lines changed

5 files changed

+184
-11
lines changed

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

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,10 +87,10 @@ function processOperation<TSource, TContext, TArgs>(
8787
if (data.options.equivalentToMessages) {
8888
// Description may not exist
8989
if (typeof description !== 'string') {
90-
description = ''
90+
description = `Equivalent to ${operationString}`
91+
} else {
92+
description += `\n\nEquivalent to ${operationString}`
9193
}
92-
93-
description += `\n\nEquivalent to ${operationString}`
9494
}
9595

9696
// Tags
@@ -699,7 +699,13 @@ export function createDataDef<TSource, TContext, TArgs>(
699699
// Found existing data definition and fetch it
700700
const existingDataDef = data.defs[index]
701701

702-
if (existingDataDef.targetGraphQLType === TargetGraphQLType.oneOfUnion && Array.isArray(existingDataDef.subDefinitions) // Special handling for oneOf. Sub definitions are always an array (see createOneOfUnion)
702+
/**
703+
* Special handling for oneOf. Subdefinitions are always an array
704+
* (see createOneOfUnion)
705+
*/
706+
if (
707+
existingDataDef.targetGraphQLType === TargetGraphQLType.oneOfUnion &&
708+
Array.isArray(existingDataDef.subDefinitions)
703709
) {
704710
existingDataDef.subDefinitions.forEach((def) => {
705711
collapseLinksIntoDataDefinition({
@@ -1140,8 +1146,6 @@ function sanitizeLinks<TSource, TContext, TArgs>({
11401146
/**
11411147
* Given an existing data definition, collapse the link object with the existing
11421148
* one captured in the data definition.
1143-
*
1144-
*
11451149
*/
11461150
function collapseLinksIntoDataDefinition<TSource, TContext, TArgs>({
11471151
additionalLinks,
@@ -1558,7 +1562,8 @@ function createOneOfUnion<TSource, TContext, TArgs>(
15581562
memberSchema,
15591563
isInputObjectType,
15601564
data,
1561-
oas
1565+
oas,
1566+
def.links
15621567
)
15631568
;(def.subDefinitions as DataDefinition[]).push(subDefinition)
15641569
})

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -812,8 +812,12 @@ function createFields<TSource, TContext, TArgs>({
812812

813813
let description = link.description
814814

815-
if (data.options.equivalentToMessages && description) {
816-
description += `\n\nEquivalent to ${linkedOp.operationString}`
815+
if (data.options.equivalentToMessages) {
816+
if (typeof description !== 'string') {
817+
description = `Equivalent to ${linkedOp.operationString}`
818+
} else {
819+
description += `\n\nEquivalent to ${linkedOp.operationString}`
820+
}
817821
}
818822

819823
// Finally, add the object type to the fields (using sanitized field name)

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

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -676,3 +676,70 @@ test('oneOf test with allOf, requiring oneOf collapse\n\nEquivalent to GET /oneO
676676
})
677677
})
678678
})
679+
680+
/**
681+
* oneOf contains two member schemas, each with allOf
682+
*
683+
* oneOf also contains a link object
684+
*
685+
* Resolving the oneOf and allOfs should correctly create a union of two object
686+
* types, each object type with a link field from the oneOf schema
687+
*/
688+
test('oneOf test with allOfs, requiring oneOf collapse\n\nEquivalent to GET /OneOfWithAllOfsAndLink', () => {
689+
return graphql(createdSchema, oneOfQuery).then((result) => {
690+
expect(
691+
result.data['__schema'].queryType.fields.find((field) => {
692+
return field.name === 'oneOfWithAllOfsAndLink'
693+
})
694+
).toEqual({
695+
"name": "oneOfWithAllOfsAndLink",
696+
"description": "Equivalent to GET /oneOfWithAllOfsAndLink",
697+
"type": {
698+
"name": "OneOfWithAllOfsAndLink",
699+
"kind": "UNION",
700+
"possibleTypes": [
701+
{
702+
"name": "One",
703+
"fields": [
704+
{
705+
"type": {
706+
"name": "String"
707+
}
708+
},
709+
{
710+
"type": {
711+
"name": "String"
712+
}
713+
},
714+
{
715+
"type": {
716+
"name": "String"
717+
}
718+
}
719+
]
720+
},
721+
{
722+
"name": "Two",
723+
"fields": [
724+
{
725+
"type": {
726+
"name": "String"
727+
}
728+
},
729+
{
730+
"type": {
731+
"name": "String"
732+
}
733+
},
734+
{
735+
"type": {
736+
"name": "String"
737+
}
738+
}
739+
]
740+
}
741+
]
742+
}
743+
})
744+
})
745+
})

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@ const openAPIToGraphQL = require('../dist/index')
1414
// const oas = require('./fixtures/example_oas.json')
1515
// const oas = require('./fixtures/example_oas2.json')
1616
// const oas = require('./fixtures/example_oas3.json')
17-
// const oas = require('./fixtures/example_oas4.json')
17+
const oas = require('./fixtures/example_oas4.json')
1818
// const oas = require('./fixtures/example_oas5.json')
19-
const oas = require('./fixtures/example_oas6.json')
19+
// const oas = require('./fixtures/example_oas6.json')
20+
// const oas = require('./fixtures/example_oas7.json')
21+
// const oas = require('./fixtures/example_oas8.json')
2022
// const oas = require('./fixtures/file_upload.json')
2123

2224
// const oas = require('./fixtures/github.json')

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

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,47 @@
506506
}
507507
}
508508
}
509+
},
510+
"/testLink": {
511+
"get": {
512+
"operationId": "testLink",
513+
"responses": {
514+
"200": {
515+
"description": "Success",
516+
"content": {
517+
"application/json": {
518+
"schema": {
519+
"type": "string"
520+
}
521+
}
522+
}
523+
}
524+
}
525+
}
526+
},
527+
"/oneOfWithAllOfsAndLink": {
528+
"get": {
529+
"responses": {
530+
"200": {
531+
"description": "Return a user.\n\nEquivalent to GET /oneOfWithAllOfsAndLink",
532+
"content": {
533+
"application/json": {
534+
"schema": {
535+
"$ref": "#/components/schemas/OneOfWithAllOfsAndLink"
536+
}
537+
}
538+
},
539+
"links": {
540+
"testLink": {
541+
"operationId": "testLink",
542+
"parameters": {
543+
"someId": "$request.path.id"
544+
}
545+
}
546+
}
547+
}
548+
}
549+
}
509550
}
510551
},
511552
"components": {
@@ -533,6 +574,60 @@
533574
"type": "string"
534575
}
535576
}
577+
},
578+
"OneOfWithAllOfsAndLink": {
579+
"oneOf": [
580+
{
581+
"$ref": "#/components/schemas/One"
582+
},
583+
{
584+
"$ref": "#/components/schemas/Two"
585+
}
586+
]
587+
},
588+
"Abstract": {
589+
"type": "object",
590+
"properties": {
591+
"commonProp": {
592+
"type": "string"
593+
}
594+
}
595+
},
596+
"OneProps": {
597+
"type": "object",
598+
"properties": {
599+
"differentOneProp": {
600+
"type": "string"
601+
}
602+
}
603+
},
604+
"TwoProps": {
605+
"type": "object",
606+
"properties": {
607+
"differentTwoProp": {
608+
"type": "string"
609+
}
610+
}
611+
},
612+
"One": {
613+
"allOf": [
614+
{
615+
"$ref": "#/components/schemas/Abstract"
616+
},
617+
{
618+
"$ref": "#/components/schemas/OneProps"
619+
}
620+
]
621+
},
622+
"Two": {
623+
"allOf": [
624+
{
625+
"$ref": "#/components/schemas/Abstract"
626+
},
627+
{
628+
"$ref": "#/components/schemas/TwoProps"
629+
}
630+
]
536631
}
537632
}
538633
}

0 commit comments

Comments
 (0)